PHP implements downloading remote pictures and saving them locally

陈政宽~
Release: 2023-03-11 18:54:01
Original
1478 people have browsed it

This article mainly introduces the method of downloading remote images in PHP. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

When using PHP to make a simple crawler, we often encounter the need to download remote images, so let’s simply implement this need.

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.

The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support Script House.

The above is the detailed content of PHP implements downloading remote pictures and saving them locally. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!