views:

426

answers:

3

When creating images using the GD library in PHP ie) imagecreatetruecolor() what is the DPI of the resulting image? I haven't been able to find anyone specifying where the dpi can be set or what it defaults to.

I require a 300DPI tiff or jpeg to be created and then saved to the file system from the program.

If this isn't possible using the GD library, is there another that would work for this situation?

Thanks for your help

Edit: Yes this is creating an image - I would like to have a 300dpi file saved from the script not have to open up each file in photoshop to specify the dpi.

+2  A: 

I believe its always 72dpi. So you should multiply your pixel dimensions acoordingly to produce the desired resolution image.

prodigitalson
Thank you, now by creating a larger image, I will still have to open them up in some editing program to change. Do you know of a way to change the dpi setting in php?
You would need to open them up in an image program or as Adam said above, you would need to modify the necessary bytes of the file.
prodigitalson
+2  A: 

I don't think there's a way of setting DPI with GD. The DPI of an image is specified in the leading bytes of the image data - I believe for JPEG images that's bytes 15-18. Bytes 15-16 are horizontal DPI, 17-18 vertical. The values are stored as octals.

I'm a bit ropey with byte-level editing, but you could resize the image in GD to the target pixel size and then edit the file to adjust the DPI.

adam
But hes not opening existing files i dont think he creating files from scratch - meaning they would have some sort of default - right? Also out of curiosity does GD expose an interface directly to edit by bytes (im too lazy to investigate ATM :-))? Also it should be noted this will most likely be different for all formats he wishes to support.
prodigitalson
It's the same whether he creates from scratch or resizes an exisiting, except the initial dpi of an existing image might not always be the same. GD doesn't have byte-level editing, and yes the byte layout will be different - hence why I gave JPEG as my example.
adam
@adam, thanks for the information. Are there any ways using php to edit this information?
@razass:use `fopen` and `fseek` to the necessary bytes then write the dpi info as expected by the file format and write out.
prodigitalson
Great, I found a resource for the JPEG information, adam was correct with his byte locations - 01 2C = 300DPI and 00 48 = 72DPI. I can't find any information like this for TIFF files, does anyone have any information on the byte locations for that format?
http://partners.adobe.com/public/developer/tiff/index.html
prodigitalson
A: 

i think it's 96 dpi always

witek