<?php/** 远程图片本地化 **/ 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_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; // 返回数据 } //远程图片本地化 function imglocation($content){ preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png)(.*\"))/isu",$content,$img_array); $timesrc=date("ymd", time()); $img_array = array_unique($img_array[1]); $imgpath = dirname(__file__).'/../um/php/upload/'.$timesrc; if(!is_dir($imgpath.'/')) { mkdir($imgpath, '0777',true); chmod($imgpath, '0777'); } foreach($img_array as $key=>$value){ $value=str_repeat('"',"",$value); $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'; } } $runds=md5(time()); $rndfilename=$imgpath."/".$runds.$itype; $tp = fopen($rndfilename, 'wb'); fwrite($tp, $http['data']); fclose($tp); if(file_exists($rndfilename)) { $sqlurl="/home/um/php/upload/".$timesrc."/".$runds.$itype; $content = str_replace($value, $sqlurl.'"', $content); $content = str_replace("alt=", "", $content); } } return $content; } ?>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号