PHP download remote images and save to local code

Source: Internet
Author: User
Tags php download
When we use PHP as a simple crawler, we often encounter the need to download remote images, so the following is a simple implementation of the requirements. The main article and we introduce the PHP implementation of remote image download method, small series feel very good, and now share to everyone, but also for everyone to do a reference, hope to help everyone.

1. Using Curl

For example, we have the following two pictures:


$images = [  ' https://dn-laravist.qbox.me/2015-09-22_00-17-06j.png ',  ' https://dn-laravist.qbox.me/ 2015-09-23_00-58-03j.png '];

In the first step, we can directly use the simplest code implementations:


function Download ($url, $path = ' images/') {  $ch = Curl_init ();  curl_setopt ($ch, Curlopt_url, $url);  curl_setopt ($ch, Curlopt_returntransfer, 1);  curl_setopt ($ch, Curlopt_connecttimeout,);  $file = curl_exec ($ch);  Curl_close ($ch);  $filename = PathInfo ($url, pathinfo_basename);  $resource = fopen ($path. $filename, ' a ');  Fwrite ($resource, $file);  Fclose ($resource);}

That's what you can do when you download a remote image:


foreach ($images as $url) {  download ($url);}

2. Encapsulation of a class

After the idea is clear, we can encapsulate this basic function in a class:


Class Spider {public  function downloadimage ($url, $path = ' images/')  {    $ch = Curl_init ();    curl_setopt ($ch, Curlopt_url, $url);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_connecttimeout,);    $file = curl_exec ($ch);    Curl_close ($ch);    $filename = PathInfo ($url, pathinfo_basename);    $resource = fopen ($path. $filename, ' a ');    Fwrite ($resource, $file);    Fclose ($resource);  }}

We can also optimize this a little bit:


Public Function Downloadimage ($url, $path = ' images/')  {    $ch = Curl_init ();    curl_setopt ($ch, Curlopt_url, $url);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_connecttimeout,);    $file = curl_exec ($ch);    Curl_close ($ch);    $this->saveasimage ($url, $file, $path);  }  Private Function Saveasimage ($url, $file, $path)  {    $filename = PathInfo ($url, pathinfo_basename);    $resource = fopen ($path. $filename, ' a ');    Fwrite ($resource, $file);    Fclose ($resource);  }

After encapsulation into a class, we can call the code to download the image:


$spider = new Spider (), foreach ($images as $url) {  $spider->downloadimage ($url);}

This way, the basic remote image download is OK.

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.