PHP图片处理之使用imagecopyresampled函数裁剪图片例子_PHP

WBOY
Release: 2016-05-31 19:27:38
Original
1013 people have browsed it

图片裁剪是指在一个大的背景图片中裁剪出一张指定区域的图片,常见的应用是在用户设置个人头像时,可以从上传的图片中,裁剪出一个合适的区域作为自己的个人头像图片。图像裁剪和图片缩放的相似,所以也是借助imagecopyresampled()函数去实现这个功能。同样也是以JPEG图片格式为例,声明一个图像裁剪函数cut(),代码如下所示:

代码如下:


    //在一个大的背景图片中裁剪出指定区域的图片,以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);
?>

Related labels:
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!