PHP Gets the remote picture and saves it to the local method

Source: Internet
Author: User
Tags exit php programming

The example in this article describes how PHP gets a remote picture and saves it locally. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The <?php function Grabimage ($url, $filename = " ") {if ($url = =" "): return false; endif If the $url address is empty, simply exit if ($filename = = "") {//If no new filename is specified $ext = STRRCHR ($url, "."); Get $url Picture format if ($ext!= ". gif" && $ext!= ". jpg"): return false; endif If the picture is not formatted as. gif or. jpg, exit $filename = Date ("Dmyhis") directly. $ext; Name the new filename on the day of the month (Ob_start);//Open Output ReadFile ($url);//output picture file $img = ob_get_contents ()//Get browser output Ob_end_clean ();// Clear output and close $size = strlen ($img);//Get picture size $fp 2 = @fopen ($filename, "a"); Fwrite ($fp 2, $img);//write a picture file to the current directory and rename fclose ($fp 2); return $filename//returns a new filename} $img = Grabimage ("6648d73db0edd1e89f3d62f7.gif", ""); if ($img): Echo ' <pre></pre> '; If the return value is true, this shows that the picture that has been collected on the server Else:echo "false"; endif Otherwise, the output acquisition fails?> the

I hope this article will help you with your PHP programming.

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.