在 PHP 中压缩 GIF 图像质量?

发布于 2024-09-24 01:02:47 字数 240 浏览 6 评论 0原文

如何在 PHP5 中压缩 GIF 图像文件?

我知道可以像这样使用 JPG imagejpeg($resource, $filename, $quality) 根据 http://us.php.net/manual/en/function。 imagejpeg.php

How would one compress GIF image file in PHP5 ?

I know that its possible to do with JPG like this imagejpeg($resource, $filename, $quality)
according to http://us.php.net/manual/en/function.imagejpeg.php

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

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

发布评论

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

评论(2

━╋う一瞬間旳綻放 2024-10-01 01:02:47

Gif 肯定是有损的,而且你绝对可以压缩它们——相当显着。这是 PHP 代码:

$img = imagecreatefromstring(file_get_contents($_FILES["my_field"]["tmp_name"]));
imagetruecolortopalette($img, false, 16);  //  compress to 16 colors in gif palette (change 16 to anything between 1-256)
imagegif($img, $destination_filename);  //  $destination_filename is the location on your server where you want to save the compressed gif file

感谢 Mario,从上面的第一行链接: 在 PHP 中将 JPG/GIF 图像转换为 PNG?

许多人声称 gif 是无损的,这是不正确的。 GIF 肯定会丢失数据。丢失发生在保存时,而不是在文件打开时,并且 gif 丢失与 jpeg 丢失的行为不同,这就是人们感到困惑的地方。一点点道理也可以告诉我们,几乎所有不是位图的图像类型确实是有损的,否则我们最好只使用位图。除了基本的理解之外,图像压缩并不是所有这些专家“博主”所提倡的精确科学,程序员最好自己深入研究数据格式和压缩算法(双关语)。

本文 http://searchcio-midmarket.techtarget.com/definition/ lossless-and-lossy-compression 平淡且错误地指出“图形交换文件 (GIF) 是一种在 Web 上使用的提供无损压缩的图像格式”。这就像说“所有矩形都是正方形”。有些矩形肯定是正方形,但从逻辑上来说,这种说法是 100% 错误的。如果您有黑白图像,当然可以使用无损 gif 图像,但 gif 绝不是“无损”。此外,以 100% 质量保存 jpeg 文件也是无损的,但会创建比原始文件更大的文件。

作为一个非常简单的规则:为了高速传输,请使用带有小调色板的 gif 来获取高对比度图像(例如白色桌子上的黑色笔记本的图像);在对比度非常低的图像(例如森林图像)上使用 50%-70% 之间的 jpeg;并在中等对比度或混合对比度图像(例如您和您朋友的图像)上使用大调色板的 gif 或质量为 63%-85% 的 jpeg。

LZW 压缩(对于 Tiff 文件)是惊人的,但请记住,压缩图像并最终得到一个翻译字典,除了压缩数据本身之外,它还比原始图像更大。图像压缩非常复杂,没有一种方法适合所有情况,因此在讨论图像压缩时避免笼统的陈述。没有“正确”的选择,只有更好的选择。

Gifs certainly are lossy and you absolutely can compress them - quite significantly. Here is the PHP code:

$img = imagecreatefromstring(file_get_contents($_FILES["my_field"]["tmp_name"]));
imagetruecolortopalette($img, false, 16);  //  compress to 16 colors in gif palette (change 16 to anything between 1-256)
imagegif($img, $destination_filename);  //  $destination_filename is the location on your server where you want to save the compressed gif file

Thanks to Mario, from this link for the first line above: Convert JPG/GIF image to PNG in PHP?

Many people are claiming gifs are lossless, which is not correct. Gifs most certainly can lose data. Loss occurs at the point of save, not at the point of file open, and gif loss behaves differently jpegs loss, which is where people are getting confused. A little bit of reason would also tell that almost EVERY image type that is not a bitmap is indeed lossy, otherwise we may as well just use the bitmap. Beyond that basic understanding, image compression is not the exact science that all these expert "bloggers" are promoting, and programmers will do well to study the data formats and compression algorithms in depth (pun intended), and on their own.

This article http://searchcio-midmarket.techtarget.com/definition/lossless-and-lossy-compression flatly and falsely states "the Graphics Interchange File (GIF) is an image format used on the Web that provides lossless compression." That's like saying, "all rectangles are squares". Some rectangles are certainly squares, nevertheless, the statement is, by logic, 100% false. If you have a black and white image, you can certainly get away with a lossless gif image, but gifs are by no means "lossless". Furthermore, saving a jpeg file at 100% quality is also lossless, but can create a larger file than the original.

As a VERY oversimplified rule: for high speed transfer, use gifs with small palettes for high contrast images (like an image of a black notebook on a white table); use jpegs between 50%-70% on very low contrast images (like an image of a forest); and use gifs with large palettes or jpegs with 63%-85% quality on medium contrast or mixed contrast images (such as images of you and your friends).

LZW compression (for Tiff files) is amazing, but keep in mind it is possible to compress an image and end up with a translation dictionary that, in addition to the compression data itself, is bigger than the original image. Image compression is very complex and no one size fits all, therefore avoid blanket statements when discussing image compression. There's no such thing as the "right" option, only better options.

不语却知心 2024-10-01 01:02:47

JPG 是有损压缩。这意味着您可以使用“质量参数”来调整尺寸/质量比。

GIF是无损压缩,无法通过调整质量来获得更好的压缩效果。

要创建 GIF 图像,请使用 imagegif(...)
http://us.php.net/manual/en/function.imagegif.php

JPG is a lossy compression. This means that you can use "quality parameter" to adjust size / quality ratio.

GIF is a lossless compression, you cannot get better compression by adjusting quality.

To create a GIF image use imagegif(...)
http://us.php.net/manual/en/function.imagegif.php

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