php将远程图片保存到本地服务器的实现代码


Posted in PHP onAugust 03, 2015

php如何将远程图片本地化,本文分享了实现代码

<?php 
//站点根目录 
$cfg_basedir = dirname(__FILE__); 
//停建目录属性 
$cfg_dir_purview ='0755'; 
 /*Get请求远程内容函数*/ 
 $cookie_file = dirname(__FILE__).'/cookie.txt'; //COOKIE存放地址 
  function pget($url,$ref=false,$head=false){ 
  $curl = curl_init(); // 启动一个CURL会话 
  curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址       
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在 
  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 
  if($ref){ curl_setopt($curl, CURLOPT_REFERER, $ref);//带来的Referer 
  }else{ 
  curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer 
  } 
  curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求 
  curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie信息的文件名称 
  curl_setopt($curl, CURLOPT_COOKIEFILE,$GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息 
  curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 
  curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 
  $tmpInfo = curl_exec($curl); // 执行操作 
  if (curl_errno($curl)) { 
 echo 'Errno'.curl_error($curl); 
  } 
   if($head){ $data['head']=curl_getinfo($curl);} 
  curl_close($curl); // 关键CURL会话 
  $data['data']=$tmpInfo; 
  return $data; // 返回数据 
} 
 
 
/** 
 * 创建所有目录 
 * 
 * @param   string $truepath 真实地址 
 * @param   string $mmode  模式 
 * @return  bool 
 */ 
if ( ! function_exists('MkdirAll')) 
{ 
  function MkdirAll($truepath) 
  { global $cfg_dir_purview; 
      if(!file_exists($truepath)) 
      { 
        mkdir($truepath, $cfg_dir_purview); 
        chmod($truepath, $cfg_dir_purview); 
        return true; 
      } 
      else 
      { 
        return true; 
      } 
  } 
} 
  /** 
 * 获得文章body里的外部资源 
 * 
 * @access  public 
 * @param   string $body 文档内容 
 * @return  string 
 */ 
function GetCurContent($body) 
{ 
  global $cfg_multi_site,$cfg_basehost,$cfg_basedir,$cfg_image_dir,$arcID,$cuserLogin,$dsql; 
  $cfg_uploaddir = '/c'; 
  $basehost = "http://".$_SERVER["HTTP_HOST"]; 
     
  $img_array = array(); 
  preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png))/isU",$body,$img_array); 
   
  $img_array = array_unique($img_array[1]); 
  $imgUrl = $cfg_uploaddir.'/'.date("ymd", time()); 
  $imgPath = $cfg_basedir.$imgUrl; 
  if(!is_dir($imgPath.'/')) 
  { 
    MkdirAll($imgPath, $GLOBALS['cfg_dir_purview']); 
    CloseFtp(); 
  } 
  $milliSecond = date('His',time()); 
  foreach($img_array as $key=>$value) 
  {  
    if(preg_match("#".$basehost."#i", $value)) 
    { 
      continue; 
    } 
    if(preg_match("#".$basehost."#i", $value)) 
    { 
      continue; 
    } 
    if(!preg_match("#^http:\/\/#i", $value)) 
    { 
      continue; 
    } 
    
    $http=pget($value,'$value',true); 
    $itype=($http['head']['content_type']); 
    if(!preg_match("#\.(jpg|gif|png)#i", $itype)) 
    { 
      if($itype=='image/gif') 
      { 
        $itype = ".gif"; 
      } 
      else if($itype=='image/png') 
      { 
        $itype = ".png"; 
      } 
      else 
      { 
        $itype = '.jpg'; 
      } 
    } 
    $milliSecondN = rand(1000,9999).rand(1000,9999); 
    $value = trim($value); 
    $rndFileName = $imgPath.'/'.$milliSecondN.'-'.$key.$itype; 
    $fileurl = $imgUrl.'/'.$milliSecondN.'-'.$key.$itype; 
 
    $tp = fopen($rndFileName, 'wb'); 
    fwrite($tp, $http['data']); 
    fclose($tp); 
     
    if(file_exists($cfg_basedir.$fileurl)) 
    { 
      $info = ''; 
      $imginfos = GetImageSize($rndFileName, $info); 
      $fsize = filesize($rndFileName); 
      $body = str_replace($value, $fileurl, $body); 
  
    } 
  } 
  return $body; 
} 
 
