当前位置:Gxlcms > PHP教程 > PHP图片裁剪函数(图像不变形)

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

时间:2021-07-01 10:21:17 帮助过:4人阅读

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

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

人气教程排行