Although the PNG format excels at creating small files (generally always smaller than the equivalent GIF file), there are a number of simple rules you can follow to be sure that you are getting the smallest PNG you can.
The PNG file format has a number of compression levels. At the highest level of compression you get the smallest file size, but it takes the longest time to compress/decompress. At the lowest level of compression you get the opposite - speedy compression and decompression but the largest file size. Visually, the highest level and lowest level of compression are precisely the same.
If you can, use paletted PNG files. True-colour PNG files have 16,777,216 colours in there, and if you aren't using them all you are wasting space. If you can restrict your colours to just 256, 128, 64, or even just 2 colours, then you will get much smaller files.
Use a decent program to create your PNGs. PHP has an excellent PNG implementation, as does Corel PhotoPaint and Jasc's Paint Shop Pro. Adobe Photoshop, on the other hand, seems to have an inferior PNG exporter that may well create larger files than necessary, or, worse, use slightly incorrect colours on occasion.
Remember that PNGs are generally not ideal for photographic images - this is what JPEGs are for.
Remember that not everyone who will access your site will have a broadband Internet connection - downloading huge pictures over a 56K modem is painfully slow, and will not win you any friends!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Making graphs >>
Previous chapter: Getting an image's MIME type
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.