• Is there a way to get WP to just resize an image but not apply any optimisation?

    As we know WordPress applies a default image compression on all images uploaded to create the different sizes as set for your WP image sizes, e.g. 1200, 600 and 300. From what I am reading that is 82%.

    I use TinyJPG to optimise my files (manually not a WP plugin) before I add them to WP, and hence the optimisation is done and no further optimisation is required.

    Since I do not want to have any further image compression done by WP I placed this in my functions file.

    add_filter( 'jpeg_quality', 'my_custom_jpeg_quality' );
    function my_custom_jpeg_quality( $quality ) {
      return 100;
    }

    From what I could tell at the time adding this, it overrides the default WP 82% image compression and uses 100%, which I assumed was to not apply any further compression?

    However when I do this the created images sizes, 1200, 600 and so on are bigger than the size of the original, they are the required dimensions but the file sizes are much bigger.

    For example I have a photo 1400 x 1100 pixels, I then optimise that using TinyJPG (manually not a plugin). The optimised file goes from the original 1.1MB to 165KB, still at the same 1400 x 1100 pixels. Awesome :).

    I then upload that file to my WP site (WooCommerce Product). It then goes and creates the set image sizes, e.g. 1200, 900 and 300.

    The problem I have is that the created WP images are resized but they are larger file sizes than the original, and by a big margin. e.g the 1200 is 689KB, 900 is 443KB … This is after I have already optimised the image via TinyJPG.

    Why does it do this and how can I get it to resize based on the original file compression, as in be smaller in size than the original file which would make more sense.

    Thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Yui

    (@fierevere)

    永子

    WordPress does not do compression by itself, nor trying to optimize.
    It will just pass quality as parameter to PHP extensions
    Imagick (if available)
    or GD
    they will use standard system libjpeg internally.

    So if you have used special tools to optimize your images, file size will definitely grow, when compared to optimize tools.
    So does the “simple resize” too.

    You can use plugins that will apply optimizations to images (by passing them to external tools or services), or you can simply run optimization on your media library with tools like leanify or guetzli (it will screw metadata on sizes, but it shouldnt cause any problems).

    Thread Starter GregW

    (@gwmbox)

    Thanks for the reply.

    Why do the sizes grow seeing as the original is a smaller size. Does it upscale (to what) and then resize and compress with the PHP extensions? Just trying to understand the logic/process that it uses.

    I was trying to avoid using another plugin and wanted to optimise pre adding to the site rather than after.

    If I leave the jpeg quality to WP standard 82% it works fine. So I am assuming by what you have said that WP via the PHP extensions calculates the difference of WP setting of 82 to 100 and upscales to the 100% as that is what I had in my functions file (or I’m not understanding).

    So is there a way i can get the php imagick or GD to only resize the image based on WP settings?

    BTW what is better Imagick or GD. If I have both available, how do I set it to use one or the other?

    Moderator Yui

    (@fierevere)

    永子

    Each JPEG format is decoded first to bitmap representation using JPEG decoder (libjpeg),
    needed operations are performed (i.e. resize) by PHP extension (GD/Imagick)
    then bitmaps passed to encoder (libjpeg)

    In fact you can achieve better optimizations with various implementations of libjpeg, i.e. with Mozilla’s fork of libjpeg-turbo (mozjpeg), but still behind specialized tools (like Guetzli)

    Imagick is better. If you have Imagick, then WordPress will automatically use it for WP Image Editor class, some plugins may do the same or let you to choose. Some plugins can only work with GD.

    GD offers very basic operations with worse quality for internal pixel representation. It also does not support EXIF.
    Images processed by GD will be
    1. worse quality
    2. Stripped EXIF metadata (if thats been present in original)
    3. Slightly smaller filesize (with same quality settings, or 100)

    ImageMagick has advanced API for image processing, while PHP extension does not expose full power of ImageMagick to PHP, its still much better than basic GD,
    WordPress is using small portion of available functions, enough for WP Image Editor class to be functional. Some plugins (like Watermark) may utilize more functions.
    ImageMagick internal image representations can be (defined at buildtime)
    Q8 (8 bit per color channel) – low, same as GD, can be built, but i havent seen that anyone does that for distribution.
    Q16 (16 bit) – allows high precision image processing. Good tradeoff between high quality and speed. Default.
    Q32 (32 bit per color channel for each pixel) – Ultra quality. Slow.
    Imagick also can support HDR for some image formats that support it (Like TIFF)
    Imagick supports MultiProcessing and GPU-accelerated operations.
    Images processed by Imagick (PHP Imagick)
    1. Better quality
    2. Keep EXIF metadata (have changed metadata, that reflect processed image)
    3. When passed to encoder (libjpeg) may have slightly bigger filesize due to higher bitmap quality.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Stop WordPress Image Compression’ is closed to new replies.