Home  >  Article  >  Backend Development  >  一个PHP的远程图片抓取函数分享_php技巧

一个PHP的远程图片抓取函数分享_php技巧

WBOY
WBOYOriginal
2016-05-17 08:55:16767browse
复制代码 代码如下:

function grabImage($url, $filename = '') {
if($url == '') {
return false; //如果 $url 为空则返回 false;
}
$ext_name = strrchr($url, '.'); //获取图片的扩展名
if($ext_name != '.gif' && $ext_name != '.jpg' && $ext_name != '.bmp' && $ext_name != '.png') {
return false; //格式不在允许的范围
}
if($filename == '') {
$filename = time().$ext_name; //以时间戳另起名
}
//开始捕获
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;
}
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