PHP Crawl remote picture and save as the implementation method

Source: Internet
Author: User

The following is the source code, and its related explanations

  

URL is a remote full picture address, can not be empty, $filename is saved as a picture name

Default to put the picture in the same directory as this script

function Grabimage ($url, $filename = "") {

Returns False if the $url is empty;

if ($url = = "") {return false;}

$ext = STRRCHR ($url, "."); /Get the image extension

if ($ext!= ". gif" && $ext!= ". jpg" && $ext!= ". bmp") {echo "format does not support!"; return false;}

if ($filename = = "") {$filename = time (). " $ext ";} A time stamp, another name.

Start capturing

Ob_start ();

ReadFile ($url);

$img = Ob_get_contents ();

Ob_end_clean ();

$size = strlen ($img);

$FP 2 = fopen ($filename, "a");

Fwrite ($fp 2, $img);

Fclose ($fp 2);

return $filename;

}

Test

Grabimage ("200609082320515027.bmp", "as.gif");

?>

Related description:

Ob_start: Turn on output buffering

This function would turn output buffering on. While output buffering are active no output is sent from the script (other than headers), instead the "output is" stored in a n Internal buffer. (output is stored in the internal buffer)

//

ReadFile: Reads a file and writes to the output buffer

Returns the number of bytes read from the file. If the error returns false and an error message is displayed unless invoked in the form of @readfile ().

//

Ob_get_contents:return the contents of the output buffer (returns the contents of the outputs buffered)

This would return the contents of the output buffer without clearing it or FALSE, if output buffering isn ' t active. (FALSE if the output buffer is not active (open))

//

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

The

This function discards (discards) the contents of the topmost output buffer and turns off this output buffering. (Discard and turn off) If you are want to further process the buffer ' s contents you have to call Ob_get_contents () before Ob_end_clean () as the Buffe R contents are discarded when Ob_end_clean () is called. (If you want to use buffered content, call Ob_get_contents () before you clean out the output buffer.) The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. Reasons for failure are-I-called the function without an active buffer or this for some reason a buffer could Not is 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.