手机
当前位置:查字典教程网 >编程开发 >php教程 >用php获取远程图片并把它保存到本地的代码
用php获取远程图片并把它保存到本地的代码
摘要:dedecms中的:if(!empty($saveremoteimg)){$body=stripslashes($body);$img_ar...

<?php

functionGrabImage($url,$filename=""){

if($url==""):returnfalse;endif;

if($filename==""){

$ext=strrchr($url,".");

if($ext!=".gif"&&$ext!=".jpg"):returnfalse;endif;

$filename=date("dMYHis").$ext;

}

ob_start();

readfile($url);

$img=ob_get_contents();

ob_end_clean();

$size=strlen($img);

$fp2=@fopen($filename,"a");

fwrite($fp2,$img);

fclose($fp2);

return$filename;

}

$img=GrabImage("http://news.bbc.co.uk/images/_1978837_detector_ap100.jpg","");

if($img):echo'<pre><imgsrc="'.$img.'"></pre>';else:echo"false";endif;

?>

dedecms中的:

if(!empty($saveremoteimg))

{

$body=stripslashes($body);

$img_array=array();

preg_match_all("/(src|SRC)=["|'|]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);

$img_array=array_unique($img_array[2]);

set_time_limit(0);

$imgUrl=$img_dir."/".strftime("%Y%m%d",time());

$imgPath=$base_dir.$imgUrl;

$milliSecond=strftime("%H%M%S",time());

if(!is_dir($imgPath))@mkdir($imgPath,0777);

foreach($img_arrayas$key=>$value)

{

$value=trim($value);

$get_file=@file_get_contents($value);

$rndFileName=$imgPath."/".$milliSecond.$key.".".substr($value,-3,3);

$fileurl=$imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);

if($get_file)

{

$fp=@fopen($rndFileName,"w");

@fwrite($fp,$get_file);

@fclose($fp);

}

$body=ereg_replace($value,$fileurl,$body);

}

$body=addslashes($body);

}

【用php获取远程图片并把它保存到本地的代码】相关文章:

PHP实现远程下载文件到本地

用php发送带附件的Email

用PHP代码给图片加水印

php curl 获取https请求的2种方法

PHP CURL 多线程操作代码实例

php获取网页里所有图片并存入数组的方法

PHP中把对象转换为关联数组代码分享

用IE远程创建Mysql数据库的简易程序

Yii获取当前url和域名的方法

PHP获取远程图片并保存到本地的方法

精品推荐
分类导航