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…

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