SDwarfs
SDwarfs

Reputation: 3239

Clean solution to set DPI on created PNG images

I would like to create PNG images using PHP on a website. These shall be printed at a defined scale. So I would like to set the DPI value of the images using PHP directly. Unfortunately I did not find any function call for this.

Is there any function that can set/update metadata of PNG files? Maybe an other solution is more reasonable as using a HTML-Wrapper with CSS style sheet for printing which externally defines the resolution. But I would prefer the "directly on the image" approach...

Upvotes: 3

Views: 2239

Answers (3)

soulmare
soulmare

Reputation: 83

The easiest way is to use ImageMagick, as suggested in this answer. If You want to set PNG resolution in pure PHP, you may look at my answer to the similar question.

Upvotes: 0

leonbloy
leonbloy

Reputation: 75936

THe pHYs chunk (Physical resolution) lets you set a DPI (well, actually pixels by meter, but it's just a unit conversion). Of course, the PNG reader might ignore it.

PHP does not include (AFAIK) support for reading/writing full PNG metadata, you must do it yourself, see eg

Upvotes: 1

Homer6
Homer6

Reputation: 15159

PNGs can contain arbitrary headers. If you look at the PNG specification, you can add tEXt blocks (which are called chunks) to a given PNG. See section 4.2.3 of the specification for more information on tEXT chunks.

As an example, Adobe Photoshop adds meta XML to its PNGs. I'm not sure if GD supports this, but I'd look there to start. It's definitely possible.

Here is some PHP code that deals with parsing PNG chunks. It might steer you in the right direction. http://code.svn.wordpress.org/imagelibs/libpng.php

Here's an screenshot for a text editor of a PNG, showing the XML that was generated by Photoshop. https://stackoverflow.com/a/14356339/278976

Upvotes: 2

Related Questions