function resize($srcImage, $toFile = "", $maxWidth, $maxHeight, $imgQuality = 100)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
// if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1:
$img = imagecreatefromgif($srcImage);
break;
case 2:
$img = imagecreatefromjpeg($srcImage);
break;
case 3:
$img = imagecreatefrompng($srcImage);
break;
default:
$img = imagecreatefrompng($srcImage);
break;
}
$scale = min($maxWidth / $width, $maxHeight / $height); //求出绽放比例
if ($scale < 1) {
$newWidth = floor($scale * $width);
$newHeight = floor($scale * $height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
switch ($type) {
case 1:
break;
case 2:
imagejpeg($newImg, $toFile . ".png", $imgQuality);
break;
case 3:
imagepng($newImg, $toFile . ".png");
break;
default:
imagepng($newImg, $toFile . ".png");
break;
}
imagedestroy($newImg);
}
imagedestroy($img);
}
用法:
<?php
$makedir="./res";
resize($makedir.'/head.png',$makedir."/smallfang",162,162);
//resize('需要压缩图片','压缩后图片 path + 名字','压缩后宽度大小','压缩后高度大小');
?>