0

In a linux console if you use identify -verbose file.png it gives you a full print out of the file. Is there anyway to get that same information in php?

Specifically I need the "Type" line which states the type of png is it. TrueColorAlpha, PaletteAlpha, ect..

Why? Operating system corrupted and in attempting to rebuild a structure of more than 5 million images 2 million of them were dumped into lost and found. Some of them are system created and some of them were uploaded. If I am able to find a difference between the two, it would save tremendous amounts of time.

Case
  • 4,244
  • 5
  • 35
  • 53
  • 3
    You could do a `$output = shell_exec("identify -verbose $filename")` and get the same result as if you were running `identify` from the terminal. – apokryfos May 17 '16 at 07:54
  • You are absolutely correct. I do know about exec, but I was wondering if there was another way based on things I am already having to do. If not that's fine and that's what I'll go with. – Case May 17 '16 at 07:58
  • Well I think you'll have to go through more trouble doing it purely with PHP then executing some bash code. – Strah Behry May 17 '16 at 07:58
  • shell_exec while of course it gives me the information for a human to read. I need then to have access to run a switch based on types. – Case May 17 '16 at 08:04

2 Answers2

1

From these articles I wrote a simple function than can give you the color type of a PNG file:

https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header

http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html

In short: A PNG file is composed of a header and chunks. In the header from second the forth byte should be ASCII string equal to "PNG", then the chunks which names are 4 bytes follow. The IHDR chunk gives you some data about the image like with, height and the needed color type. This chunk's position is always fixed as it is always the first chunk. And it's content is described in the second link I gave you:

The IHDR chunk must appear FIRST. It contains:

   Width:              4 bytes
   Height:             4 bytes
   Bit depth:          1 byte
   Color type:         1 byte
   Compression method: 1 byte
   Filter method:      1 byte
   Interlace method:   1 byte

So knowing the length of the header, length of the chunk name and it's structure we can calculate the position of the color type data and it's the 26 byte. Now we can write a simple function that reads the color type of a PNG file.

function getPNGColorType($filename)
{
    $handle = fopen($filename, "r");

    if (false === $handle) {
        echo "Can't open file $filename for reading";
        exit(1);
    }

    //set poitner to where the PNG chunk shuold be
    fseek($handle, 1);
    $mime = fread($handle, 3);
    if ("PNG" !== $mime) {
        echo "$filename is not a PNG file.";
        exit(1);
    }

    //set poitner to the color type byte and read it
    fseek($handle, 25);
    $content = fread($handle, 1);
    fclose($handle);

    //get integer value
    $unpack = unpack("c", $content);

    return $unpack[1];
}

$filename = "tmp/png.png";
getPNGColorType($filename);

Here is the color type nomenclature (from the second link):

   Color   Allowed     Interpretation
   Type    Bit Depths

   0       1,2,4,8,16  Each pixel is a grayscale sample.

   2       8,16        Each pixel is an R,G,B triple.

   3       1,2,4,8     Each pixel is a palette index;
                       a PLTE chunk must appear.

   4       8,16        Each pixel is a grayscale sample,
                       followed by an alpha sample.

   6       8,16        Each pixel is an R,G,B triple,

I hope this helps.

Pavel Petrov
  • 847
  • 10
  • 19
-1

Do it with Bash code in your PHP Executing a Bash script from a PHP script

<?php
      $type=shell_exec("identify -verbose $filename");
      print_r($type);
 ?>  
Community
  • 1
  • 1
Strah Behry
  • 551
  • 2
  • 8
  • 25
  • Problem with this is an I need programmatic access to this variable. Simply being able to read it in PHP is not helpful for me. – Case May 17 '16 at 08:03
  • "In a linux console if you use identify -verbose file.png it gives you a full print out of the file. Is there anyway to get that same information in php?" This is the answer to your question... Maybe you should edit your question then if it's something else that you want. – Strah Behry May 17 '16 at 08:09
  • You read the comments before posting the answer. You knew what I wanted. – Case May 17 '16 at 08:12
  • I don't understand the issue you can just do identify - verbose $filename | grep "Type" and write the output to a php variable – Strah Behry May 17 '16 at 08:29