Home >Backend Development >PHP Tutorial >php generate thumbnail code

php generate thumbnail code

WBOY
WBOYOriginal
2016-07-25 08:45:05812browse
  1. /**********************
  2. *@filename - path to the image
  3. *@tmpname - temporary path to thumbnail
  4. *@xmax - max width
  5. *@ymax - max height
  6. */
  7. function resize_image($filename, $tmpname, $xmax, $ymax)
  8. {
  9. $ext = explode(".", $filename);
  10. $ext = $ext[count($ext)-1];
  11. if($ext == "jpg" || $ext == "jpeg")
  12. $im = imagecreatefromjpeg($tmpname);
  13. elseif($ext == "png")
  14. $im = imagecreatefrompng($tmpname);
  15. elseif($ext == "gif")
  16. $im = imagecreatefromgif($tmpname);
  17. $x = imagesx($im);
  18. $y = imagesy($im);
  19. if($x <= $xmax && $y <= $ymax)
  20. return $im;
  21. if($x >= $y) {
  22. $newx = $xmax;
  23. $newy = $newx * $y / $x;
  24. }
  25. else {
  26. $newy = $ymax;
  27. $newx = $x / $y * $newy;
  28. }
  29. $im2 = imagecreatetruecolor($newx, $newy);
  30. imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
  31. return $im2;
  32. }
复制代码

php


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
Previous article:API monitoring practicesNext article:API monitoring practices