Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-12-2012, 09:33 PM   PM User | #1
pootlecat
New Coder

 
Join Date: Mar 2006
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
pootlecat is an unknown quantity at this point
Checking PNG files for transparency

Hi,

I found this code today and was really happy, for a short while:
http://stackoverflow.com/questions/2...pha-color-type
Code:
if(ord(file_get_contents($alpha_png_candidate, NULL, NULL, 25, 1)) == 6) {
  is_alpha_png_so_do_something();
  }
The function is supposed to return the following:
0 - greyscale
2 - RGB
3 - RGB with palette
4 - greyscale + alpha
6 - RGB + alpha
But all the PNG files I test return a value of "1".
Does anyone have any ideas?

Thanks!
pootlecat is offline   Reply With Quote
Old 04-13-2012, 04:49 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Meh I should get my environment reinstalled.
Taking a look at the first spec I found here: http://www.libpng.org/pub/png/spec/1...Structure.html 25th byte looks correct. 8 for png header, 4 for chunk length, 4 for chunk type, 4 for width, 4 for height, and finally the bit depth. So lets see, 8+4+4+4+4 = yeah 24 so that's right. Start at 25, read one byte.

What's actually returning 1 that you indicate here? Have you successfully opened the file and read that byte? Given the filestructure, the ord should only return 0, 2, 3, 4, 6; assuming that this structure is correct, 1 is not an option.
Fou-Lu is offline   Reply With Quote
Old 04-13-2012, 02:26 PM   PM User | #3
pootlecat
New Coder

 
Join Date: Mar 2006
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
pootlecat is an unknown quantity at this point
Thanks very much for your reply, I'm pulling my hair out lol
Here is the code I am using:
Code:
function is_alpha_png($fn){
    return (ord(@file_get_contents($fn, NULL, NULL, 25, 1)) == 6);
}
$fin = "00100000001.png";
$trans = is_alpha_png($fin);
echo "trans = $trans <br>";
and it's printing out "trans = 1".
This is what the PNG looks like:

If I'm calculating right (its been many years!) it should be returning '6' which would be right...

Last edited by pootlecat; 04-13-2012 at 02:29 PM..
pootlecat is offline   Reply With Quote
Old 04-13-2012, 05:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yes this is correct then.
The result of the function is_alpha_png is returning a boolean. If it returns 0, then it does not have a bit depth of 6. Any other result (typically 1), it does.

PHP Code:
if (is_alpha_png($fin))
{
    print 
'This file has alpha transparency';

If you want to return the bitdepth, pull the ==6 off of the comparison.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
pootlecat (04-13-2012)
Old 04-13-2012, 05:27 PM   PM User | #5
pootlecat
New Coder

 
Join Date: Mar 2006
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
pootlecat is an unknown quantity at this point
Thank you so much - I had a feeling I was missing something simple!
pootlecat is offline   Reply With Quote
Old 04-13-2012, 05:41 PM   PM User | #6
pootlecat
New Coder

 
Join Date: Mar 2006
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
pootlecat is an unknown quantity at this point
Soooo, it's reporting the bitdepth correctly now but (after some testing) I guess a PNG can have an alpha channel and not be transparent so I'm still not much further forward. Can anyone confirm this is the case?
pootlecat is offline   Reply With Quote
Old 04-13-2012, 06:14 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by pootlecat View Post
Soooo, it's reporting the bitdepth correctly now but (after some testing) I guess a PNG can have an alpha channel and not be transparent so I'm still not much further forward. Can anyone confirm this is the case?
Best I know you can have an alpha layer and not deal with it it all. It just wastes more space than necessary.
Graphics are not my specialty, but I don't believe that PHP has a builtin way to detect alpha levels in images. This will require you to parse the image according to the spec (which is relatively complex), and scan the alpha channels manually.

Is there a reason why you need to know whether a png contains a transparent layer?
Fou-Lu is offline   Reply With Quote
Old 04-13-2012, 06:28 PM   PM User | #8
pootlecat
New Coder

 
Join Date: Mar 2006
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
pootlecat is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Best I know you can have an alpha layer and not deal with it it all. It just wastes more space than necessary.
Graphics are not my specialty, but I don't believe that PHP has a builtin way to detect alpha levels in images. This will require you to parse the image according to the spec (which is relatively complex), and scan the alpha channels manually.

Is there a reason why you need to know whether a png contains a transparent layer?
It is something our customers always want to know and I was really hoping I could find an easy way to check a PNG and let them know.
Thanks!
pootlecat is offline   Reply With Quote
Old 04-13-2012, 08:17 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Then this will suffice.
The alternative is to go through the entire image and check each pixel to see if the alpha channel is opaque. Just going through the GD, it does appear that this can be done with PHP GD library.
PHP Code:
function hasAlphaColour($r)
{
    
$bResult false;
    
$x imagesx($r);
    
$y imagesy($r);
    for (
$i 0$i $x && !$bResult; ++$i)
    {
        for (
$j 0$j $y && !$bResult; ++$j)
        {
            
$index imagecolorat($r$i$j);
            
$colors imagecolorsforindex($r$index);
            if (isset(
$colors['alpha']) && $colors['alpha'] > 0)
            {
                
$bResult true;
            }
        }
    }
    return 
$bResult;
}

if (
is_alpha_png($fin))
{
    
$r imagecreatefrompng($fin);
    if (
hasAlphaColour($r))
    {
        print 
'has at least one color with alpha';
    }
    
imagedestroy($r);

Looks like would work; no clue if it actually does.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:04 PM.


Advertisement
Log in to turn off these ads.