This article mainly introduces how PHP obtains remote images and saves them to a local device. it involves related php remote file operations, for more information about how to obtain remote images and save them locally, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
<? Phpfunction GrabImage ($ url, $ filename = "") {if ($ url = ""): return false; endif; // if the $ url address is empty, exit if ($ filename = "") {// if no new file name is specified $ ext = strrchr ($ url ,". "); // get the $ url image format if ($ ext! = ". Gif" & $ ext! = ". Jpg "): return false; endif; // exit $ filename = date (" dMYHis ") if the image format is not "). $ ext; // name the new file name by the hour, minute, and second of the day,} ob_start (); // open the output readfile ($ url ); // output image file $ img = ob_get_contents (); // Get the browser output ob_end_clean (); // clear the output and close $ size = strlen ($ img ); // get the image size $ fp2 = @ fopen ($ filename, "a"); fwrite ($ fp2, $ img); // write the image file to the current directory, and rename fclose ($ fp2); return $ filename; // return the new file name} $ img = GrabImage ("http://imgsrc.baidu.com/baike/abpic/item/6648d73db0edd1e89f3d62f7.jpg", ""); if ($ img ): echo''; // If the returned value is true, the else: echo "false"; endif; // otherwise, the output fails to be collected.
I hope this article will help you with php programming.