How to save remote images to a local device in php. Today, we found a previously found function during data sorting. with this function, we can implement the above functions. Main function: Copy the code as follows: functionGrabI found a previously found function during data sorting today. through this function, we can implement the above functions.
Main functions:
The code is as follows:
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, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
Code for retrieving an image:
The code is as follows:
$ Img = GrabImage ("http://www.baidu.com/img/baidu_logo.gif", "logo.gif ");
If ($ img ){
Echo '';
} Else {
Echo "false ";
}
This is an example of saving the google logo. the obtained images are saved in the same-level Directory.
Obtain a series of regular images (for example, 100 images named after numbers 1 ):
The code is as follows:
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 image URL and needs to be modified by yourself. after the program is executed, all the images will be saved to the images directory.
Bytes. Main function: Code: function GrabI...