Home >Backend Development >PHP Tutorial >PHP code to adjust image size

PHP code to adjust image size

WBOY
WBOYOriginal
2016-07-25 09:03:11914browse
  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. }
  33. ?>

复制代码


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