-
- $src_path = '1.jpg';
- //Create source image instance
- $src = imagecreatefromstring(file_get_contents($src_path));
-
- //The coordinates of the point in the upper left corner of the cropped area
- $x = 100;
- $y = 12;
- //The 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 Transform
- $final_width = 100;
- $final_height = round($final_width * $height / $width);
-
- //Copy the cropped area to the new picture, 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);
-
Copy code
Cropped 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.
The above only lists examples of cropping images in PHP, which are server-side functions.
If the client needs it, you can use the jquery plug-in imageAreaSelect, which has good compatibility.
|