PHP保存远程图片到本地的类

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4490194096475053&amp;dt=1228587706843&amp;lmt=1228528468&amp;prev_slotnames=1891601125&amp;output=html&amp;slotname=3685991503&amp;correlator=1228587706812&amp;url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F10%2F1847.html&amp;ea=0&amp;ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__95.html&amp;frm=0&amp;ga_vid=704176619.1228587707&amp;ga_sid=1228587707&amp;ga_hid=1712630427&amp;flash=9.0.124.0&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true&amp;dtd=16" frameborder="0" width="300" scrolling="no" height="250" allowtransparency></iframe>
<?php

//
// Function: 获取远程图片并把它保存到本地
//
//
// 确定您有把文件写入本地服务器的权限
//
//
// 变量说明:
// $url 是远程图片的完整URL地址,不能为空。
// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
// 自动生成.

function GrabImage($url,$filename="") {
if($url==""):return false;endif;

if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;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><img src="'.$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_array as $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,ext,Google,Flash,FP)