?php /* *生成缩略图 *$imgPath(图片路径), $maxWidth(宽), $maxHeight(高), $directOutput = true(是否在页面输出), $quality = 90, $verbose,$imageType(图片类型) * * */ function resizeImg($imgPath, $maxWidth, $maxHeight, $directOutput =
/*
*生成缩略图
*$imgPath(图片路径), $maxWidth(宽), $maxHeight(高), $directOutput = true(是否在页面输出), $quality = 90, $verbose,$imageType(图片类型)
*
*
*/
function resizeImg($imgPath, $maxWidth, $maxHeight, $directOutput = true, $quality = 90, $verbose,$imageType)
{
$size = getimagesize($imgPath);
//print_r($size);exit;
// break and return false if failed to read image infos
if(!$size){
if($verbose && !$directOutput)echo "
Not able to read image infos.
";
return false;
}
// relation: width/height
$relation = $size[0]/$size[1];
// maximal size (if parameter == false, no resizing will be made)
$maxSize = array($maxWidth?$maxWidth:$size[0],$maxHeight?$maxHeight:$size[1]);
// declaring array for new size (initial value = original size)
$newSize = $size;
// width/height relation
$relation = array($size[1]/$size[0], $size[0]/$size[1]);
//print_r($size);
//echo "
";
//print_r($relation);exit;
if(($newSize[0] > $maxWidth))
{
$newSize[0]=$maxSize[0];
$newSize[1]=$newSize[0]*$relation[0];
}
if(($newSize[1] > $maxHeight))
{
$newSize[1]=$maxSize[1];
$newSize[0]=$newSize[1]*$relation[1];
}
// create image
switch($size[2])
{
case 1:
if(function_exists("imagecreatefromgif"))
{
$originalImage = imagecreatefromgif($imgPath);
}else{
if($verbose && !$directOutput)echo "
No GIF support in this php installation, sorry.
";
return false;
}
break;
case 2: $originalImage = imagecreatefromjpeg($imgPath); break;
case 3: $originalImage = imagecreatefrompng($imgPath); break;
default:
if($verbose && !$directOutput)echo "
No valid image type.
";
return false;
}
// create new image
$resizedImage = imagecreatetruecolor($newSize[0], $newSize[1]);
imagecopyresampled($resizedImage, $originalImage,0, 0, 0, 0,$newSize[0], $newSize[1], $size[0], $size[1]);
$rz=$imgPath;
// output or save
if($directOutput)
{
imagejpeg($resizedImage);
}
else
{
$exp=explode(".",$imgPath);
$extension=end($exp);//$exp[count($exp)-1];
$newimage=$imageType.".".$extension;
$rz=preg_replace("//.([a-zA-Z]{3,4})$/",$newimage,$imgPath);
imagejpeg($resizedImage, $rz, $quality);
}
// return true if successfull
return $rz;
} // End function Resize Image
//调用
resizeImg("var/chen.jpg", 125 ,75, false, 100, 0,"_thumb");
?>