Heim  >  Artikel  >  Backend-Entwicklung  >  PHP图片裁剪函数(图像不变形)

PHP图片裁剪函数(图像不变形)

WBOY
WBOYOriginal
2016-07-25 08:53:32828Durchsuche
  1. *exif_imagetype -- 判断一个图像的类型
  2. *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
  3. * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高
  4. */
  5. // 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白
  6. function my_image_resize($src_file, $dst_file , $new_width , $new_height) {
  7. $new_width= intval($new_width);
  8. $new_height=intval($new_width);
  9. if($new_width echo "params width or height error !";
  10. exit();
  11. }
  12. if(!file_exists($src_file)) {
  13. echo $src_file . " is not exists !";
  14. exit();
  15. }
  16. // 图像类型
  17. $type=exif_imagetype($src_file);
  18. $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
  19. if(!in_array($type, $support_type,true)) {
  20. echo "this type of image does not support! only support jpg , gif or png";
  21. exit();
  22. }
  23. //Load image
  24. switch($type) {
  25. case IMAGETYPE_JPEG :
  26. $src_img=imagecreatefromjpeg($src_file);
  27. break;
  28. case IMAGETYPE_PNG :
  29. $src_img=imagecreatefrompng($src_file);
  30. break;
  31. case IMAGETYPE_GIF :
  32. $src_img=imagecreatefromgif($src_file);
  33. break;
  34. default:
  35. echo "Load image error!";
  36. exit();
  37. }
  38. $w=imagesx($src_img);
  39. $h=imagesy($src_img);
  40. $ratio_w=1.0 * $new_width / $w;
  41. $ratio_h=1.0 * $new_height / $h;
  42. $ratio=1.0;
  43. // 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
  44. if( ($ratio_w 1 && $ratio_h > 1)) {
  45. if($ratio_w $ratio = $ratio_h ; // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大
  46. }else {
  47. $ratio = $ratio_w ;
  48. }
  49. // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
  50. $inter_w=(int)($new_width / $ratio);
  51. $inter_h=(int) ($new_height / $ratio);
  52. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  53. //var_dump($inter_img);
  54. imagecopy($inter_img, $src_img, 0,0,0,0,$inter_w,$inter_h);
  55. // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
  56. // 定义一个新的图像
  57. $new_img=imagecreatetruecolor($new_width,$new_height);
  58. //var_dump($new_img);exit();
  59. imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
  60. switch($type) {
  61. case IMAGETYPE_JPEG :
  62. imagejpeg($new_img, $dst_file,100); // 存储图像
  63. break;
  64. case IMAGETYPE_PNG :
  65. imagepng($new_img,$dst_file,100);
  66. break;
  67. case IMAGETYPE_GIF :
  68. imagegif($new_img,$dst_file,100);
  69. break;
  70. default:
  71. break;
  72. }
  73. } // end if 1
  74. // 2 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪
  75. // =if( ($ratio_w 1) || ($ratio_w >1 && $ratio_h else{
  76. $ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //取比例大的那个值
  77. // 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
  78. $inter_w=(int)($w * $ratio);
  79. $inter_h=(int) ($h * $ratio);
  80. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  81. //将原图缩放比例后裁剪
  82. imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h);
  83. // 定义一个新的图像
  84. $new_img=imagecreatetruecolor($new_width,$new_height);
  85. imagecopy($new_img, $inter_img, 0,0,0,0,$new_width,$new_height);
  86. switch($type) {
  87. case IMAGETYPE_JPEG :
  88. imagejpeg($new_img, $dst_file,100); // 存储图像
  89. break;
  90. case IMAGETYPE_PNG :
  91. imagepng($new_img,$dst_file,100);
  92. break;
  93. case IMAGETYPE_GIF :
  94. imagegif($new_img,$dst_file,100);
  95. break;
  96. default:
  97. break;
  98. }
  99. }// if3
  100. }// end function
  101. my_image_resize('test.gif','11111.gif','100px','100px');
  102. ?>
复制代码

小编总结: 以上实现的php图像裁剪函数,灵活应用了php gd库的相关函数,包括 imagecreatefromjpeg、imagecreatefrompng、imagecreatefromgif、imagecreatetruecolor等函数的用法。 因此,灵活掌握php gd库中各函数的用法,对于裁剪图片、水印、缩略图等功能的实现,至关重要。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn