php实现图片裁剪
时间:2019-11-18 23:32:38|栏目:|点击: 次
我们经常要用到图片的裁剪和压缩,例如在生成小程序朋友圈分享图的时候,需要裁剪原缩略图,本文举例说明php如何实现图片 裁剪 ,可以直接使用。
用到的图片处理函数
php图像处理函数imagecopyresampled
语法
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )参数
dst_image 目标图象连接资源。
src_image 源图象连接资源。
dst_x 目标 X 坐标点。
dst_y 目标 Y 坐标点。
src_x 源的 X 坐标点。
src_y 源的 Y 坐标点。
dst_w 目标宽度。
dst_h 目标高度。
src_w 源图象的宽度。
src_h 源图象的高度。
function tailoringImg($file_path,$save_width,$start_spot_x,$start_spot_y,$width,$height,$display=1) { if(file_exists($file_path) && is_readable($file_path)) { //从字符串中的图像流新建一图像 $src = imagecreatefromstring(file_get_contents($file_path)); //保存图片的高 $save_height = round($save_width*$height/$width); //根据要保存的宽和高创建图片 $new_image = imagecreatetruecolor($save_width, $save_height); //生成最后的图片 imagecopyresampled($new_image, $src, 0, 0, $start_spot_x, $start_spot_y, $save_width, $save_height, $width, $height); //直接展示 if($display) { header('Content-Type: image/jpeg'); imagejpeg($new_image); imagedestroy($src); imagedestroy($new_image); } } else { echo '文件查看失败';//记录日志 } } //将图片进行裁剪 $file_path = './cs.jpg'; $save_width = 200; $start_spot_x = 10; $start_spot_y = 15; $width = 100; $height = 100; tailoringImg($file_path,$save_width,$start_spot_x,$start_spot_y,$width,$height,1);