Coding Triumph

Helpful code snippets & tutorials

How to download an image from the web with PHP

When it comes to downloading images from the internet with PHP, there are many ways you can approach this problem. In this post, I’ll show you three simple methods you can use to handle most of the usecases you may encounter.

Prerequisite:

  • PHP 5.6 and above
  • allow_fopen_url enabled
  • php_curl enabled

Method 1: Downloading small images:

If the image to download has a small size, then file_get_contents() and file_put_contents() are all you need. That’s because the file_put_contents() load the entire image into the memory.

// image url
$image_url = 'https://cdn.pixabay.com/photo/2022/05/12/19/11/flowers-7192179_960_720.jpg';

// use basename() function to get the image name (flowers-7192179_960_720.jpg)
$save_name = basename($image_url);

// file_get_contents() will retrieve the image from the URL 
// file_put_contents() will save the image to the hard drive
if (file_put_contents($save_name, file_get_contents($image_url))) {
    echo "Image downloaded successfully";
} else {
    echo "Download failed.";
}

Method 2: Downloading large images with fopen()

Big images must be downloaded in small chunks otherwise the system will run out of memory. The fopen() function will download the image from the web a stream and save it to the file system:

$image_url = 'https://cdn.pixabay.com/photo/2022/05/12/19/11/flowers-7192179_960_720.jpg';

// use basename() function to get the file name
$save_name = basename($image_url);

// load the image from the URL as a stream
$in = fopen($image_url, 'r');

if ($in) {
    // the downloaded image will be saved to the current directory
    $out = fopen('./' . $save_name, 'wb');
    if ($out) {
        // set the buffer size to 8Kb (suitable for downloading large files)
        while (!feof($in)) {
            $chunk = fread($in, 1024 * 8);
            fwrite($out, $chunk, 1024 * 8);
        }
    }
}
if ($in) {
    fclose($in);
}
if ($out) {
    fclose($out);
}

Method 3: Downloading big images with cURL

Like the fopen() function above, the cURL library will download large images from the web as a stream. The only difference is that cURL offers better control over the `request’ configurations:

// the image url
$image_url = 'https://cdn.pixabay.com/photo/2022/05/12/19/11/flowers-7192179_960_720.jpg';

// use basename() function to get the file name
$file_name = basename($image_url);

// initialize the cURL session
$ch = curl_init($image_url);

if ($ch) {
    // save the image to the current directory
    $out = fopen('./' . $file_name, 'wb');
    if ($out) {
        // configure cURL 
        curl_setopt($ch, CURLOPT_FILE, $out);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // execute cURL session
        curl_exec($ch);
    }
}

// closes cURL session and free all resources
if ($ch) {
    curl_close($ch);
}

// close output directory
if ($out) {
    fclose($out);
}
If you like this post, please share
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments