Using the GD library to implement PHP server-side image cropping and generating thumbnails

Source: Internet
Author: User
Tags final php server

The general user uploads the avatar, will let the user cut out the picture by oneself. So how does PHP implement this function? The Imagecopyresampled method of the main use of the GD library in the clipping picture in PHP

Cropping example: final cropped Picture:   Inside the dotted box is the picture to be cropped out, eventually saved into a 100-wide picture. The code is as follows:     code is as follows: $src _path = ' 1.jpg '; Create an instance of the source diagram $src = imagecreatefromstring (file_get_contents ($src _path));  //Cut the coordinates $x = 100 of the point at the upper left corner of the open area; $y = 12; The width and height of the cropping area $width = 200; $height = 200; Finally saved into the picture width and height, and the source to equal proportions, otherwise it will be deformed $final _width = 100; $final _height = Round ($final _width * $height/$width);  //The cropped area is copied to the new picture and scaled or pulled up according to the width and height of the source and target $new _image = Imagecreatetruecolor ($final _width, $final _height); Imagecopyresampled ($new _image, $src, 0, 0, $x, $y, $final _width, $final _height, $width, $height);  //Output Picture header (' content-type:image/jpeg '); Imagejpeg ($new _image);   Imagedestroy ($SRC); Imagedestroy ($new _image);       In fact, if the coordinates are (0,0), the width of the clipping region is consistent with the height of the source graph, then the function of the thumbnail is generated.   Summary   Here is just a list of examples of PHP cropping pictures, which are part of the service side function. If the client has the need, recommend a jquery plug-in imageareaselect, the compatibility is very good.  

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.