PHP for image clipping and generate thumbnail program source code

Source: Internet
Author: User
Tags imagejpeg

We often encounter the image of the cutting action, the following code is the source of clipping

The processing methods are:

1. When the width or height of the original image is smaller than the specified size, only the same as the thumbnail processing,

2. When the original image width and height are larger than the specified size, first of equal than the abbreviated processing, and then calculate the center position for cropping

/* * $o _photo Original path * $d _photo after processing picture path * $width definition Width * $height Define High * Call method Cutphoto ("t Est.jpg "," temp.jpg ", 256,146);    */function Cutphoto ($o _photo, $d _photo, $width, $height) {$temp _img = imagecreatefromjpeg ($o _photo);                                $o _width = imagesx ($temp _img);                                Obtain the original width $o _height = Imagesy ($temp _img); Obtain the original image Height//Judgment processing method if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compression $newwidth = $o _        Width        $newheight = $o _height;            if ($o _width > $width) {$newwidth = $width;        $newheight = $o _height * $width/$o _width;            if ($newheight > $height) {$newwidth = $newwidth * $height/$newheight;        $newheight = $height;        }//thumbnail image $new _img = Imagecreatetruecolor ($newwidth, $newheight); Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _heighT);        Imagejpeg ($new _img, $d _photo);    Imagedestroy ($new _img); } else {//Huantuquan and height are larger than the specified size, and clipped if after compression (            $o _height * $width/$o _width > $height) {//Make sure width is the same as specified, if height is greater than specified, OK $newwidth = $width;            $newheight = $o _height * $width/$o _width;            $x = 0;        $y = ($newheight-$height)/2; } else {//otherwise determine the height and the same as the rule, Width adaptive $new            width = $o _width * $height/$o _height;            $newheight = $height;            $x = ($newwidth-$width)/2;        $y = 0;        }//thumbnail image $new _img = Imagecreatetruecolor ($newwidth, $newheight);        Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);        Imagejpeg ($new _img, $d _photo);        Imagedestroy ($new _img); $temp _img = imagecreatefromjpeg ($d _phoTo);                                $o _width = imagesx ($temp _img);                                Get thumbnail width $o _height = Imagesy ($temp _img);        Get thumbnail height//crop picture $new _IMGX = Imagecreatetruecolor ($width, $height);        Imagecopyresampled ($new _imgx, $temp _img, 0, 0, $x, $y, $width, $height, $width, $height);        Imagejpeg ($new _imgx, $d _photo);    Imagedestroy ($new _IMGX); }}cutphoto ("HTTP://TP3.SINAIMG.CN/1700691210/180/1280120708/1", "temp1.jpg", +, +) Cutphoto ("http://hhcf.tk/ Img/avatar_big/1318072877.jpg "," temp2.jpg ", 100, 100);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.