PHP code for capturing web images and saving them as images

Source: Internet
Author: User
The URL is a complete remote image address and cannot be blank. $ filename is the name of the saved Image. by default, the image is placed in the same directory as this script. the source code and related explanations are as follows:

The code is as follows:


// 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.jb51.net/images/logo.gif", "as.gif ");
?>


Ob_start: enable the output buffer.
This function will turn output buffering on. while output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. (the output is stored in the internal 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_get_contents: Return the contents of the output buffer (Return the content of the output buffer)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn' t active. (if the output buffer has no activity (open), FALSE is returned)
//
Ob_end_clean (): Clean (erase) the output buffer and turn off output buffering (clear output buffer)
This function discards (discard) the contents of the topmost output buffer and turns off this output buffering. (discard and disable) If you want to further process the buffer's contents you have to call ob_get_contents () before ob_end_clean () as the buffer contents are discarded when ob_end_clean () is called. (If you want to buffer The content, call ob_get_contents () before cleaning The output buffer.) The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. reasons for failure are first that you called the function without an active buffer or that for some reason a buffer cocould not be deleted (possible for special buffer ).

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.