//调用方式 
echo GetCurContent($body);

以上就是实现远程图片本地化的全部代码,希望对大家的学习有所帮助。

PHP 相关文章推荐
在PHP中利用XML技术构造远程服务(下)
Oct 09 PHP
浅谈apache和nginx的rewrite的区别
Feb 22 PHP
关于PHP自动判断字符集并转码的详解
Jun 26 PHP
PHP简单实现“相关文章推荐”功能的方法
Jul 19 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
PHP中new static() 和 new self() 的区别介绍
Jan 09 PHP
php输出全球各个时区列表的方法
Mar 31 PHP
laravel学习教程之关联模型
Jul 30 PHP
php微信支付接口开发程序
Aug 02 PHP
详谈phpAdmin修改密码后拒绝访问的问题
Apr 03 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 PHP
Laravel5.7框架安装与使用学习笔记图文详解
Apr 02 PHP
php基于session实现数据库交互的类实例
Aug 03 #PHP
php通过排列组合实现1到9数字相加都等于20的方法
Aug 03 #PHP
PHP实现递归复制整个文件夹的类实例
Aug 03 #PHP
UTF-8正则表达式如何匹配汉字
Aug 03 #PHP
PHP使用缓存即时输出内容(output buffering)的方法
Aug 03 #PHP
php中ob函数缓冲机制深入理解
Aug 03 #PHP
如何利用http协议发布博客园博文评论
Aug 03 #PHP
You might like
一台收音机,让一家人都笑逐颜开!
2020/08/21 无线电
PHP新手上路(八)
2006/10/09 PHP
PHP怎样用正则抓取页面中的网址
2016/08/09 PHP
微信公众平台开发(五) 天气预报功能开发
2016/12/03 PHP
js删除所有的cookie的代码
2010/11/25 Javascript
jQuery中创建实例与原型继承揭秘
2011/12/21 Javascript
js 关键词高亮(根据ID/tag高亮关键字)案例介绍
2013/01/21 Javascript
jQuery 全选/反选以及单击行改变背景色实例
2013/07/02 Javascript
通过隐藏iframe实现文件下载的js方法介绍
2014/02/26 Javascript
D3.js 从P元素的创建开始(显示可加载数据)
2014/10/30 Javascript
js实现有时间限制消失的图片方法
2015/02/27 Javascript
jQuery获取字符串中出现最多的数
2016/02/22 Javascript
标准的js无缝滚动效果
2016/08/30 Javascript
Web性能优化系列 10个提升JavaScript性能的技巧
2016/09/27 Javascript
Bootstrap风格的WPF样式
2016/12/07 Javascript
微信小程序实现倒计时补零功能
2018/07/09 Javascript
vue 项目接口管理的实现
2019/01/17 Javascript
JQuery判断radio单选框是否选中并获取值的方法
2019/01/17 jQuery
在Vue中获取自定义属性方法:data-id的实例
2020/09/09 Javascript
使用python实现baidu hi自动登录的代码
2013/02/10 Python
Python基于jieba库进行简单分词及词云功能实现方法
2018/06/16 Python
在python中使用with打开多个文件的方法
2019/01/07 Python
python numpy之np.random的随机数函数使用介绍
2019/10/06 Python
Python 中 -m 的典型用法、原理解析与发展演变
2019/11/11 Python
Python三维绘图之Matplotlib库的使用方法
2020/09/20 Python
利用css3制作3D样式按钮实现代码
2013/03/18 HTML / CSS
ECCO爱步官方旗舰店:丹麦鞋履品牌
2018/01/02 全球购物
德国高尔夫商店:Golfshop.de
2019/06/22 全球购物
印尼在线旅游门户网站:NusaTrip
2019/11/01 全球购物
全球精选男装和家居用品:Article
2020/04/13 全球购物
两年的个人工作自我评价
2014/01/10 职场文书
教学个人的自我评价分享
2014/02/16 职场文书
学生会主席竞聘书
2014/03/31 职场文书
公证书标准格式
2014/04/10 职场文书
婚宴父母致辞
2015/07/27 职场文书
Java多线程并发FutureTask使用详解
2022/06/28 Java/Android