I need to resize a bunch of large images and make them smaller. The originals are about 2500×3800 and I need to resize them down to about 400×650(I am keeping the exact aspect ratio, these are just some rough example numbers).
I am currently doing it with PHP, imagecopyresampled()
, imagejpeg()
and imagepng()
. When I resize them down this way the client is not happy with the resulting image quality – they’re slightly blurry. I am using the highest quality option for both imagejpeg()
and imagepng()
(ie. 100 and 0 respectively).
So how can I resize these images down and get a better quality than what the above is producing? Is there a better option in PHP? Should I use a different tool/language?
One SO question suggested to resize multiple times(eg. first resize to a medium size and then resize that to the small version). Is there any merit to that approach?
The funny thing is, I’m displaying the same resized images at an even smaller size on a different webpage. I’m making them about 10-20% smaller with HTML/CSS. When I do this the client is happy with the quality. So it appears there’s a way to increase image quality if you slightly reduce the image dimensions in the browser on the fly with HTML/CSS. Should I use this approach? It doesn’t sound like a great idea.
3
Answers
You don’t show your code so no one can help you much. But just a HINT. Maybe can be caused by the monitor being retina.
This is mostly due to the high resolution on retina displays.
CSS pixels are an abstract unit used by the browsers. CSS pixels are device-independent pixels. They readjust themselves to the pixel density of the screen they are rendered in.
If we have the following code:
The above component would look 250px by 400px in size in a standard display while 500px by 800px in a Retina display.
You have two options in such case
1. You can always use 2x sized image and display as 50% using css styling or setting img width and height
2. The better approach : Suppose you want to display an image of 250px by 400px, have an alternate image of size 500px by 800px in the server and render them whenever the webpage is served to a Retina display.
eg code for the second option
Hope this helps.
For proper downsampling, in a way that reduces aliasing, an image has to be band-limited by lowpass filtering (i.e. blurring !).
The function
imagecopyresampled
does not appear to perform such a blur (instead it is doing interpolation, which is only appropriate for upsampling). You can achieve the blur effect using the functionimageconvolution
, with all coefficients equal to1
, and apply two or three passes. Only then you can useimagecopyresampled
.You will think that I am crazy because the images are said to be too much blurred, but my bet is that what your client calls blurry is in fact aliased.