Sometimes we can see some pretty pictures on the network, but we don't want to save them manually, especially a large number of such images, at this time, we need to write a program to help us complete this job. Today, we found a previously found function during data sorting. with 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.