Home >Backend Development >PHP Tutorial >PHP image processing: Example of cropping images using imagecopyresampled function

PHP image processing: Example of cropping images using imagecopyresampled function

高洛峰
高洛峰Original
2016-12-26 12:58:311255browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn