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执行速度全攻略(下)
Oct 09 PHP
SMARTY学习手记
Jan 04 PHP
让的PHP代码飞起来的40条小技巧(提升php效率)
Apr 12 PHP
PHP操作Memcache实例介绍
Jun 14 PHP
浅析php中如何在有限的内存中读取大文件
Jul 02 PHP
PHP下获取上个月、下个月、本月的日期(strtotime,date)
Feb 02 PHP
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
May 04 PHP
WordPress中用于创建以及获取侧边栏的PHP函数讲解
Dec 29 PHP
PHP递归遍历指定文件夹内的文件实现方法
Nov 15 PHP
PHP单例模式详解及实例代码
Dec 21 PHP
PHP迭代器和生成器用法实例分析
Sep 28 PHP
PHP8.0新功能之Match表达式的使用
Jul 19 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
PHP数据库开发知多少
2006/10/09 PHP
php下将XML转换为数组
2010/01/01 PHP
PHP模块 Memcached功能多于Memcache
2011/06/14 PHP
php后台多用户权限组思路与实现程序代码分享
2012/02/13 PHP
php操作MongoDB类实例
2015/06/17 PHP
Yii2下session跨域名共存的解决方案
2017/02/04 PHP
Jquery 自定义动画概述及示例
2013/03/29 Javascript
点击表单提交时出现jQuery没有权限的解决方法
2014/07/23 Javascript
angular.element方法汇总
2015/01/07 Javascript
jQuery固定元素插件scrolltofixed使用指南
2015/04/21 Javascript
表单验证正则表达式实例代码详解
2015/11/09 Javascript
js性能优化技巧
2015/11/29 Javascript
JavaScript解八皇后问题的方法总结
2016/06/12 Javascript
JS代码实现根据时间变换页面背景效果
2016/06/16 Javascript
AngularJS实现DOM元素的显示与隐藏功能
2016/11/22 Javascript
微信小程序 本地存储及登录页面处理实例详解
2017/01/11 Javascript
简单的jQuery拖拽排序效果的实现(增强动态)
2017/02/09 Javascript
JS仿Base.js实现的继承示例
2017/04/07 Javascript
node通过express搭建自己的服务器
2017/09/30 Javascript
vue项目中jsonp跨域获取qq音乐首页推荐问题
2018/05/30 Javascript
简化vuex的状态管理方案的方法
2018/06/02 Javascript
JavaScript实现简单音乐播放器
2020/04/17 Javascript
js贪心算法 钱币找零问题代码实例
2019/09/11 Javascript
js实现固定区域内的不重叠随机圆
2019/10/24 Javascript
vue 解决mintui弹窗弹起来,底部页面滚动bug问题
2020/11/12 Javascript
python中的代码编码格式转换问题
2015/06/10 Python
flask框架中勾子函数的使用详解
2018/08/01 Python
python看某个模块的版本方法
2018/10/16 Python
Python脚本导出为exe程序的方法
2020/03/25 Python
优秀少先队辅导员先进事迹材料
2014/05/18 职场文书
服装设计专业求职信
2014/06/16 职场文书
公司离职证明范本(汇总)
2014/09/10 职场文书
工作总结与自我评价
2014/09/18 职场文书
公司捐书倡议书
2015/04/27 职场文书
演讲比赛通讯稿
2015/07/18 职场文书
小学科学课教学反思
2016/02/23 职场文书