Check DPI with PHP ImageMagik?

is it possible to check the DPI (dots per inch) of the image with php. If yes, how?


I’m not sure but isn’t the DPI stored in the exif meta data of an image file (jpg)? If you could read these our you might find some info on the DPI.

Images don’t carry any DPI information.

One very important thing you have to remember is that DPI is absolutely not a measure of image size. This is probably the most often made mistake. People will say: " my file is 72dpi and I have enlarged it to 300dpi". But changing dpi has nothing to do with the size of the file ! If the file you started with was 1000 by 2000 pixels, it will be exactly 1000 by 2000 pixels after changing the dpi setting. Regardless of the dpi setting, whether 10dpi or 10000dpi, the file will remain exactly 1000 by 2000 pixels. So the number of pixels is a measure of size for a digital image, dpi is not. DPI is a measure for resolution, Dots Per Inch. Once you realize this, things become much simpler. It’s easy to see that DPI is a measure of resolution. The more dots per inch your (printed) image has, the sharper it will appear.

ok, thanks for the great info. so, my client asked me to create a php script that checks the size in pixels and dpi, so it’s ok to tell him that a dpi check is not possible?


Yes, because DPI doesn’t exists within digital space it really only is used during printing.

Not strictly true. I did some digging years ago when confronted by a similar issue and posted some information on this here:
(in this case i needed to generate output from a business card generator that could be printed at 300dpi - by reading instead of patching the necessary bytes you should be able to determine dpi)

hope thats helpful


If you wanted to print at 300 DPI what you should have done is just increase the width and height of the pixels.

Standard 8x11 piece of paper and you have an image thats 120x200 to print at 300 just a simple calculation…
(8x120)x(11X200) = 960x2200 The image should be around those deminsions to effectively to print at 300

Not entirely true, not ALL images carry resolution data, if the image was taken with a digital camera that writes the exif data to the image file (which almost all digital cameras do these days) the resolution is available in the exif data too.

But i can imagine this is not always present so not a waterproof solution in this case.

I don’t think you understand - The generated output image was of course the correct resolution (in this case 1004 x 650 pixels), it wasn’t a case of resizing anything (the output was generated at the correct pixel dimensions for the required print size at 300dpi)- the problem was that the end users (non-technical) would open the outputted jpeg image in e.g photoshop to print - and the application would proceed to print it out at the wrong size (as by default a gd generated image is shown in photoshop as 72dpi). If you correctly set the dpi meta data within the file (which can’t be done directly via php gd), photoshop will now open the file and print it at the correct size without any user intervention.

I might add that in the official iso standard for jpeg images, dpi is part of the included file specification - hence is always present whether or not the exif data contains it.

If you have a look in

In the frame header the following are defined:
(1 byte)
Units for the X and Y densities.
units = 0: no units, X and Y specify the pixel
aspect ratio
units = 1: X and Y are dots per inch
units = 2: X and Y are dots per cm
(2 bytes)
Horizontal pixel density
(2 bytes)
Vertical pixel density

Out of interest this is the information you can get from an image with Imagemagick. The DPI is down as the resolution 72x72.

Image: …/code/sunflower.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 400x300+0+0
Type: TrueColor
Endianess: Undefined
Colorspace: RGB
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Channel statistics:
Min: 9 (0.0352941)
Max: 255 (1)
Mean: 167.555 (0.657078)
Standard deviation: 61.2115 (0.240045)
Min: 0 (0)
Max: 255 (1)
Mean: 127.962 (0.501814)
Standard deviation: 63.0206 (0.247139)
Min: 0 (0)
Max: 255 (1)
Mean: 30.7509 (0.120592)
Standard deviation: 66.3264 (0.260104)
Colors: 44713
Rendering intent: Undefined
Resolution: 72x72
Units: PixelsPerInch
Filesize: 37.5371kb
Interlace: None
Background color: white
Border color: rgb(223,223,223)
Matte color: grey74
Transparent color: black
Page geometry: 400x300+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 85
Orientation: TopLeft
Exif: DateTime: 2006:07:29 16:10:30
Exif:Model: FinePix E550
Exif:Orientation: 1
Exif:ResolutionUnit: 2
Exif:Software: Digital Camera FinePix E550 Ver1.00
Exif:XResolution: 72/1
Exif:YCbCrPositioning: 2
Exif:YResolution: 72/1
Jpeg:colorspace: 2
Jpeg:sampling-factor: 2x2,1x1,1x1
Signature: 55c4ad08f3ff13d0df50b0e5200e98216a291c6a2c666cb4717c19fc9dcf289e
Unknown: 251
Profile-exif: 8064 bytes
Tainted: False
Version: ImageMagick 6.3.4 05/11/07 Q16

don’t know about the checking, but i just converted one image from 180 DPI to 300 DPI, the size in pixels rest the same, but the document size (which i believe in photoshop is print size is changed almost double)
using command line tool, i’ll try to implement this in php with exec().

convert -strip -density 300x300 -units PixelsPerInch test.jpg