本文章里面image就是你要生成的图片地址,这是一php按比例生成缩略图代码,只要给你图片它就能生成指定大小的图片,并且不变形ekt.
php按比例生成缩略图代码如下:
- 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;
- }
|