PHP图片大小裁剪imagecopy、缩放imagecopyresampled函数

图片裁剪、缩放函数有很多,但是我推荐这两个.有点小修改.
//图片裁剪、缩放函数
1.

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. //$filepath图片路径,$percent缩放百分比  
  2. function imagepress($filepath,$percent='0.5'){  
  3. // 图片类型  
  4. header('Content-Type: image/jpeg');  
  5. // 获得新的图片大小  
  6. list($width, $height) = getimagesize($filepath);  
  7. $new_width = $width * $percent;  
  8. $new_height = $height * $percent;  
  9. // 重新取样  
  10. $image_p = imagecreatetruecolor($new_width, $new_height);  
  11. $image = imagecreatefromjpeg($filepath);  
  12. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
  13. // 输出  
  14. return imagejpeg($image_p, null, 100);  
  15. }  

PHP图片大小裁剪、缩放函数原图:

CSDN websites博客 关注移动互联网 关注websites

PHP图片大小裁剪、缩放函数效果图:

CSDN websites博客 关注移动互联网 关注websites

2.

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. //$filepath图片路径,$new_width新的宽度,$new_height新的高度  
  2. function imagepress($filepath, $new_width, $new_height)  
  3. {  
  4. $source_info   = getimagesize($filepath);  
  5. $source_width  = $source_info[0];  
  6. $source_height = $source_info[1];  
  7. $source_mime   = $source_info['mime'];  
  8. $source_ratio  = $source_height / $source_width;  
  9. $target_ratio  = $new_height / $new_width;  
  10. // 源图过高  
  11. if ($source_ratio > $target_ratio)  
  12. {  
  13. $cropped_width  = $source_width;  
  14. $cropped_height = $source_width * $target_ratio;  
  15. $source_x = 0;  
  16. $source_y = ($source_height - $cropped_height) / 2;  
  17. }  
  18. // 源图过宽  
  19. elseif ($source_ratio < $target_ratio)  
  20. {  
  21. $cropped_width  = $source_height / $target_ratio;  
  22. $cropped_height = $source_height;  
  23. $source_x = ($source_width - $cropped_width) / 2;  
  24. $source_y = 0;  
  25. }  
  26. // 源图适中  
  27. else  
  28. {  
  29. $cropped_width  = $source_width;  
  30. $cropped_height = $source_height;  
  31. $source_x = 0;  
  32. $source_y = 0;  
  33. }  
  34. switch ($source_mime)  
  35. {  
  36. case 'image/gif':  
  37. $source_image = imagecreatefromgif($filepath);  
  38. break;  
  39. case 'image/jpeg':  
  40. $source_image = imagecreatefromjpeg($filepath);  
  41. break;  
  42. case 'image/png':  
  43. $source_image = imagecreatefrompng($filepath);  
  44. break;  
  45. default:  
  46. return false;  
  47. break;  
  48. }  
  49. $target_image  = imagecreatetruecolor($new_width, $new_height);  
  50. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);  
  51. // 裁剪  
  52. imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);  
  53. // 缩放  
  54. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $new_width, $new_height, $cropped_width, $cropped_height);  
  55. header('Content-Type: image/jpeg');  
  56. imagejpeg($target_image);  
  57. imagedestroy($source_image);  
  58. imagedestroy($target_image);  
  59. imagedestroy($cropped_image);  
  60. }  

 

本文地址: http://blog.csdn.NET/websites/article/details/18594321

websites博客 关注移动互联网 关注websites

posted @ 2016-12-29 15:55  天涯海角路  阅读(747)  评论(0)    收藏  举报