skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

     <div style=”width:250px; height:400px”></div>
    

    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

    /* low resolution display */
    .image {
      background-image: url(/pathTo/lowResImg.png);
      background-size: 250px 400px;
    }
    
    /* for retina display */
    @media only screen and (min--moz-device-pixel-ratio: 2),
    only screen and (-o-min-device-pixel-ratio: 2/1),
    only screen and (-webkit-min-device-pixel-ratio: 2),
    only screen and (min-device-pixel-ratio: 2) {
      .image {
        background: url(/pathTo/highResImage.png) no-repeat;
        background-size: 250px 400px;
      }
    }
    

    Hope this helps.

    Login or Signup to reply.
  3. 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 function imageconvolution, with all coefficients equal to 1, and apply two or three passes. Only then you can use imagecopyresampled.


    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search