经典的php缩略图生成程式,基于gd库,可指定生成路径及生成目标的宽高细节 使用方法: 在支持gd库的php环境中,将以下代码另存为resize.php测试
经典的php缩略图生成程式,基于gd库,可指定生成路径及生成目标的宽高细节
使用方法: 在支持gd库的php环境中,将以下代码另存为resize.php测试
复制代码 代码如下:
$filename=image_name;
// 生成图片的宽度
$resizewidth=400;
// 生成图片的高度
$resizeheight=400;
//生成图片的路径
$uploaddir=c:/winnt/temp;
function resizeimage($im,$maxwidth,$maxheight,$name){
global $uploaddir;
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $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,$uploaddir.$name . .jpg);
imagedestroy ($newim);
}else{
imagejpeg ($im,$uploaddir.$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);
}
}
?>
>
http://www.bkjia.com/phpjc/317857.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/317857.htmltecharticle经典的php缩略图生成程式,基于gd库,可指定生成路径及生成目标的宽高细节使用方法:在支持gd库的php环境中,将以下代码另存为resize.php测试 经...
