在 PHP 中,imagepng() 接受过滤器参数。这些过滤器如何影响函数的输出?

发布于 2024-09-05 23:19:42 字数 476 浏览 3 评论 0原文

这些过滤器如何影响 PHP 中 imagepng() 的输出?

  • PNG_NO_FILTER
  • PNG_FILTER_NONE
  • PNG_FILTER_SUB
  • PNG_FILTER_UP
  • PNG_FILTER_AVG
  • PNG_FILTER_PAETH
  • PNG_ALL_FILTERS

文档只是说,“一个特殊的 PNG 滤镜,由 imagepng() 函数使用”。

似乎使用 PNG_NO_FILTER 会减少输出的文件大小,但除此之外,我不确定它是如何受到影响的。任何见解将非常感激。

How do these filters affect the output of imagepng() in PHP?

  • PNG_NO_FILTER
  • PNG_FILTER_NONE
  • PNG_FILTER_SUB
  • PNG_FILTER_UP
  • PNG_FILTER_AVG
  • PNG_FILTER_PAETH
  • PNG_ALL_FILTERS

The documentation simply says, "A special PNG filter, used by the imagepng() function" for each of them.

It seems that using PNG_NO_FILTER will reduce the filesize of the output, but other than that, I am unsure as to how it is affected. Any insight would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山有枢 2024-09-12 23:19:42

根据 http://www.w3.org/TR/PNG-Filters 上的 PNG 规范.html 这些过滤器的目的是准备图像数据以实现最佳压缩。

使用无过滤器,扫描线为
未经修改地传输;这只是
需要插入过滤器类型字节
数据之前。

子过滤器传输
每个字节之间的差异
相应字节的值
先前的像素。

上过滤器就像下过滤器一样
过滤除像素
紧邻当前像素上方,
而不仅仅是在其左侧,使用
作为预测器。

平均过滤器使用以下平均值
两个相邻像素(左和
上面)来预测 a 的值
像素。

Paeth 过滤器计算一个简单的
三者的线性函数
相邻像素(左、上、上
左),然后选择作为预测变量
最接近的相邻像素
计算值。这项技术是由于
致艾伦·W·佩斯 [PAETH]。*

According to the PNG Specifications at http://www.w3.org/TR/PNG-Filters.html The purpose of these filters is to prepare the image data for optimum compression.

With the None filter, the scanline is
transmitted unmodified; it is only
necessary to insert a filter type byte
before the data.

The Sub filter transmits the
difference between each byte and the
value of the corresponding byte of the
prior pixel.

The Up filter is just like the Sub
filter except that the pixel
immediately above the current pixel,
rather than just to its left, is used
as the predictor.

The Average filter uses the average of
the two neighbouring pixels (left and
above) to predict the value of a
pixel.

The Paeth filter computes a simple
linear function of the three
neighbouring pixels (left, above, upper
left), then chooses as predictor the
neighboring pixel closest to the
computed value. This technique is due
to Alan W. Paeth [PAETH].*

后eg是否自 2024-09-12 23:19:42

从链接到问题的 imagepng() 手册页中,

过滤器

允许减小 PNG 文件大小。它是一个位掩码字段,可以
设置为 PNG_FILTER_XXX 常量的任意组合。
PNG_NO_FILTER >或 PNG_ALL_FILTERS 也可分别用于
禁用或激活所有过滤器。

因此,要让 libpng 尝试 none、sub 和 up 过滤器,您可以使用

PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP

PNG_ALL_FILTERS 只是简写。

PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH

至于使用哪个过滤器,这取决于图像。具有 256 种或更少颜色的图像通常使用 PNG_NO_FILTER 压缩效果更好,而具有多种颜色的图像(例如照片)通常使用 PNG_FILTER_SUB 或 PNG_ALL_FILTERS 压缩效果更好。 “optipng”或“pngcrush”等应用程序尝试优化过滤器选择。如果您打算使用这些第三方应用程序之一进行最终优化,则应该仅对工作副本使用 PNG_NO_FILTERS,以提高速度。

From the imagepng() man page linked to the question,

filters

Allows reducing the PNG file size. It is a bitmask field which may
be set to any combination of the PNG_FILTER_XXX constants.
PNG_NO_FILTER > or PNG_ALL_FILTERS may also be used to respectively
disable or activate all filters.

So, to let libpng try the none, sub, and up filters, you'd use

PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP

PNG_ALL_FILTERS is just shorthand for

PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH

As for which filter to use, it depends upon the image. Images with 256 or fewer colors generally compress better with PNG_NO_FILTER, while images with many colors (such as photos) generally compress better with PNG_FILTER_SUB or PNG_ALL_FILTERS. Applications like "optipng" or my "pngcrush" try to optimize the filter selection. If you're going to use one of those third-party applications for final optimization, you should just use PNG_NO_FILTERS for your working copies, for speed.

止于盛夏 2024-09-12 23:19:42

这些都是 PNG 编码器可以用来确定像素值的不同算法。不太了解,但此页面似乎有些深入: http: //www.w3.org/TR/PNG-Filters.html

Those are all different algorithms the PNG encoder can use to determine pixel values. Don't know a whole lot, but this page seems to go into some depth: http://www.w3.org/TR/PNG-Filters.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文