0

I'm using imagecopyresampled to resize (shrink) an image, which happens to be a gif. The image contains text which, when resized, is quite blurry. I wouldn't necessarily mind that, but when displaying the original image on a web page, at the reduced size, my browser scales it down with much nicer results. Any idea what I can do to improve what PHP produces?

UPDATE: Here's an example of the code I'm running:

$x1 = 0;
$y1 = 0;
$w1 = 196;
$h1 = 260;
$x2 = 0;
$y2 = 0;
$w2 = 140;
$h2 = 186;
$r1 = imagecreatefromgif($source);
$r2 = imagecreatetruecolor($w2, $h2);
imagealphablending($r2, false);
imagesavealpha($r2, true);
$res = imagecopyresampled($r2, $r1, $x2, $y2, $x1, $y1, $w2, $h2, $w1, $h1);
imagegif($r2, $dest);

Here's an example of the image scaled by the browser: enter image description here

Here's an example of the image scaled with the above code: enter image description here

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
  • You should post your code and some sample images (original and resized) so people can see what you are actually doing. – maxhb Feb 16 '16 at 11:10
  • Are you keeping it in GIF format or changing to a JPEG? If so, you might need to set the JPEG quality higher. – davidethell Feb 16 '16 at 11:10
  • @maxhb will do, as soon as I can – Bobby Jack Feb 16 '16 at 11:14
  • @davidethell keeping it in gif – Bobby Jack Feb 16 '16 at 11:14
  • It's hard to tell because your code sample is incomplete - you don't provide $w2 and $h2. If I use the sizes of the images you have posted (338x426 and 324x394) it comes out fine. – Mark Setchell Feb 16 '16 at 11:46
  • @MarkSetchell I've added some example values. BTW, the images attached *here* are just screenshots, so please ignore their dimensions. The first (the nice one) is never actually an image because it's scaled in-browser. – Bobby Jack Feb 16 '16 at 11:59

2 Answers2

1

Try to use imagepng instead of imagegif. imagepng third parameter is quality. Check specs: http://php.net/manual/en/function.imagepng.php

0

The PHP libraries aren't going to produce the best quality resize operation however you may find you have better results if you stick to a ratio based on the original file such that resizing a 200x200 to 100x100 or 50x50 would be find as its easy maths for the process to handle (i.e. merge 2/4 pixels into one). Your current operation is producing blurry results as it'll be a random value such as scaling to 0.63% of the original size.

puppyFlo
  • 445
  • 4
  • 16
  • Understood. I can't actually do that, unfortunately, so I'll have to settle for the results I'm getting. Are you suggesting that something like the ImageMagick library would give better results? I'm still surprised the browser does it, in real-time, so much better than PHP. – Bobby Jack Feb 16 '16 at 14:09
  • I haven't ever used that library but might be worth a shot. It is disappointing that the browser does render it better however not too surprising that the performance is better since its a pretty important feature of a browser, PHP not so much. You might want to try along these lines: http://stackoverflow.com/questions/3708947/compress-gif-images-quality-in-php – puppyFlo Feb 16 '16 at 14:16