Compress GIF Images Quality in PHP?
How would one com开发者_JAVA技巧press 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
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.
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
精彩评论