在linux中,如果您使用identify -verbose file.png,它将为您提供文件的完整打印。在php中是否存在相同的信息?
具体来说,我需要“类型”行,它说明png的类型是它。TrueColorAlpha,PaletteAlpha,等等。
为什么?操作系统损坏了,在试图重建500多万张图像的过程中,有200万张图像被丢弃到失物招领处。其中一些是系统创建的,有些是上传的。如果我能找到两者的不同之处,这将节省大量的时间。
发布于 2016-05-17 10:53:16
从这些文章中,我编写了一个简单的函数,可以提供PNG文件的颜色类型:
http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html
简而言之: PNG文件由头部和块组成。在第二个字节的头中,第四个字节应该是等于"PNG“的ASCII字符串,然后是名称为4个字节的块。IHDR块为您提供一些有关图像的数据,如with、高度和所需的颜色类型。这个块的位置总是固定的,因为它总是第一个块。我给你的第二个链接描述了它的内容:
IHDR块必须首先出现。它包括:
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因此,知道标题的长度,块名的长度和它的结构,我们可以计算颜色类型数据的位置,它是26字节。现在,我们可以编写一个简单的函数来读取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);以下是颜色类型命名(来自第二个链接):
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,我希望这能帮到你。
发布于 2016-05-17 08:00:31
在PHP Executing a Bash script from a PHP script中使用Bash代码来实现它
<?php
$type=shell_exec("identify -verbose $filename");
print_r($type);
?> https://stackoverflow.com/questions/37270213
复制相似问题