blog

有问题或意见请留言,谢谢您的参与使用。

« 李医生按摩器_眼保仪,按摩眼罩,眼护士,眼睛治疗仪,护眼仪

ap100_php保存远程图片到本地的类

ap100_php保存远程图片到本地的类
<?php

//
// function: 获取远程图片并把它保存到本地
//
//
//   确定您有把文件写入本地服务器的权限
//  
//
// 变量说明:
// $url 是远程图片的完整url地址,不能为空。
// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
// 自动生成.

function grabimage($url,$filename="") {
   if($url==""):return false;endif;

   if($filename=="") {
     $ext=strrchr($url,".");
     if($ext!=".gif" && $ext!=".jpg"):return false;endif;
     $filename=date("dmyhis").$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;
}


$img=grabimage("http://news.bbc.co.uk/images/_1978837_detector_ap100.jpg","");
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;   




?>
dedecms中的:
        if(!empty($saveremoteimg))
         {
                 $body = stripslashes($body);
                 $img_array = array();
                 preg_match_all("/(src|src)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isu",$body,$img_array);
                 $img_array = array_unique($img_array[2]);
                 set_time_limit(0);
                 $imgurl = $img_dir."/".strftime("%y%m%d",time());
                 $imgpath = $base_dir.$imgurl;
                 $millisecond = strftime("%h%m%s",time());
                 if(!is_dir($imgpath)) @mkdir($imgpath,0777);
                 foreach($img_array as $key =>$value)
                 {
                         $value = trim($value);
                         $get_file = @file_get_contents($value);
                         $rndfilename = $imgpath."/".$millisecond.$key.".".substr($value,-3,3);
                         $fileurl = $imgurl."/".$millisecond.$key.".".substr($value,-3,3);
                         if($get_file)
                         {
                                 $fp = @fopen($rndfilename,"w");
                                 @fwrite($fp,$get_file);
                                 @fclose($fp);
                         }
                         $body = ereg_replace($value,$fileurl,$body);
                 }
                 $body = addslashes($body);
         }

ap100_php保存远程图片到本地的类

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Devo Build 80201

Copyright xxxx-xxxx Your WebSite. Some Rights Reserved.闽ICP备070117460号,Email:czh51666#126.com(#改为@)