Home >Backend Development >PHP Tutorial >Sharing a PHP remote image grabbing function_PHP tutorial

Sharing a PHP remote image grabbing function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:13:07874browse

Copy code The code is as follows:

function grabImage($url, $filename = '') {
if($url = = '') {
return false; //If $url is empty, return false;
}
$ext_name = strrchr($url, '.'); //Get the extension of the image
if($ext_name != '.gif' && $ext_name != '.jpg' && $ext_name != '.bmp' && $ext_name != '.png') {
return false; //Format Not within the allowed range
}
if($filename == '') {
$filename = time().$ext_name; //Rename with timestamp
}
// Start capturing
ob_start();
readfile($url);
$img_data = ob_get_contents();
ob_end_clean();
$size = strlen($img_data);
$local_file = fopen($filename , 'a');
fwrite($local_file, $img_data);
fclose($local_file);
return $filename;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313576.htmlTechArticleCopy the code as follows: function grabImage($url, $filename = '') { if($url == '') { return false; //If $url is empty, return false; } $ext_name = strrchr($url, '.'); //Get the image...
Statement:
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