Categories: Development

Comparing speed for 3 PHP save image methods

Testing the speed with 3 methods to save or download an image in PHP, each method was run twice and then the average time was used with the output filename being unique to avoid overwrites.

File get and put contents: 0.0122 seconds.

file_put_contents("out.jpg", file_get_contents("https://in.jpg"));

Curl: 0.0222 seconds.

$ch = curl_init("https://in.jpg");
$fp = fopen("out.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch);
curl_close($ch);
fclose($fp);

Imagick: 0.2944 seconds.

$im = new Imagick();
$im->readImage("https://in.jpg");
$im->writeImage("out.jpg");

Surprising the most simplest method was the quickest. It’s just one line and doesn’t use any external PHP plugin. file_get_contents and then file_put_contents won this battle in being the quickest at saving an external jpg file.

Share
Tags: PHPWeb Dev

Recent Posts

Kennington reservoir drained drone images

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

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago