How to solve the problem of cropping a specified area in an image in php?-php Tutorial

Source: Internet
Author: User
Tags imagecopy
In the php cropping image, if an error occurs after an image is cropped in the yellow box area to be cropped in the specified position area, the image is truncated based on the coordinate specified by XY to specify the width and height of the image, an error occurred while using the image function. the starting point of the xy axis was correct, but it reduced all the remaining areas and then added a black edge to the insufficiency, resulting in deformation of the image. List ($ width, $ heght, $ type, $ attr) getimagesize ($ imgurl); $ thisim php crop the specified location area in the image


Images in the yellow box area to be cropped




Image with an error after cropping


According to the specified coordinates of x y, the width and height of the image are captured. now, an error occurs with the function of the image. The start point of the x Y axis is correct, but it reduces all the remaining areas, then, the image is distorted by adding black borders.

List ($ width, $ heght, $ type, $ attr) = getimagesize ($ imgurl );
$ Thisimage = imagecreatetruecolor ($ new_w, $ new_h );
$ Background = imagecolorallocate ($ thisimage, 255,255,255 );
Imagefilledrectangle ($ thisimage, 0, 0, $ new_w, $ new_h, $ background );
$ Oldimg = imagecreatefromjpeg ($ imgurl );
Imagecopyresampled ($ thisimage, $ oldimg, 0, 0, $ src_x, $ src_y, $ new_w, $ new_h, $ width, $ heght );
Header ("Content-type: image/jpeg ");
Imagejpeg ($ thisimage );


Could you tell me how to take this picture to get the picture you want in the yellow box?

------ Solution --------------------
$ Thisimage = imagecreatetruecolor ($ new_w, $ new_h); // $ new_w, $ new_h indicates the width and height of the cropped image.
$ Oldimg = imagecreatefromjpeg ($ imgurl); // load the original image
Imagecopy ($ thisimage, $ oldimg, 0, 0, $ src_x, $ src_y, $ new_w, $ new_h); // $ src_y, $ new_w is the coordinate of the upper left corner of the cropping area in the source image.

------ Solution --------------------
The width of your image is set incorrectly.
There is no problem with cutting.
------ Solution --------------------
Is the parameter incorrect?
------ Solution --------------------
Discussion

$ Thisimage = imagecreatetruecolor ($ new_w, $ new_h); // $ new_w, $ new_h indicates the width and height of the cropped image.
$ Oldimg = imagecreatefromjpeg ($ imgurl); // load the original image
Imagecopy ($ thisimage, $ oldimg, 0, 0, $ src_x, $ src_y, $ new_w, $ new_h );......

Related Article

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.