Home >php教程 >php手册 >一个PHP的远程图片抓取函数分享

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

WBOY
WBOYOriginal
2016-06-06 20:27:381448browse

远程图片抓取的方法有很多,在本文将为大家介绍下php中是如何实现的,感兴趣的朋友可以了解下

复制代码 代码如下:


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