php获取远程图片url生成缩略图的方法
getimg.php
<?php
/**
*
*函数:调整图片尺寸或生成缩略图
*返回:True/False
*参数:
* $Image 需要调整的图片(含路径)
* $Dw=450 调整时最大宽度;缩略图时的绝对宽度
* $Dh=450 调整时最大高度;缩略图时的绝对高度
* $Type=1 1,调整尺寸; 2,生成缩略图
*/
$phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');
function compressImg($Image,$Dw,$Dh,$Type){
IF(!file_exists($Image)){
return false;
}
// 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
// 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
IF($Type!=1){
copy($Image,str_replace(".","_x.",$Image));
$Image=str_replace(".","_x.",$Image);
}
// 取得文件的类型,根据不同的类型建立不同的对象
$ImgInfo=getimagesize($Image);
Switch($ImgInfo[2]){
case 1:
$Img =@imagecreatefromgif($Image);
break;
case 2:
$Img =@imagecreatefromjpeg($Image);
Break;
case 3:
$Img =@imagecreatefrompng($Image);
break;
}
// 如果对象没有创建成功,则说明非图片文件
IF(Empty($Img)){
// 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
IF($Type!=1){
unlink($Image);
}
return false;
}
// 如果是执行调整尺寸操作则
IF($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width = $w;
$height = $h;
IF($width>$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;
}
?>
调用:demo.php
<?php
require_once( 'getimg.php' );
$imgPath = 'http://xxx.com/aa.jpg';//远程URL 地址
$tempPath = 'adsdf.jpg';//保存图片路径
if(is_file($tempPath)){
unlink($tempPath);
}
$bigImg=GrabImage($imgPath, $tempPath);
var_dump(compressImg($bigImg,100,100,1));
?>
版权与免责声明:
凡注明稿件来源的内容均为转载稿或由网友用户注册发布,本网转载出于传递更多信息的目的;如转载稿涉及版权问题,请作者联系我们,同时对于用户评论等信息,本网并不意味着赞同其观点或证实其内容的真实性;

![英雄棋士团(预下载)?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/shouyoupic/yingxiongqishituanyuxiazai.jpg)
![美食小当家?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/shouyoupic/meishixiaodangjia.png)
![2047?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/shouyoupic/2047.jpg)
![荣誉指挥官(预下载)?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/shouyoupic/rongyuzhihuiguanyuxiazai.png)
![繁荣美食市场物语?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/shouyoupic/fanrongmeishishichangwuyu.jpg)
![夸克浏览器 v4.2.1.138 好用的手机浏览器?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/appimg/202007/kuakezuolanqi.jpg)
![移动办公软件 OfficeSuite Premium v10.18.28716 内购解锁版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/appimg/202007/yidongbangongruanjian.jpg)
![乐秀视频编辑器 VideoShow v8.8.4 内购解锁版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/appimg/202007/lexiushipinbianjiqi.png)
![X 浏览器 v3.3.9 一款小巧的安卓浏览器?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/appimg/202007/x.jpg)
![安卓密码管理软件 Enpass v6.4.5.368 内购解锁版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/appimg/202007/anzhuomimaguanliruanjian.jpg)
![差分复制同步 FastCopy-M v3.6.3.51 绿色便携版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/softimg/FastCopy3.png)
![多标签页拓展 Clover v3.5.2 Build 19809 精简绿色版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/softimg/Clover.png)
![文件重命名 Advanced Renamer v3.85 Lite 绿色便携版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/softimg/Advanced_Renamer.png)
![网络防火监控 GlassWire Elite v2.1.166 绿色便携版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/softimg/GlassWire.png)
![影音播放器 Daum Potplayer v1.7.20538 美化便携版?=$bqr['banben']?>](http://shouyouimg.cnzzla.com/d/file/softimg/PotPlayer.png)