您好,欢迎访问一九零五行业门户网

PHP用GD库生成高质量的缩略图片

php用gd库生成高质量的缩略图片,php一般情况下生成的缩略图都比较不理想。今天试用php,gd库来生成缩略图。虽然并不100%完美。可是也应该可以满足缩略图的要求了。
以下是php源代码(resizeimage.php)。
代码如下:
$maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if($resizewidth && $resizeheight){
if($widthratio $ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($resizewidth){
$ratio = $widthratio;
}elseif($resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists(imagecopyresampled)){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg ($newim,$name . .jpg);
imagedestroy ($newim);
}else{
imagejpeg ($im,$name . .jpg);
}
}
if($_files['image']['size']){
if($_files['image']['type'] == image/pjpeg){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == image/x-png){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == image/gif){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if($im){
if(file_exists($filename.jpg)){
unlink($filename.jpg);
}
resizeimage($im,$resizewidth,$resizeheight,$filename);
imagedestroy ($im);
}
}
?>
以下是测试代码(demo.php)
代码如下:
其它类似信息

推荐信息