How to save remote images to a local place in php _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
How to save remote images to a local device in php. Today, we found a previously found function during data sorting. with this function, we can implement the above functions. Main function: Copy the code as follows: functionGrabI found a previously found function during data sorting today. through this function, we can implement the above functions.

Main functions:

The code is 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 );

$ Fp2 = @ fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );

Return $ filename;
}


Code for retrieving an image:

The code is 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 the google logo. the obtained images are saved in the same-level Directory.

Obtain a series of regular images (for example, 100 images named after numbers 1 ):

The code is 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 image URL and needs to be modified by yourself. after the program is executed, all the images will be saved to the images directory.

Bytes. Main function: Code: function GrabI...

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.