Php collects remote webpage images and saves them to the local device-PHP source code

Source: Internet
Author: User
Ec (2); ob_start: Open the output buffer readfile: reads a file and writes it to the output buffer to return the number of bytes read from the file. If an error is returned, FALSE is returned, and the error message is displayed unless called in the form of @ readfile. Ob_end_clean (): Clean (erase) theoutputbufferandturnoffoutputbuffering (clear output script ec (2); script

Ob_start: Enable the output buffer.

Readfile: reads a file and writes it to the output buffer.
Returns the number of bytes read from the file. If an error is returned, FALSE is returned, and the error message is displayed unless called in the form of @ readfile.

Ob_end_clean (): Clean (erase) the output buffer and turn off output buffering (clear output buffer)


// The URL is the complete remote image address and cannot be blank. $ filename is the name of the saved image.
// By default, images are stored in the same directory as the script.
Function GrabImage ($ url, $ filename = ""){
// If $ url is null, false is returned;
If ($ url = "") {return false ;}
$ Ext = strrchr ($ url, "."); // get the image extension.
If ($ ext! = ". Gif" & $ ext! = ". Jpg" & $ ext! = ". Bmp") {echo "format is not supported! "; Return false ;}
If ($ filename = "") {$ filename = time (). "$ ext";} // name it with a timestamp
// Start capturing
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ Fp2 = fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
// Test
GrabImage ("http://www.111cn.net tutorial/banner/banner.gif", "as.gif ");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.