Home > Backend Development > PHP Tutorial > How to crop pictures in php? php image cutting code

How to crop pictures in php? php image cutting code

WBOY
Release: 2016-07-25 08:52:44
Original
1105 people have browsed it
  1. $src_path = '1.jpg';
  2. //Create source image instance
  3. $src = imagecreatefromstring(file_get_contents($src_path));
  4. //The coordinates of the point in the upper left corner of the cropped area
  5. $x = 100;
  6. $y = 12;
  7. //The width and height of the cropped area
  8. $width = 200;
  9. $height = 200;
  10. //The width and height of the final saved image must be in the same proportion as the source, otherwise it will Transform
  11. $final_width = 100;
  12. $final_height = round($final_width * $height / $width);
  13. //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
  14. $new_image = imagecreatetruecolor($final_width, $final_height);
  15. imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
  16. //Output image
  17. header('Content-Type: image/jpeg');
  18. imagejpeg($new_image);
  19. imagedestroy($src);
  20. imagedestroy($new_image);
Copy code

Cropped image: How to crop pictures in php? php image cutting code

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.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template