/**
* PHP captures images on a webpage to local storage
* @param $imgUrl Picture URL address
* @param string $saveDir Local storage path is stored by default in the current path
* @param null $fileName picture stored to a local file name
* @return Mix
*/
function Crabimage ($imgUrl, $saveDir = './', $fileName =null) {
if (empty ($IMGURL)) {
return false;
}
Get picture information size
$imgSize = getimagesize ($IMGURL);
if (!in_array ($imgSize [' MIME '],array (' image/jpg ', ' image/gif ', ' image/png ', ' Image/jpeg '), true)) {
return false;
}
Get suffix Name
$_mime = explode ('/', $imgSize [' mime ']);
$_ext = '. '. End ($_mime);
if (empty ($fileName)) {//Generate a unique file name
$fileName = Uniqid (Time (), true). $_ext;
}
Start grabbing
Ob_start ();
ReadFile ($IMGURL);
$imgInfo = Ob_get_contents ();
Ob_end_clean ();
if (!file_exists ($saveDir)) {
mkdir ($saveDir, 0777,true);
}
$fp = fopen ($saveDir. $fileName, ' a ');
$imgLen = strlen ($imgInfo); Calculate the image source size
$_inx = 1024; 1k per write
$_time = ceil ($imgLen/$_inx);
for ($i =0; $i <$_time; $i + +) {
Fwrite ($fp, substr ($imgInfo, $i *$_inx, $_inx));
}
Fclose ($FP);
Return Array (' file_name ' = $fileName, ' save_path ' = $saveDir. $fileName);
}
$url = ' https://mmbiz.qlogo.cn/mmbiz/ 7wqtti9h56hgvoh8j0xp5v97cdnoxf94vq0ndnohzmb2ziajlfwd2u8gnoevtqxdwlrpouibkksebexmr2epe0pq/0?wx_fmt=gif ';
$url = ' http://www.phpernote.com/images/logo.gif ';
$url = ' http://avatar.csdn.net/5/3/6/1_u014236259.jpg ';
Var_dump (Crabimage ($url));
PHP Crawl remote image to local save