加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何从PHP读取PNG元数据?

发布时间:2020-12-13 16:24:23 所属栏目:PHP教程 来源:网络整理
导读:这是我到目前为止 ?php$file = "18201010338AM16390621000846.png";$test = file_get_contents($file,FILE_BINARY);echo str_replace("n","br",$test);? 输出是我想要的,但我真的只需要3-7行(包括).这是现在的输出:http://silentnoobs.com/pbss/collector/
这是我到目前为止
<?php

$file = "18201010338AM16390621000846.png";

$test = file_get_contents($file,FILE_BINARY);

echo str_replace("n","<br>",$test);

?>

输出是我想要的,但我真的只需要3-7行(包括).这是现在的输出:http://silentnoobs.com/pbss/collector/test.php.我正在尝试从“PunkBuster屏幕截图(±)AAO桥梁交叉”获取数据到“结果:w = 394 X h = 196 sample = 2”.我认为读取文件是相当简单的,并且将每一行存储在数组中,行[0]将需要是“PunkBuster屏幕截图(±)AAO Bridge Crossing”等等.所有这些行都可以改变,所以我不能只搜索有限的东西.

我已经尝试了几天了,这对我在PHP很穷的帮助不大.

PNG file format定义了一个PNG文档被分割成多个数据块.因此,您必须浏览您想要的大块.

您要提取的数据似乎在text块中定义.我写了下面的类来允许你从PNG文件中提取块.

class PNG_Reader
{
    private $_chunks;
    private $_fp;

    function __construct($file) {
        if (!file_exists($file)) {
            throw new Exception('File does not exist');
        }

        $this->_chunks = array ();

        // Open the file
        $this->_fp = fopen($file,'r');

        if (!$this->_fp)
            throw new Exception('Unable to open file');

        // Read the magic bytes and verify
        $header = fread($this->_fp,8);

        if ($header != "x89PNGx0dx0ax1ax0a")
            throw new Exception('Is not a valid PNG image');

        // Loop through the chunks. Byte 0-3 is length,Byte 4-7 is type
        $chunkHeader = fread($this->_fp,8);

        while ($chunkHeader) {
            // Extract length and type from binary data
            $chunk = @unpack('Nsize/a4type',$chunkHeader);

            // Store position into internal array
            if ($this->_chunks[$chunk['type']] === null)
                $this->_chunks[$chunk['type']] = array ();
            $this->_chunks[$chunk['type']][] = array (
                'offset' => ftell($this->_fp),'size' => $chunk['size']
            );

            // Skip to next chunk (over body and CRC)
            fseek($this->_fp,$chunk['size'] + 4,SEEK_CUR);

            // Read next chunk header
            $chunkHeader = fread($this->_fp,8);
        }
    }

    function __destruct() { fclose($this->_fp); }

    // Returns all chunks of said type
    public function get_chunks($type) {
        if ($this->_chunks[$type] === null)
            return null;

        $chunks = array ();

        foreach ($this->_chunks[$type] as $chunk) {
            if ($chunk['size'] > 0) {
                fseek($this->_fp,$chunk['offset'],SEEK_SET);
                $chunks[] = fread($this->_fp,$chunk['size']);
            } else {
                $chunks[] = '';
            }
        }

        return $chunks;
    }
}

您可以使用它来提取您想要的tEXt块:

$file = '18201010338AM16390621000846.png';
$png = new PNG_Reader($file);

$rawTextData = $png->get_chunks('tEXt');

$metadata = array();

foreach($rawTextData as $data) {
   $sections = explode("",$data);

   if($sections > 1) {
       $key = array_shift($sections);
       $metadata[$key] = implode("",$sections);
   } else {
       $metadata[] = $data;
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读