Get remote images with PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:34:58
Original
839 people have browsed it

//
// Function: Get the remote image and save it locally
//
//
// Make sure you have permission to write files to the local server
//
//
// Variable description:
// $url is the complete URL address of the remote image and cannot be empty.
// $filename is optional: if empty, the local filename will be based on time and date
// Automatically generated.
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://www.BkJia.com/images/_1978837_detector_ap100.jpg","");
if($img):echo ‘

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508426.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 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!