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.