Home > Backend Development > PHP Tutorial > PHP function class to save remote images to local_PHP tutorial

PHP function class to save remote images to local_PHP tutorial

WBOY
Release: 2016-07-21 15:48:28
Original
1042 people have browsed it

//
// Function: Get the remote image and save it locally
//
//
// Make sure you have written the file to the local server Permissions
//
//
// Variable description:
// $url is the complete URL address of the remote image and cannot be empty.
// $filename is an optional variable: if empty, the local filename will be automatically generated based on time and date
//.
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("/upload/20081208002838680 .jpg","");
if($img):echo '

<img src="'.$img.'">
';else:echo "false ";endif;

?> in 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);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319705.htmlTechArticle?php // // Function: Get the remote image and save it locally // // // OK You have permission to write files to the local server // // // Variable description: // $url is the complete U of the remote image...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template