简体   繁体   English

使用PHP获取PNG类型

[英]Using PHP get png type

In a linux console if you use identify -verbose file.png it gives you a full print out of the file. 在Linux控制台中,如果使用identify -verbose file.png它将为您提供文件的完整打印。 Is there anyway to get that same information in php? 反正有没有得到相同的信息在PHP?

Specifically I need the "Type" line which states the type of png is it. 具体来说,我需要“类型”行来说明png的类型。 TrueColorAlpha, PaletteAlpha, ect.. TrueColorAlpha,PaletteAlpha等

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. 操作系统损坏,并试图重建超过500万张图像的结构,其中200万张图像被丢弃并丢失。 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. 如果我能够找到两者之间的区别,那将节省大量时间。

From these articles I wrote a simple function than can give you the color type of a PNG file: 从这些文章中,我编写了一个简单的函数,可以为您提供PNG文件的颜色类型:

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

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

In short: A PNG file is composed of a header and chunks. 简而言之:PNG文件由标题和块组成。 In the header from second the forth byte should be ASCII string equal to "PNG", then the chunks which names are 4 bytes follow. 在第二个标头中,第四个字节应为等于“ PNG”的ASCII字符串,然后是名称为4个字节的块。 The IHDR chunk gives you some data about the image like with, height and the needed color type. IHDR块为您提供有关图像的一些数据,如高度,高度和所需的颜色类型。 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. IHDR块必须首先出现。 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. 因此,知道标题的长度,块名称的长度及其结构,我们就可以计算出颜色类型数据的位置,它是26个字节。 Now we can write a simple function that reads the color type of a PNG file. 现在我们可以编写一个简单的函数来读取PNG文件的颜色类型。

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. 我希望这有帮助。

Do it with Bash code in your PHP Executing a Bash script from a PHP script 使用PHP中的Bash代码执行此操作从PHP脚本执行Bash脚本

<?php
      $type=shell_exec("identify -verbose $filename");
      print_r($type);
 ?>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM