Save remote images to local method under PHP
- 2020-03-31 21:00:10
- OfStack
Today, when sorting out the data, we found a function found before, through which we can achieve the above function.
Main functions:
Get a picture of the code:
This is an example of saving the Google logo, and the image is saved under the same category.
Get a series of regular pictures (for example, 100 pictures named after the Numbers 1-100) :
The above www.yourimagesite.com is the website of the image, which needs to be modified by yourself. After the execution of the program, all images will be saved in the directory of images.
Main functions:
function GrabImage($url,$filename="") {
if($url=="") return false;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
$filename=date("YmdHis").$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;
}
Get a picture of the code:
$img=GrabImage("http://www.baidu.com/img/baidu_logo.gif","logo.gif");
if($img){
echo '<img src="'.$img.'">';
}else{
echo "false";
}
This is an example of saving the Google logo, and the image is saved under the same category.
Get a series of regular pictures (for example, 100 pictures named after the Numbers 1-100) :
for ($i=1;$i<=100;$i++){
$img=GrabImage("http://www.yourimagesite.com/images/$i.gif","images/$i.gif");
}
The above www.yourimagesite.com is the website of the image, which needs to be modified by yourself. After the execution of the program, all images will be saved in the directory of images.