手机版

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

时间:2021-09-04 来源:互联网 编辑:宝哥软件园 浏览:

本文实例讲述了服务器端编程语言(专业超文本预处理器的缩写)实现的自定义图像居中裁剪函数。分享给大家供大家参考,具体如下:

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

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

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

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

具体代码:

//==================缩放裁剪函数====================/** * 居中裁剪图片* @param string $source [原图路径] * @param int $width [设置宽度] * @param int $height [设置高度] * @param string $target [目标路径]* @返回布尔[裁剪结果]*/function image _ center _ crop($ source,$width,$height,$target){ if(!file_exists($source))返回false/*根据类型载入图像*/switch(EXIF _ IMAGETYPE($ source)){ case IMAGETYPE _ JPEG : $ image=imagecreatefrom JPEG($ source);打破;case IMAGETYPE _ png : $ image=imagecreatefrompng($ source);打破;case IMAGETYPE _ gif : $ image=imagecreatefrom gif($ source);打破;} if(!isset($image))返回false/*获取图像尺寸信息*/$ target _ w=$ width;$ target _ h=$ height $ source _ w=imagesx($ image);$ source _ h=imagesy($ image);/* 计算裁剪宽度和高度*/$ judge=($ source _ w/$ source _ h)($ target _ w/$ target _ h));$resize_w=$judge?($ source _ w * $ target _ h)/$ source _ h : $ target _ w;$resize_h=!$法官?($ source _ h * $ target _ w)/$ source _ w : $ target _ h;$start_x=$judge?($ resize _ w-$ target _ w)/2 : 0;$start_y=!$法官?($ resize _ h-$ target _ h)/2 : 0;/* 绘制居中缩放图像*/$ resize _ img=imagecreatetrue color($ resize _ w,$ resize _ h);imagecopyresholded($ resize _ img,$image,0,0,0,$resize_w,$resize_h,$source_w,$ source _ h);$ target _ img=imagecreatetrue color($ target _ w,$ target _ h);imagecopy($target_img,$resize_img,0,0,$start_x,$start_y,$resize_w,$ resize _ h);/* 将图片保存至文件*/if(!file _ exists(dirname($ target)))mkdir(dirname($ target),0777,true);switch(EXIF _ IMAGETYPE($ source)){ case IMAGETYPE _ JPEG : image JPEG($ target _ img,$ target);打破;case IMAGETYPE _ png : image png($ target _ img,$ target);打破;case IMAGETYPE _ gif : imagegif($ target _ img,$ target);打破;}//返回boolval(file _ exists($ target));//PHP5.5以上可用boolval()函数获取返回的布尔值return file_exists($target)?true:false//兼容低版本服务器端编程语言(专业超文本预处理器的缩写)写法}//==================函数使用方式====================//原始图片的路径$source='./source/img/middle。jpg ';$ width=480//裁剪后的宽度$ height=480//裁剪后的高度//裁剪后的图片存放目录$target='./source/temp/resize。jpg ';//裁剪后保存到目标文件夹if (image_center_crop($source,$width,$height,$target)) { echo '原图1440*900为:img src=" $ sourceecho ' hr "回声"修改后图片480*480为:img src=' $ target}运行效果:

原图1440*900为

修改后图片480*480为

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

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

报错:调用未定义的函数exif_imagetype()

解决方法:

打开扩展扩展名=php_exif.dll

并将扩展名=php_mbstring.dll,放到扩展名=php_exif.dll前边

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

return file_exists($target)?true :假声:这里再为大家推荐几款相关的图片在线工具供大家参考使用:

在线图片格式转换(jpg/bmp/gif/png)工具:http://工具。JB 51。net/aiddesign/picext

在线PS图像处理工具:http://tools.jb51.net/aideddesign/webps

ICO图标在线生成工具:http://tools.jb51.net/aideddesign/ico_img

更多对PHP相关内容感兴趣的读者可以查看本网站专题:《PHP图形与图片操作技巧汇总》、《PHP网络编程技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》和《PHP基本语法入门教程》

希望本文对PHP编程有所帮助。

版权声明:PHP实现的自定义图像居中裁剪函数示例【测试可用】是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。