php>依据远程图片URL生成缩略图
php > 根据远程图片URL 生成缩略图
$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
} ElseIF($height>$Dh) {
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
} Else {
$width=$width;
$height=$height;
}
$nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
return true;
} Else {// 如果是执行生成缩略图操作则
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
$nImg =ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){// 高比较大
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
} Else {// 宽比较大
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
}
ImageJpeg($nImg,$Image);
return true;
}
};
/**
*根据url获取服务器上的图片
*$url服务器上图片路径 $filename文件名
*/
function GrabImage($url,$filename="") {
if($url=="") return false;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
return false;
$filename=date("YmdHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
?>