Image cropping refers to cutting out a picture of a designated area from a large background picture. A common application is when the user sets a personal avatar, a suitable area can be cut out from the uploaded picture as his or her own. Profile picture. Image cropping is similar to image scaling, so the imagecopyresampled() function is also used to implement this function. Also taking the JPEG image format as an example, declare an image cropping function cut(). The code is as follows:
<?php //在一个大的背景图片中裁剪出指定区域的图片,以jpeg图片格式为例 function cut($filename,$x,$y,$width,$height){ $back = imagecreatetruecolor($width, $height); //创建一个可以保存裁剪后图片的资源 $cutimg = imagecreatetruecolor($width, $height); //使用imagecopyresampled()函数对图片进行裁剪 imagecopyresampled($cutimg,$back,0,0,$x,$y,$width,$height,$width,$height); //保存裁剪 后的图片,如果不想覆盖图片可以为裁剪后的图片加上前缀 imagejpeg($cutimg,$filename); imagedestroy($cutimg); imagedestroy($back); } cut("brophp.jpg", 50, 50, 200, 200); ?>
For more PHP image processing examples of cropping images using the imagecopyresampled function, please pay attention to the PHP Chinese website for related articles!