How do You Resize a High Resolution Picture with PHP

wavyGravy picture wavyGravy · Jan 21, 2011 · Viewed 29.9k times · Source

I'm processing images when the user uploads a pic but my problem is high rez pics. How do you detect when the user uploads one? I'm using the GD library for PHP. Setting the quality to 90 with the imagejpeg() method doesn't do anything to it but scales the dimension and then sets the ppi to 72. I want to be able to preserve the dimension and drop the ppi to 72 at the same time. Any advice would be greatly appreciated.

Answer

coreyward picture coreyward · Jan 21, 2011

There is no such thing as PPI as far as you need to be concerned. Also, DPI doesn't exist in digital. There are only pixels, and the dimensions (which are just a measurement of pixels in either direction).

What you are really trying to do is downsample an image, reducing the overall dimensions of an image. You will probably want to do it proportionally, which is to say you want to keep the same ratio of width to height so that the image doesn't appear stretched after resampling it. Also worth noting, resizing and resampling differ in that resizing doesn't pay much attention to the content, resulting in very visually-poor resized images. You probably want resampling.

The PHP documentation has some great examples on handling uploaded files as well as how to resize images using the GDImage library. I suggest you check them out. Other relevant methods you might want to know about:

  • getimagesize() will let you check the dimensions of an image without opening it (useful to check if it needs to be resized without loading it up).
  • imagesx() and imagesy() grab dimensions of a GD Image resource.