Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Farai Gandiya
Farai Gandiya

Posted on • Originally published atcodelab.farai.xyz on

How I Optimize PNGs

How I Optimize PNGs was first published onFarai's Codelab.


Here’s how I optimize images in three steps:

  1. Resize the image usingImageMagick to the desired sizes. The size I usually convert to is a 240px width image for small screen devices likefeature phones that fit the media query of(max-width: 400px) and (-webkit-device-pixel-ratio: 1).
    • If it’s a non-photogenic image inside a JPEG, I convert the image sizes to PNG using it.
    • If I’m converting an SVG, I useInkscape (specificallyinkscape-cli) since it’s very good at rendering SVGs.
  2. UseCompressPNG.com to restrict the color palette as much as I can using a slider. This makes a massive difference in file sizes.
  3. Finally, I thenuseZopfliPNG to recompress the PNG for a slight size reduction. PNGs use some sort of DEFLATE compression algorithm to compress all the image data—Zopfli is avery good, but slow implementation of DEFLATE. It’s a ~7% reduction which is just a handful of kilobytes.
  4. Depending on the file size reduction and quality, I might alsoconvert it to WebP or AVIF. This isn’t as common since PNGs ae really good at non photographic images.

This process is based on my naive understanding of how PNGs work. I want to make a comprehensive post on how PNGs work and on how to use that understanding to better compress PNGs. There’s a lot about the compression techniques, color indexing, sampling and the like.

Glossary

non-photographic image—not a photo like illustrations, graphics and diagrams.


Thanks for reading! If you liked this post, considersupporting my work by:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

¯\_(ツ)_/¯
  • Location
    Harare, Zimbabwe
  • Education
    B.A in Computer Science from Wartburg College
  • Joined

More fromFarai Gandiya

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp