首页 > 语言 > PHP > 正文

php实现图片按比例截取的方法

2024-09-04 11:44:40
字体:
来源:转载
供稿:网友
  1. filename ='img/test.jpg' 
  2. $all_type=array
  3. "jpg" =>array("create"=>"ImageCreateFromjpeg","output"=>"imagejpeg" ,"exn"=>".jpg"), 
  4. "gif" =>array("create"=>"ImageCreateFromGIF","output"=>"imagegif" ,"exn"=>".gif"), 
  5. "jpeg" =>array("create"=>"ImageCreateFromjpeg","output"=>"imagejpeg" ,"exn"=>".jpg"), 
  6. "png" =>array("create"=>"imagecreatefrompng","output"=>"imagepng" ,"exn"=>".png"), 
  7. "wbmp" =>array("create"=>"imagecreatefromwbmp","output"=>"image2wbmp","exn"=>".wbmp"
  8. ); 
  9. $imgtype=getimagesize($filename); 
  10. $width=$imgtype[0]; 
  11. $height=$imgtype[1]; 
  12. $type=str_replace('image/','',$imgtype['mime']); 
  13. $func_create=$all_type[$type]['create']; 
  14. $func_output=$all_type[$type]['output']; 
  15. $x=$y=0; 
  16. if(($width* 100)>($height* 120)) 
  17. $newwidth=ceil($height* 120/100); 
  18. $newheight=$height
  19. $x= ($width-$newwidth)/2; 
  20. elseif(($width* 100)<($height* 120)) 
  21. $newheight=ceil($width* 100/120); 
  22. $newwidth=$width
  23. $y= ($height-$newheight)/2; 
  24. else 
  25. $newheight=$height
  26. $newwidth=$width
  27. // Load 
  28. $thumb= imagecreatetruecolor($newwidth,$newheight); 
  29. $source=$func_create($filename); 
  30. // Resize 
  31. imagecopyresized($thumb,$source, 0, 0, 0, 0,$newwidth,$newheight,$newwidth,$newheight); 
  32. // Output 
  33. $func_output($thumb,'a.jpeg'); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表