2

I'm trying to retrieve a remote image, but when I view the image (either output straight to browser or saved to disk and viewed) the quality is always lower.

Is there a way to do this without quality loss? Here are the methods I've used, but with the same result.

$imagePath is a path like http://www.example.com/myimage.jpg and $filePath is where the image will be saved to.

curl

$ch = curl_init($imagePath);
$fp = fopen($filePath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

file_get_contents

$tmpImage = file_get_contents($imagePath);
file_put_contents($filePath, $tmpImage);

I'll get a screenshot to show the quality issues shortly.

If you look around the KIA logo you can see of the left the quality issues I'm having. enter image description here

Update: Upon some further testing with different images it seems the quality issues are different with each image. Some has no issues at all.

Also the image which the screenshots are from above, based on the long url to the image, it seems like the image has already been resized and had it's quality alter before it gets to my script, so I'm thinking that could account for these issues too.

TMH
  • 6,096
  • 7
  • 51
  • 88
  • 7
    Quality shouldn't be worse. Have you compared file sizes, it's should be identical. – Blackus Apr 17 '15 at 09:38
  • 2
    Please share the exact image URL. I guess quality wouldn't be different. – Jenson M John Apr 17 '15 at 09:43
  • It can also be due to some hotlinking protection. – tmt Apr 17 '15 at 10:06
  • 1
    Unless the server outputs different quality based on headers or another random factor (which is highly unlikely) the images should always be the same, and thus have the same quality. – Rob Apr 17 '15 at 10:08
  • 2
    Yeah this doesn't make sense. Calculate the md5sum of the original and downloaded image. If they're not the same, you have something screwing with your data in transit. Try to perform the same task over HTTPS to eliminate the chance of something at the network level re-encoding the image files – Drew Hammond Apr 17 '15 at 10:13
  • 3
    Are you sure you are downloading the proper image and not just a thumbnail ? – mb14 Apr 17 '15 at 10:40

0 Answers0