Local connection restricted or no connection PHP save remote picture to local method

Source: Internet
Author: User
Today, when we collate the information, we find a function that we have found before, through which we can implement the above function.
Main functions:

Copy the Code code as follows:


function Grabimage ($url, $filename = "") {
if ($url = = "") return false;
if ($filename = = "") {
$ext =STRRCHR ($url, ".");
if ($ext! = ". gif" && $ext! = ". jpg" && $ext! = ". png") return false;
$filename =date ("Ymdhis"). $ext;
}
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;
}


Get the code for a picture:

Copy the Code code as follows:


$img =grabimage ("Http://www.baidu.com/img/baidu_logo.gif", "logo.gif");
if ($img) {
Echo ';
}else{
echo "false";
}


This is an example of saving Google's logo, and the captured image is saved under a sibling directory.
Get a series of regular pictures (for example: 100 pictures named with the number 1-100):

Copy the Code code as follows:


for ($i =1; $i <=100; $i + +) {
$img =grabimage ("http://www.yourimagesite.com/images/$i. gif", "images/$i. gif");
}


The above www.yourimagesite.com is the URL of the picture, you need to modify it, after the execution of the program, all the pictures will be saved to the images directory.

The above describes the local connection is limited or no connection PHP to save the remote image to the local, including local connection limited or no connection content, I hope to be interested in PHP tutorial friends helpful.

  • 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.