Home >Backend Development >PHP Tutorial >Use gd library to implement PHP server image cropping and thumbnail generation function sharing_PHP tutorial

Use gd library to implement PHP server image cropping and thumbnail generation function sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:25824browse

Crop example:

Use gd library to implement PHP server image cropping and thumbnail generation function sharing_PHP tutorial

The final cropped picture:

Use gd library to implement PHP server image cropping and thumbnail generation function sharing_PHP tutorial

The dotted box contains the image to be cropped, and will eventually be saved as a 100-wide image. The code is as follows:

Copy code The code is as follows:

$src_path = '1.jpg';
//Create the source image Example
$src = imagecreatefromstring(file_get_contents($src_path));

//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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621662.htmlTechArticleCropping example: The final cropped picture: The dotted box is the picture to be cropped, and it is finally saved as 100 wide picture. The code is as follows: Copy the code The code is as follows: $src_path...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn