PHP image processing: Example of cropping images using imagecopyresampled function

高洛峰
Release: 2023-03-04 09:40:01
Original
1222 people have browsed it

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);
?>
Copy after login

For more PHP image processing examples of cropping images using the imagecopyresampled function, please pay attention to the PHP Chinese website for related articles!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!