This time to bring you PHP GD library to achieve the service-side picture clipping and thumbnail preview function, PHP GD library to achieve the service-side picture clipping and thumbnail preview features of the note, the following is a practical case, to see.
example, the Imagecopyresampled method of the GD library is used to cut the image in PHP.
Examples of cropping pictures:
The final cropped image:
Inside the dashed box is the picture to be cropped and finally saved to 100 wide.
<?php$src_path = ' 1.jpg ';//Create an instance of the source graph $src = imagecreatefromstring (file_get_contents ($src _path));//The coordinates of the point in the upper-left corner of the crop open area $x = $y = 12;//Clipping area width and height $width = n, $height = 200;//finally save the width and height of the picture, and the source to equal proportions, otherwise it will deform $final_width =; $final _height = Round ($ Final_width * $height/$width);//Copy the cropped area onto the new picture and scale according to the width of the source and destination, or pull $new_image = Imagecreatetruecolor ($final _width, $ Final_height); imagecopyresampled ($new _image, $src, 0, 0, $x, $y, $final _width, $final _height, $width, $height);// Output image header (' content-type:image/jpeg '); imagejpeg ($new _image); Imagedestroy ($SRC); Imagedestroy ($new _image);
In fact, if the coordinates are (0,0), the width and height of the cropping area are consistent with the width of the source graph, then it is the function of generating thumbnails.
Summarize
Here are just a list of examples of PHP cropped images, which are part of the service-side functionality.
If the client needs, recommend a jquery plug-in imageareaselect, compatibility is very good.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP array functions Shuffle () and Array_rand () random functions using steps
PHP Array Lookup function Use method summary