PHP capture Remote Web page images and save to local code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags clear code error error message file local php programming

Ob_start: Turn on output buffering

ReadFile: Reads a file and writes to the output buffer
Returns the number of bytes read from a file. If the error returns false and an error message is displayed unless invoked in the form of @readfile ().

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


<?php Tutorial
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 the script
function Grabimage ($url, $filename = "") {
returns false $url null;
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 ";} Another name with a timestamp
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.jzread.com/skin/default/imga/logo.gif", "as.gif");
?>

PHP captures remote Web page images and saves them to local
 

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.