Autogenerated Thumbnail compression depending on size

This is a follow up to this question, in witch the jpg compression is altered depending on what WP’s built-in image resulting image size returns. While it may work for most of the people, i’m looking for a more discrete and automatic approach. And that is altering the wp_create_thumbnail i think.

The point is this: How to alter the JPG compression of autogenerated thumbnails for an uploaded image? For example, images below 400px width at 60%, and between 401px~100px width at 80%.

1 Answer
1

i’m looking for a more discrete and automatic approach. And that is altering the wp_create_thumbnail i think.

And that is where you’d be wrong. Here is the entire code for wp_create_thumbnail() from core:

function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '1.2' );
    $thumbpath = image_resize( $file, $max_side, $max_side );
    return apply_filters( 'wp_create_thumbnail', $thumbpath );
}

This function, on its own, doesn’t do much. Instead, if you need to change anything, it would be image_resize() … but changing the core function is still the wrong way to do this.

Why Not Change Core

Changing a core file is usually frowned upon by mainstream developers. If you change core and don’t resubmit your changes to the project, you’re left with a forked version of WordPress – the next time an update is released, you’ll have to re-edit core files after you update to maintain your new functionality.

In many cases, the changes you want to make to core only apply to a handful of people – typically just you. Unless the changes will benefit a majority of WP users, any core changes outside the scope of the current development cycle will usually be tabled for later or left in Trac with a “wontfix” resolution.

What works for just you or just me doesn’t belong in the core project – it belongs in a plugin.

The Right Way

The right way to change things up is to create your own version of image_resize() in a plugin. Then build your own my_create_thumbnail() function that fits the following:

function my_create_thumbnail( $file, $max_side ) {
    return apply_filters( 'wp_create_thumbnail', my_image_resize( $file, $max_side, $max_side ) );
}

This function has the same signature as wp_create_thumbnail() and fires the same filters, so it can be used as a one-to-one replacement of the original function in all of your other plugin and theme scripts.

Leave a Comment