欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP图片重新采样及大小缩放函数imagecopyresampled用法实例

shiping1 的头像
(PHP 4 >= 4.0.6, PHP 5)

imagecopyresampled — 重采样拷贝部分图像并调整大小

imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,减小了图像的大小而仍然保持了极大的清晰度。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_image 和src_image 相同的话)区域,但如果区域交迭的话则结果不可预知。

函数参数如下:


  1. bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

 

今天根据这个函数写了个方法,能够等比缩放图片的大小,并且可以设置宽度固定高度等比缩放,或者高度固定宽度等比缩放,或者设置最大宽和最大高。具体代码如下:


  1. /**
  2. * 图片缩放函数(可设置高度固定,宽度固定或者最大宽高,支持gif/jpg/png三种类型)
  3. * Author : Specs
  4. * Homepage: http://9iphp.com
  5. *
  6. * @param string $source_path 源图片
  7. * @param int $target_width 目标宽度
  8. * @param int $target_height 目标高度
  9. * @param string $fixed_orig 锁定宽高(可选参数 width、height或者空值)
  10. * @return string
  11. */
  12. function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = ''){
  13. $source_info = getimagesize($source_path);
  14. $source_width = $source_info[0];
  15. $source_height = $source_info[1];
  16. $source_mime = $source_info['mime'];
  17. $ratio_orig = $source_width / $source_height;
  18. if ($fixed_orig == 'width'){
  19. //宽度固定
  20. $target_height = $target_width / $ratio_orig;
  21. }elseif ($fixed_orig == 'height'){
  22. //高度固定
  23. $target_width = $target_height * $ratio_orig;
  24. }else{
  25. //最大宽或最大高
  26. if ($target_width / $target_height > $ratio_orig){
  27. $target_width = $target_height * $ratio_orig;
  28. }else{
  29. $target_height = $target_width / $ratio_orig;
  30. }
  31. }
  32. switch ($source_mime){
  33. case 'image/gif':
  34. $source_image = imagecreatefromgif($source_path);
  35. break;
  36.  
  37. case 'image/jpeg':
  38. $source_image = imagecreatefromjpeg($source_path);
  39. break;
  40.  
  41. case 'image/png':
  42. $source_image = imagecreatefrompng($source_path);
  43. break;
  44.  
  45. default:
  46. return false;
  47. break;
  48. }
  49. $target_image = imagecreatetruecolor($target_width, $target_height);
  50. imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);
  51. //header('Content-type: image/jpeg');
  52. $imgArr = explode('.', $source_path);
  53. $target_path = $imgArr[0] . '_new.' . $imgArr[1];
  54. imagejpeg($target_image, $target_path, 100);
  55. }

用法如下:


  1. myImageResize($filename, 200, 200); //最大宽高
  2. myImageResize($filename, 200, 200, 'width'); //宽度固定
  3. myImageResize($filename, 200, 200, 'height'); //高度固定

宽图结果如下:

imagecopyresampled(原图)

imagecopyresampled
最大宽高

imagecopyresampled固定高

imagecopyresampled
固定宽

高图结果如下:

imagecopyresampled
原图

imagecopyresampled
最大宽高

imagecopyresampled
固定高

imagecopyresampled
固定宽

附:把图片剪切为固定大小的函数:


  1. function imagecropper($source_path, $target_width, $target_height){
  2. $source_info = getimagesize($source_path);
  3. $source_width = $source_info[0];
  4. $source_height = $source_info[1];
  5. $source_mime = $source_info['mime'];
  6. $source_ratio = $source_height / $source_width;
  7. $target_ratio = $target_height / $target_width;
  8.  
  9. // 源图过高
  10. if ($source_ratio > $target_ratio){
  11. $cropped_width = $source_width;
  12. $cropped_height = $source_width * $target_ratio;
  13. $source_x = 0;
  14. $source_y = ($source_height - $cropped_height) / 2;
  15. }elseif ($source_ratio < $target_ratio){ // 源图过宽
  16. $cropped_width = $source_height / $target_ratio;
  17. $cropped_height = $source_height;
  18. $source_x = ($source_width - $cropped_width) / 2;
  19. $source_y = 0;
  20. }else{ // 源图适中
  21. $cropped_width = $source_width;
  22. $cropped_height = $source_height;
  23. $source_x = 0;
  24. $source_y = 0;
  25. }
  26.  
  27. switch ($source_mime){
  28. case 'image/gif':
  29. $source_image = imagecreatefromgif($source_path);
  30. break;
  31.  
  32. case 'image/jpeg':
  33. $source_image = imagecreatefromjpeg($source_path);
  34. break;
  35.  
  36. case 'image/png':
  37. $source_image = imagecreatefrompng($source_path);
  38. break;
  39.  
  40. default:
  41. return false;
  42. break;
  43. }
  44.  
  45. $target_image = imagecreatetruecolor($target_width, $target_height);
  46. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  47.  
  48. // 裁剪
  49. imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  50. // 缩放
  51. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  52. $dotpos = strrpos($source_path, '.');
  53. $imgName = substr($source_path, 0, $dotpos);
  54. $suffix = substr($source_path, $dotpos);
  55. $imgNew = $imgName . '_small' . $suffix;
  56. imagejpeg($target_image, $imgNew, 100);
  57. imagedestroy($source_image);
  58. imagedestroy($target_image);
  59. imagedestroy($cropped_image);
  60. }
  61. 来自 http://9iphp.com/web/php/1208.html
  62.  
 

普通分类: