7

PHP and GD seem to have trouble creating images from PNGs of type greyscale with alpha when using imagecreatefrompng(). The results are incredibly distorted.

I was wondering if anyone knew of a way to test for the colour type in order to notify the user of the incompatibility?

Example:

Original Image: http://dl.dropbox.com/u/246391/Robin.png
Resulting Image: http://dl.dropbox.com/u/246391/Robin_result.png

Code:

<?php

$resource = imagecreatefrompng('./Robin.png');
header('Content-type: image/png');
imagepng($resource);
imagedestroy($resource);

Cheers,

Aron

2
  • What do you mean by distorted? There are certain steps you need to take to preserve alpha transparency. But greyscale issues... that's new to me Commented Jan 13, 2010 at 15:45
  • I've updated my example to include an example image. Preserving alpha transparency is fine. It's specifically greyscale images with transparency that GD can't handle, see haim evgi's answer below.
    – ac94
    Commented Jan 13, 2010 at 16:54

3 Answers 3

12

The colour type of a PNG image is stored at byte offset 25 in the file (counting from 0). So if you can get hold of the actual bytes of the PNG file, simply look at byte 25 (I don't do PHP, so I don't know how to do that):

  • 0 - greyscale
  • 2 - RGB
  • 3 - RGB with palette
  • 4 - greyscale + alpha
  • 6 - RGB + alpha

The preceding byte (offset 24) gives the number of bits per channel. See the PNG spec for more details.

In a slight twist a PNG file may have "1-bit alpha" (like GIFs) by having a tRNS chunk (when it is colour type 0 2 or 3).

0
5

i landed here today searching for a way to tell (via php) if a specific .png image is an alpha-png one -
David Jones' answer points to the right direction, really easy to implement in php:

file_get_contents to load just that 25' byte (there it is, indeed!), and
ord() to get its ASCII value, to test it (against '6' in my case)

if(ord(file_get_contents($alpha_png_candidate, NULL, NULL, 25, 1)) == 6) {
  is_alpha_png_so_do_something();
  }

actually i needed that for assuring backward compatibility with ie6 within cms-user-generated-pages, to replace all alpha-png < img > tags with inline-block < spans > - the alpha-png file will then be served as variable for the ms-proprietary css property filter

.alpha_png_span{
  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
    src='$alpha_png_candidate', sizingMethod='crop')
  }

...and it all works, so thanks!

paolo

1
  • Using (bool) (ord(file_get_contents($alpha_png_candidate, NULL, NULL, 25, 1)) & 4) instead will tell you if there's an alpha channel regardless of whether the image is greyscale or rgb.
    – Walf
    Commented Mar 21, 2014 at 2:46
0

see this answer :

Another usefull note for those using ImageCreateFromPng: PHP and GD do not recognize grayscale/alpha images.

So if you use grayscale images with transparency between 0% and 100%, then save the image as RGB.

At least this is true for PHP Version 4.4.2-1 and in 5.1.2-1 with pictures made with GIMP 2.2.8.

url : http://php.net/manual/en/function.imagecreatefrompng.php

1
  • Thanks, however I'm looking for a way of detecting that the image is greyscale with transparency before trying to create the resource with imagecreatefrompng(). The images are submitted to the site so unfortunately I have no way of re-saving the image as RBG (at least I'm not aware of a method).
    – ac94
    Commented Jan 13, 2010 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.