PHP Crawl remote pictures and save as an implementation method _php tutorial

Source: Internet
Author: User

How PHP crawls remote images and saves them as an implementation


Here is the source code, and its related explanations

  

URL is the full picture address of the remote, cannot be empty, $filename is saved as the picture name

By default, the image is placed in the same directory as this script.

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

Returns False if $url is empty;

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

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

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

if ($filename = = "") {$filename = time (). " $ext ";} Named by Time stamp

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 ("Http://www.66xing.com/UploadFile/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 buffered in-house)

//

ReadFile: Read in a file and write to output buffer

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

//

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

This would return the contents of the output buffer without clearing it or FALSE if output buffering isn ' t active. (returns 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)

This function discards (discarded) the contents of the topmost output buffer and turns off this output buffering. (Discard and turn off) If you want to further process the buffer's contents you had to call Ob_get_contents () before Ob_end_clean () as the Buffe R contents is discarded when Ob_end_clean () is called. (If you want to use buffered content, ob_get_contents () is called before the output buffer is cleaned) The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. Reasons for failure is first, you called the function without an active buffer or that for some reason a buffer could Not being deleted (possible for special buffer).

http://www.bkjia.com/PHPjc/848320.html www.bkjia.com true http://www.bkjia.com/PHPjc/848320.html techarticle php Crawl remote picture and save as the implementation method below is the source code, and its related interpretation//url is the full picture address of the remote, can not be empty, $filename is saved as the picture name/...

  • Related Article

    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.