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 );......