一聚教程网:一个值得你收藏的教程网站

热门教程

一款php按比例生成缩略图代码

时间:2022-06-24 21:10:27 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

function resizeimage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imagetype) = getimagesize($image);
 $imagetype = image_type_to_mime_type($imagetype);
 $newimagewidth = ceil($width * $scale);
 $newimageheight = ceil($height * $scale);
 $newimage = imagecreatetruecolor($newimagewidth,$newimageheight);
 switch($imagetype) {
  case "image/gif":
   $source=imagecreatefromgif($image);
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image);
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image);
   break;
   }
 imagecopyresampled($newimage,$source,0,0,0,0,$newimagewidth,$newimageheight,$width,$height);
 
 switch($imagetype) {
  case "image/gif":
     imagegif($newimage,$image);
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newimage,$image,90);
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newimage,$image); 
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
?>

热门栏目