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);
以上就是实现远程图片本地化的全部代码,希望对大家的学习有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!