[PHP] PHP调整图像尺寸 →→→→→进入此内容的聊天室

来自 , 2019-08-19, 写在 PHP, 查看 154 次.
URL http://www.code666.cn/view/df839716
  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.  
  12.     if($ext == "jpg" || $ext == "jpeg")
  13.         $im = imagecreatefromjpeg($tmpname);
  14.     elseif($ext == "png")
  15.         $im = imagecreatefrompng($tmpname);
  16.     elseif($ext == "gif")
  17.         $im = imagecreatefromgif($tmpname);  
  18.  
  19.     $x = imagesx($im);
  20.     $y = imagesy($im);  
  21.  
  22.     if($x <= $xmax && $y <= $ymax)
  23.         return $im;  
  24.  
  25.     if($x >= $y) {
  26.         $newx = $xmax;
  27.         $newy = $newx * $y / $x;
  28.     }
  29.     else {
  30.         $newy = $ymax;
  31.         $newx = $x / $y * $newy;
  32.     }  
  33.  
  34.     $im2 = imagecreatetruecolor($newx, $newy);
  35.     imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
  36.     return $im2;
  37. }
  38.  

回复 "PHP调整图像尺寸"

这儿你可以回复上面这条便签

captcha