在PHP中读取PNG元数据的最快方法 [英] Fastest way to read PNG metadata in PHP

查看:108
本文介绍了在PHP中读取PNG元数据的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PNG文件中提取两个字段。即,几何字段和元数据中的一个字段。

I would like to extract two fields from a PNG file. Namely, the geometry field and one of the fields from the metadata.

我能做到这一点的最快方法是什么?我已经对我当前执行此操作的脚本进行了基准测试,到目前为止,最慢的操作是在PNG文件上执行实际的ImageMagick识别程序。 (.4秒vs .0001秒解析几何体的输出数组,8.39E-5秒解析元数据中的关键短语)

What is the fastest way I could go about doing this? I have benchmarked my script that currently performs this and by far the slowest action is executing the actual ImageMagick "identify" program on the PNG file. (.4 seconds vs .0001 seconds to parse the outputted array for the geometry and 8.39E-5 seconds to parse key phrases from the metadata)

提前感谢任何帮助,

Jonathan

推荐答案

我对任何人都不熟悉现成的库或类在没有子进程调用的情况下在PHP中完成,但是如果找不到,那么编写自己的库肯定是要走的路。

I'm not familiar with any ready-made libraries or classes to do it in PHP without a subprocess call, but if you can't find one, writing your own would definitely be the way to go.

PNG是一种相当简单的块流格式,因此寻找特定的块并提取一些头字段是微不足道的。

PNG's a fairly simple block stream format, so seeking to a specific block and extracting some header fields is trivial.

所有你需要的是读取和检查8字节的东西 89 50 4E 47 0D 0A 1A 0A PNG标题然后在读取8个字节(块长度加类型)之间交替,并使用长度搜索块,直到您达到所需的块类型。

All you'd need is something which reads and checks the 8-byte 89 50 4E 47 0D 0A 1A 0A PNG header and then alternates between reading 8 bytes (chunk length plus type) and seeking past the block using the length until you hit the chunk type you want.

对于几何,假设PNG遵循规范,这里是它的方式:

For the geometry, assuming the PNG follows the spec, here's how it'd go:


  1. 读取并验证PNG标题(8字节)

  2. 读取并检查第一个块的标题(8个字节)
  1. Read and verify PNG header (8 bytes)
  2. Read and check header of first block (8 bytes)

  1. 成功。 type = IHDR

  2. 为几何(宽度,高度,每个4字节)读取额外的8个字节


  • 如果您想要的其他字段不在 IHDR ,使用步骤2中的块大小来搜索下一个块以搜索您想要的其他字段。

  • If the other field you wanted isn't in IHDR, use the chunk size from step 2 to seek to the next block in search of the other field you wanted.
  • 我可能需要5到15分钟才能用Python鞭打这样的东西。 (我用RAR和GIF做了类似的事情)因为我在使用低级文件I / O的经验较少,所以在PHP中可能有15到25个。

    It'd probably take me 5 to 15 minutes to whip something like that up in Python. (I've done similar things with RAR and GIF) Maybe 15 to 25 in PHP since I've got less experience doing low-level file I/O in it.

    这篇关于在PHP中读取PNG元数据的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