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, see
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, see
This example describes how PHP retrieves remote images and saves them to a local device. 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.
,