加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

PHP实现的自定义图像居中裁剪函数示例【测试可用】

发布时间:2021-02-23 07:17:29 所属栏目:编程 来源:网络整理
导读:本篇章节讲解PHP实现的自定义图像居中裁剪函数。供大家参考研究具体如下: 图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域。(imagecopyresampled — 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放

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

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

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

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

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

具体代码:

(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_w / http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_h)); $resize_w = $judge ? (http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_w * http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_h) / http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_h : http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_w; $resize_h = !$judge ? (http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_h * http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_w) / http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_w : http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_h; $start_x = $judge ? ($resize_w - http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_w) / 2 : 0; $start_y = !$judge ? ($resize_h - http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_h) / 2 : 0; /* 绘制居中缩放图像 */ $resize_img = imagecreatetruecolor($resize_w,$resize_h); imagecopyresampled($resize_img,$image,$resize_w,$resize_h,http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_w,http://www.lidatong.com.cn/html/jc/php/2020/1212/$source_h); http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_img = imagecreatetruecolor(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_w,http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_h); imagecopy(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_img,$resize_img,$start_x,$start_y,$resize_h); /* 将图片保存至文件 */ if (!file_exists(dirname(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target))) mkdir(dirname(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target),0777,true); switch (exif_imagetype(http://www.lidatong.com.cn/html/jc/php/2020/1212/$source)) { case IMAGETYPE_JPEG: imagejpeg(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_img,http://www.lidatong.com.cn/html/jc/php/2020/1212/$target); break; case IMAGETYPE_PNG: imagepng(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_img,http://www.lidatong.com.cn/html/jc/php/2020/1212/$target); break; case IMAGETYPE_GIF: imagegif(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target_img,http://www.lidatong.com.cn/html/jc/php/2020/1212/$target); break; } // return boolval(file_exists(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target));//PHP5.5以上可用boolval()函数获取返回的布尔值 return file_exists(http://www.lidatong.com.cn/html/jc/php/2020/1212/$target)?true:false;//兼容低版本PHP写法 }
"; echo "
"; echo "修改后图片480*480为:PHP实现的自定义图像居中裁剪函数示例【测试可用】"; }

运行效果:

原图1440*900为:PHP实现的自定义图像居中裁剪函数示例【测试可用】


修改后图片480*480为:

PHP实现的自定义图像居中裁剪函数示例【测试可用】

同理,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版本以上才能使用的函数,本文测试代码中为兼容低版本,使用如下语句代替:

PS:这里再为大家推荐几款相关的图片在线工具供大家参考使用:

在线图片格式转换(jpg/bmp/gif/png)工具:

在线PS图像处理工具:

ICO图标在线生成工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读