Development

Save image using cURL with PHP

Often cURL in PHP is used to download whole webpages, another great use is downloading and saving images.

Using cURL to save images is used over the file_put_content(file_get_contents()) because you can apply more parameters to the request most important being able to set a user agent.

With cURL I can state “who I am” when I make the request for the image.

You can hide your scraping bot and pretend you’re just a regular web user…

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)");

Or have some fun:

curl_setopt($ch, CURLOPT_USERAGENT, "Grabbing image in 3, 2, 1... Thanks");

Obviously a regular user wouldn’t be hitting as many requests in little time as a scraping bot would.

By using

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

The image is returned as a raw binary string and not outputted.

curl_setopt($ch, CURLOPT_HEADER, false);

Means no header is in the output.

$fp = fopen($save_as, 'x');
fwrite($fp, $raw_data);
fclose($fp);

Handles the saving of the raw image data into the actual viewable image file.

The full code is:

<?php
$image_url = 'https://website.com/img/1234.jpg';
$save_as = 'img/1234.jpg';
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)");
$raw_data = curl_exec($ch);
curl_close($ch);
$fp = fopen($save_as, 'x');
fwrite($fp, $raw_data);
fclose($fp);

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago