zoukankan      html  css  js  c++  java
  • PHP获取远程图片并调整图像大小

    1. <?php  
    2. /** 
    3. * 
    4. *函数:调整图片尺寸或生成缩略图 
    5. *修改:2013-2-15 
    6. *返回:True/False 
    7. *参数: 
    8. *   $Image   需要调整的图片(含路径) 
    9. *   $Dw=450   调整时最大宽度;缩略图时的绝对宽度 
    10. *   $Dh=450   调整时最大高度;缩略图时的绝对高度 
    11. *   $Type=1   1,调整尺寸; 2,生成缩略图 
    12. */  
    13.   
    14. $phtypes=array('img/gif''img/jpg''img/jpeg''img/bmp''img/pjpeg''img/x-png');  
    15.   
    16. function compressImg($Image,$Dw,$Dh,$Type){  
    17.     echo $Image;  
    18.     IF(!file_exists($Image)){  
    19.         echo "不存在图片";  
    20.         return false;  
    21.     }  
    22.     echo "存在图片";  
    23.     // 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)  
    24.     // 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)  
    25.     IF($Type!=1){  
    26.         copy($Image,str_replace(".","_x.",$Image));  
    27.         $Image=str_replace(".","_x.",$Image);  
    28.     }  
    29.     // 取得文件的类型,根据不同的类型建立不同的对象  
    30.     $ImgInfo=getimagesize($Image);  
    31.     Switch($ImgInfo[2]){  
    32.         case 1:  
    33.             $Img =@imagecreatefromgif($Image);  
    34.             break;  
    35.         case 2:  
    36.             $Img =@imagecreatefromjpeg($Image);  
    37.             Break;  
    38.         case 3:  
    39.             $Img =@imagecreatefrompng($Image);  
    40.             break;  
    41.     }  
    42.     // 如果对象没有创建成功,则说明非图片文件  
    43.     IF(Empty($Img)){  
    44.         // 如果是生成缩略图的时候出错,则需要删掉已经复制的文件  
    45.         IF($Type!=1){  
    46.             unlink($Image);  
    47.         }  
    48.         return false;  
    49.     }  
    50.     // 如果是执行调整尺寸操作则  
    51.     IF($Type==1){  
    52.         $w=ImagesX($Img);  
    53.         $h=ImagesY($Img);  
    54.         $width = $w;  
    55.         $height = $h;  
    56.         IF($width>$Dw){  
    57.             $Par=$Dw/$width;  
    58.             $width=$Dw;  
    59.             $height=$height*$Par;  
    60.             IF($height>$Dh){  
    61.                 $Par=$Dh/$height;  
    62.                 $height=$Dh;  
    63.                 $width=$width*$Par;  
    64.             }  
    65.         } ElseIF($height>$Dh) {  
    66.             $Par=$Dh/$height;  
    67.             $height=$Dh;  
    68.             $width=$width*$Par;  
    69.             IF($width>$Dw){  
    70.                 $Par=$Dw/$width;  
    71.                 $width=$Dw;  
    72.                 $height=$height*$Par;  
    73.             }  
    74.         } Else {  
    75.             $width=$width;  
    76.             $height=$height;  
    77.         }  
    78.         $nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布  
    79.         ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小  
    80.         ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件  
    81.         return true;  
    82.     } Else {// 如果是执行生成缩略图操作则  
    83.         $w=ImagesX($Img);  
    84.         $h=ImagesY($Img);  
    85.         $width = $w;  
    86.         $height = $h;  
    87.         $nImg =ImageCreateTrueColor($Dw,$Dh);  
    88.         IF($h/$w>$Dh/$Dw){// 高比较大  
    89.             $width=$Dw;  
    90.             $height=$h*$Dw/$w;  
    91.             $IntNH=$height-$Dh;  
    92.             ImageCopyReSampled($nImg$Img, 0, -$IntNH/1.8, 0, 0, $Dw$height$w$h);  
    93.         } Else {// 宽比较大  
    94.             $height=$Dh;  
    95.             $width=$w*$Dh/$h;  
    96.             $IntNW=$width-$Dw;  
    97.             ImageCopyReSampled($nImg$Img,-$IntNW/1.8,0,0,0, $width$Dh$w$h);  
    98.         }  
    99.         ImageJpeg($nImg,$Image);  
    100.         return true;  
    101.     }  
    102. };  
    103. ?>  
    104.   
    105. <?php  
    106. //网络图片路径  
    107. $imgPath = 'http://www.lesohome.com/phone/compress-img/251139474ba926db3d7850.jpg';  
    108. //$imgPath = "http://www.lesohome.com/userfiles/image/20111125/251139474ba926db3d7850.jpg";  
    109. $tempPath = str_replace('http://www.lesohome.com/'''$imgPath);//替换换行字符  
    110. $name = strrchr($tempPath"/");  
    111. $path = str_replace($name''$tempPath);//替换换行字符  
    112.   
    113. /** 
    114.  *根据路径path建立多级目录 
    115.  *$dir目标目录 $mode权限,0700表示最高权限 
    116. */  
    117. function  makedir( $dir , $mode = "0700" ) {  
    118.     if(strpos($dir , "/" )){  
    119.         $dir_path = "" ;  
    120.         $dir_info = explode("/" , $dir );  
    121.         foreach($dir_info as $key => $value){  
    122.             $dir_path .= $value ;  
    123.             if (!file_exists($dir_path)){  
    124.                 @mkdir($dir_path$modeor die ("建立文件夹时失败了" );  
    125.                 @chmod($dir_path$mode);  
    126.             } else {  
    127.                 $dir_path .= "/" ;  
    128.                 continue ;  
    129.             }  
    130.             $dir_path .= "/" ;  
    131.         }  
    132.         return $dir_path ;  
    133.     } else {  
    134.         @mkdir($dir$modeor die"建立失败了,请检查权限" );  
    135.         @chmod($dir$mode);  
    136.         return $dir ;  
    137.     }  
    138. //end makedir  
    139. makedir($path);  
    140.   
    141. /** 
    142.  *根据url获取服务器上的图片 
    143.  *$url服务器上图片路径 $filename文件名 
    144. */  
    145. function GrabImage($url,$filename="") {  
    146.     if($url==""return false;  
    147.     if($filename=="") {  
    148.         $ext=strrchr($url,".");  
    149.         if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")  
    150.             return false;  
    151.         $filename=date("YmdHis").$ext;  
    152.     }  
    153.     ob_start();   
    154.     readfile($url);   
    155.     $img = ob_get_contents();   
    156.     ob_end_clean();  
    157.     $size = strlen($img);   
    158.   
    159.     $fp2=@fopen($filename"a");  
    160.     fwrite($fp2,$img);  
    161.     fclose($fp2);  
    162.     return $filename;  
    163. }  
    164. ?>  
    165.   
    166. <html>  
    167. <body>  
    168. 允许上传的文件类型为:<?=implode(', ',$phtypes)?><br/>  
    169. <input type="button" id="getImg" name="getImg" size="10" value="获取图片并保存" onclick="alert('12333');" />  
    170.   
    171. <?php  
    172.   
    173. echo $path."<br>";  
    174. /**/  
    175. $bigImg=GrabImage($imgPath$tempPath);  
    176. if($bigImg){  
    177.     echo '<img src="'.$bigImg.'"><br>';  
    178. else {  
    179.     echo "false";  
    180. }   
    181.   
    182. compressImg($bigImg,100,80,1);  
    183. ?>  
    184. </body>  
    185. </html>  
  • 相关阅读:
    ASP.NET版本的Kindeditor插件的使用
    股票交易时间
    vs2010 安装mvc3
    JDK,JRE,JVM区别与联系
    使用 AngularJS 从零构建大型应用
    JavaScript奇技淫巧45招
    知道这20个正则表达式,能让你少写1,000行代码
    $timeout, $interval
    js页面loading加载
    jq倒计时
  • 原文地址:https://www.cnblogs.com/hechunhua/p/3409995.html
Copyright © 2011-2022 走看看