当前位置:Gxlcms > PHP教程 > php中自定义图像居中裁剪函数实现的代码案例

php中自定义图像居中裁剪函数实现的代码案例

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

本文实例讲述了PHP实现的自定义图像居中裁剪函数。分享给大家供大家参考,具体如下:

图像居中裁减的大致思路:

1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域。(imagecopyresampled — 重采样拷贝部分图像并调整大小)

2.将缩放后的图像放置在裁减区域中间。(imagecopy — 拷贝图像的一部分)

3.裁减图像并保存。(imagejpeg | imagepng | imagegif — 输出图象到浏览器或文件)

具体代码:


  1. //==================缩放裁剪函数====================
  2. /**
  3. * 居中裁剪图片
  4. * @param string $source [原图路径]
  5. * @param int $width [设置宽度]
  6. * @param int $height [设置高度]
  7. * @param string $target [目标路径]
  8. * @return bool [裁剪结果]
  9. */
  10. function image_center_crop($source, $width, $height, $target)
  11. {
  12. if (!file_exists($source)) return false;
  13. /* 根据类型载入图像 */
  14. switch (exif_imagetype($source)) {
  15. case IMAGETYPE_JPEG:
  16. $image = imagecreatefromjpeg($source);
  17. break;
  18. case IMAGETYPE_PNG:
  19. $image = imagecreatefrompng($source);
  20. break;
  21. case IMAGETYPE_GIF:
  22. $image = imagecreatefromgif($source);
  23. break;
  24. }
  25. if (!isset($image)) return false;
  26. /* 获取图像尺寸信息 */
  27. $target_w = $width;
  28. $target_h = $height;
  29. $source_w = imagesx($image);
  30. $source_h = imagesy($image);
  31. /* 计算裁剪宽度和高度 */
  32. $judge = (($source_w / $source_h) > ($target_w / $target_h));
  33. $resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w;
  34. $resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h;
  35. $start_x = $judge ? ($resize_w - $target_w) / 2 : 0;
  36. $start_y = !$judge ? ($resize_h - $target_h) / 2 : 0;
  37. /* 绘制居中缩放图像 */
  38. $resize_img = imagecreatetruecolor($resize_w, $resize_h);
  39. imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h);
  40. $target_img = imagecreatetruecolor($target_w, $target_h);
  41. imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h);
  42. /* 将图片保存至文件 */
  43. if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true);
  44. switch (exif_imagetype($source)) {
  45. case IMAGETYPE_JPEG:
  46. imagejpeg($target_img, $target);
  47. break;
  48. case IMAGETYPE_PNG:
  49. imagepng($target_img, $target);
  50. break;
  51. case IMAGETYPE_GIF:
  52. imagegif($target_img, $target);
  53. break;
  54. }
  55. // return boolval(file_exists($target));//PHP5.5以上可用boolval()函数获取返回的布尔值
  56. return file_exists($target)?true:false;//兼容低版本PHP写法
  57. }


  1. //==================函数使用方式====================
  2. // 原始图片的路径
  3. $source = '../source/img/middle.jpg';
  4. $width = 480; // 裁剪后的宽度
  5. $height = 480;// 裁剪后的高度
  6. // 裁剪后的图片存放目录
  7. $target = '../source/temp/resize.jpg';
  8. // 裁剪后保存到目标文件夹
  9. if (image_center_crop($source, $width, $height, $target)) {
  10. echo "原图1440*900为:<img src='$source'>";
  11. echo "<hr>";
  12. echo "修改后图片480*480为:<img src='$target'>";
  13. }

运行效果:

原图1440*900为:


修改后图片480*480为:

同理,480*320,、800*600等尺寸的图片只需修改相应参数即可。

附:代码测试中遇到的问题

报错:call an undefined function exif_imagetype()

解决方法:

打开扩展 extension=php_exif.dll

并将extension=php_mbstring.dll ,放到extension=php_exif.dll前边

另:boolval()函数为PHP5.5版本以上才能使用的函数,本文测试代码中为兼容低版本,使用如下语句代替:


  1. return file_exists($target)?true:false;

以上就是php中自定义图像居中裁剪函数实现的代码案例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行