3

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...

NDM
  • 6,731
  • 3
  • 39
  • 52
SDwarfs
  • 3,189
  • 5
  • 31
  • 53

3 Answers3

2

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

Community
  • 1
  • 1
Homer6
  • 15,034
  • 11
  • 61
  • 81
  • Well, the http://code.svn.wordpress.org/imagelibs/libpng.php code seems to parse the chunks manually. This is not what I was looking for. But, anyway it gives some insight... thx. – SDwarfs Aug 26 '13 at 08:04
  • That's what gd would be doing too. In the end, its just bits. The only question is how fast it will do it. If you're going to do it yourself, I'd highly recommend Hexworkshop if you're using windows. – Homer6 Aug 26 '13 at 08:06
  • 1
    @StefanK. This post seems to demonstrate it as well: http://stackoverflow.com/questions/8842387/php-add-itxt-comment-to-a-png-image – Homer6 Aug 26 '13 at 08:13
  • Hm, ok, the last solution seems to be an option. It's much more compact and seems like maintainable code... – SDwarfs Aug 26 '13 at 08:22
  • 1
    Yes. As far as I know, you can place tEXT chunks anywhere after the IHDR chunk (including where this code places it). As the question states, this is a quick and dirty solution that doesn't check to see if the tEXT chunk already exists. So you might want to use the code above if you have more elaborate requirements. Also keep in mind that tEXT is Latin-1 encoding, where as iTXt is UTF-8. See section 4.2.3.3 of the specification for further details on encoding. – Homer6 Aug 26 '13 at 08:26
  • To set the physical resolution you should not use a textual chunk, that's what the pHYs chunk was standarized for. – leonbloy Aug 27 '13 at 22:49
1

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

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
0

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.

soulmare
  • 73
  • 1
  • 7