Sample code for filling white edges in php thumbnails

WBOY
Release: 2016-07-25 08:54:59
Original
1018 people have browsed it
  1. //The path of the source image, which can be a local file or a remote image

  2. $src_path = '1.jpg';
  3. //The width of the final saved image
  4. $width = 160 ;
  5. //The height of the final saved image
  6. $height = 120;

  7. //Source image object

  8. $src_image = imagecreatefromstring(file_get_contents($src_path));
  9. $src_width = imagesx($ src_image);
  10. $src_height = imagesy($src_image);

  11. //Generate thumbnails of equal proportions

  12. $tmp_image_width = 0;
  13. $tmp_image_height = 0;
  14. if ($src_width / $ src_height >= $width / $height) {
  15. $tmp_image_width = $width;
  16. $tmp_image_height = round($tmp_image_width * $src_height / $src_width);
  17. } else {
  18. $tmp_image_height = $height;
  19. $tmp_image_width = round ($tmp_image_height * $src_width / $src_height);
  20. }

  21. $tmpImage = imagecreatetruecolor($tmp_image_width, $tmp_image_height);

  22. imagecopyresampled($tmpImage, $src_image, 0, 0, 0, 0, $tmp_image_width, $tmp_image_height, $src_width, $src_height);

  23. //Add white edges

  24. $final_image = imagecreatetruecolor($width, $height);
  25. $color = imagecolorallocate($ final_image, 255, 255, 255);
  26. imagefill($final_image, 0, 0, $color);

  27. $x = round(($width - $tmp_image_width) / 2);

  28. $y = round(($height - $tmp_image_height) / 2);

  29. imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height) ;

  30. //Output image

  31. header('Content-Type: image/jpeg');
  32. imagejpeg($final_image);

Copy code


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