views:

1437

answers:

4

Calling all PHP gurus!

I understand that you can use getimagesize() to get the actual pixel height and width of an image in PHP. However, if you open an image in photoshop and look at the image size dialog, you notice that there is a resolution value that determines the print size of the image.

Given an arbitrary jpg image file, I need to use PHP to determine this resolution number. It appears that this information is stored in the jpg file somewhere, so how do I get to it?

One other requirement - I only have gdlib available to me. I need to do this without the use of other php libraries (imagemagick, etc.)

Thanks for the help!

+3  A: 

You could just read the JPEG file directly, bytes 14-18 specify:

  • byte 14: 01, X and Y density unit specifier (00: none, pixel ratios, 01: DPI,02: DPC)
  • bytes 15-16: horizontal pixel density,
  • byte 16-18: vertical pixel densit

Also see: http://www.obrador.com/essentialjpeg/headerinfo.htm

That is some fantastic information. Now how can you read this information using PHP?
Rafe
A: 

Depending on how the image is saved, EXIF contains a metric crapload of information - Read more about it in the PHP manual. You may need to parse/process the results a bit, however (e.g. the flash info is, or at least has been, just a byte, expressing various states).

Henrik Paul
I don't think EXIF contains resolution / density information. Plus, the php EXIF requires php to be compiled w/ a the exif lib, which conflicts w/ the requirements specified in my question.
Rafe
A: 

SOLUTION: User the PHP JPEG Metadata Toolkit - downloaded from here: http://www.ozhiker.com/electronics/pjmt/

This toolkit has some handy scripts that will do all sorts of things, including viewing and editing of the header, metadata, and jfif information in jpeg file. Here is a script that gives you the XDensity and the YDensity (the x and y print resolution) of a jpg:

<?php

include_once("./JPEG.php");
include_once("./JFIF.php");

$image_header = get_jpeg_header_data("./myImage.jpg");
$image_info = get_JFIF($image_header);

print( "XDensity:" . $image_info['XDensity'] . "<br />");
print( "YDensity:" . $image_info['YDensity'] . "<br />");

?>
Rafe
+1  A: 

I don't understand this. Pixels = printsize x resolution, and the number of pixels is a set value. So, if you have an image of 300x300 pixels, you have 1"x1" of 300 DPI resolution, 2"x2" of 150 DPI resolution, or 4"x4" of 75 DPI resolution etc. An image doesn't have a resolution unless it has a physical size to compare to its pixel size.

What is it I'm missing? (and just how glaringly obvious is it to everyone else? =] )

JoeBloggs
Exactly Joe. The XDensity and YDensity are the values I was seeking. Together with the height and width in pixels, they define the print resolution of the image.
Rafe
Also, to clarify, JPEG images have an optional JFIF metadata section that can include the xdensity and ydensity. If this information is not present, then graphics apps typically default to 72 dpi resolution, but you always have the option to change this value, at least in photoshop.
Rafe
I think you miss JB's point. You have X pixels, that is set in stone. If you have a certain print size also set in stone, you can work out your resolution and you cannot increase it from that. If you have a resolution set in stone, you can work out your print size and you cannot increase that either
jTresidder