当前位置:Gxlcms > PHP教程 > php调整图像尺寸的代码

php调整图像尺寸的代码

时间:2021-07-01 10:21:17 帮助过:3人阅读

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

人气教程排行