PHP 远程图片本地化

php中文网
发布: 2016-06-20 12:45:44
原创
1484人浏览过

<?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;			 }	 	 	 ?>
登录后复制


改图鸭AI图片生成
改图鸭AI图片生成

改图鸭AI图片生成

改图鸭AI图片生成 30
查看详情 改图鸭AI图片生成
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号