Home >Backend Development >PHP Tutorial >Use gd library to implement PHP server image cropping and thumbnail generation function sharing_PHP tutorial
Crop example:
The final cropped picture:
The dotted box contains the image to be cropped, and will eventually be saved as a 100-wide image. The code is as follows:
//Coordinates of the upper left corner point of the cropped area
$x = 100;
$y = 12;
//Width and height of the cropped area
$width = 200;
$height = 200;
//The width and height of the final saved image must be in the same proportion as the source, otherwise it will be deformed
$final_width = 100;
$final_height = round($final_width * $ height / $width);
//Copy the cropped area to the new image, and scale or pull it 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 image
header('Content-Type: image/jpeg');
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);
In fact, if the coordinates are (0,0) and the width and height of the cropped area are consistent with the width and height of the source image, then it is the function of generating thumbnails.
Summary
Only examples of php cropping images are listed here, which are server-side functions. If the client needs it, I recommend a jquery plug-in imageAreaSelect, which has very good compatibility.