Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

How to compress images for WordPress without a plugin

Disclosure: I built Pixlune, and all measurements in this post were made with it. This is a copy of the original guide on pixlune.com. You don't need a plugin to keep images light on a WordPress site. Do the work once,

How to compress images for WordPress without a plugin

Disclosure: I built Pixlune, and all measurements in this post were made with it. This is a copy of the original guide on pixlune.com.

You don't need a plugin to keep images light on a WordPress site. Do the work once, before the file reaches the Media Library: resize the photo to the width your theme shows, compress it, and save it as WebP. The Pixlune image tool does all three in one step with the "Blog content image" preset. Your photos are processed in your browser and are not uploaded to a server.

This guide shows the steps, what WordPress does with the file after you upload it, and how small five real photos got.

Step 1: resize to the width you need

A camera or phone photo is usually far wider than any content column. The "Blog content image" preset limits the width to 1200 pixels and keeps the aspect ratio. Smaller images are never enlarged.

If your theme's content area is narrower or wider, open Advanced settings and change "Max width (px)". For a full-width header image there is a separate preset, "Full-width hero", with a 1920 pixel limit.

Step 2: compress and convert to WebP

The same preset saves the result as WebP at quality 80. According to the WordPress theme handbook, WebP support was added in WordPress 5.8. The WordPress optimization handbook also suggests: "Consider using a more modern image format like WebP which is smaller in size."

If you need JPG instead, pick JPG as the target format. The width limit and quality stay the same.

What I measured

I ran five photos through the preset in Chrome and read the downloaded files: three product photos on plain backgrounds and two detailed scenes. Quality 80, width limited to 1200 pixels.

Bar chart on a log scale: original, JPG and WebP file sizes for five photos

Photo Original WebP JPG
Red mug, 2950 × 2847 1,363 KB 36.8 KB (1200 × 1158) 71.5 KB
Black shoe, 3000 × 2254 1,670 KB 29.8 KB (1200 × 902) 44.6 KB
Glass vase, 4000 × 6000 10,714 KB 12.8 KB (1200 × 1800) 23.0 KB
Lake and forest, 3024 × 4032 3,977 KB 439.8 KB (1200 × 1600) 419.7 KB
Street at night, 6400 × 5846 35,078 KB 294.0 KB (1200 × 1096) 308.6 KB

1 KB = 1,000 bytes. To see what resizing alone does, I also saved the vase photo as WebP at quality 80 without resizing it. At 4000 × 6000 pixels it was 143.9 KB; at 1200 × 1800 it was 12.8 KB, about 11 times smaller.

At the same quality setting, WebP was 33–49% smaller than JPG for the three product photos. For the detailed scenes the gap almost disappeared: the street photo was 5% smaller as WebP, the lake photo 5% larger. Quality numbers are not equivalent between formats, so this compares the setting, not how the images look.

As WebP, the detailed photos came out at 294–440 KB, the product photos at 13–37 KB. Check your own results in the tool's summary before you upload.

What WordPress does after you upload

WordPress makes its own copies of every image. Its developer documentation says: "So WordPress automatically creates several sizes of each image uploaded to the media library." These sizes are used for thumbnails and for responsive images, so the browser can pick a smaller file on a phone.

The sizes are made from the file you upload. By default they keep its format, so a WebP upload gives WebP sizes. A smaller, already compressed upload means every size starts from a lighter file.

For very large uploads WordPress also makes a scaled-down copy. The documentation for this limit says: "The threshold value in pixels. Default 2560." If you resize to 1200 or 1920 pixels before uploading, your image stays under this limit.

You can see and change the size limits in WordPress under Settings → Media. The WordPress help page for that screen describes them as maximum dimensions in pixels.

When a plugin makes sense

Resizing before upload works well for new images. A plugin is the better fit when:

  • the Media Library already holds hundreds of large images and you want to convert them all in place;
  • several people upload images and you can't ask each of them to prepare files first.

Pixlune doesn't change files that are already on your site. It prepares new ones before they are uploaded.

Photos: red mug, Daderot, CC0; black shoe, Wikipedia-ce, CC0; glass vase, Jonas Salcedo, CC0; lake, AutumnNY, CC0; street at night, Wilfredor, CC0; all from Wikimedia Commons.

Sources

Originally published at pixlune.com.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.