Home > Backend Development > PHP Tutorial > PHP downloads remote images and saves them to local code

PHP downloads remote images and saves them to local code

小云云
Release: 2023-03-20 10:18:01
Original
9891 people have browsed it

When using PHP to make a simple crawler, we often encounter the need to download remote images, so let’s simply implement this need. The article mainly introduces to you the method of downloading remote pictures in PHP. The editor thinks it is quite good. Now I share it with you and give you a reference. I hope it can help you.

1. Use 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'
];
Copy after login

In the first step, we can use it directly The simplest code implementation:

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, 30);
  $file = curl_exec($ch);
  curl_close($ch);
  $filename = pathinfo($url, PATHINFO_BASENAME);
  $resource = fopen($path . $filename, 'a');
  fwrite($resource, $file);
  fclose($resource);
}
Copy after login

Then when downloading remote images, you can do this:

foreach ( $images as $url ) {
  download($url);
}
Copy after login

2. Encapsulate a class

After clarifying the idea, we can encapsulate this basic function into 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, 30);
    $file = curl_exec($ch);
    curl_close($ch);
    $filename = pathinfo($url, PATHINFO_BASENAME);
    $resource = fopen($path . $filename, 'a');
    fwrite($resource, $file);
    fclose($resource);
  }
}
Copy after login

Now, we can also optimize it a little like this:

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, 30);
    $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);
  }
Copy after login

After encapsulating it into a class, we You can call the code like this to download pictures:

$spider = new Spider();

foreach ( $images as $url ) {
  $spider->downloadImage($url);
}
Copy after login

In this way, basic remote picture downloading is OK.

Related recommendations:

php implements an efficient method to obtain the size and size of remote images

php development example for downloading remote images to local Share

How to use php to collect remote pictures to local area

The above is the detailed content of PHP downloads remote images and saves them to local code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template