Force WordPress to create Thumbnails of same Size as the uploaded Image

AFAIK, WordPress only generates Thumbnails (e.g., ‘large’) only when the desired image size is smaller (and not when the target size would be the same).

I use

Read More

add_image_size( 'referenzen-big', 627, 490, true );

So if my customer uploads a file with 627x490px, the original Image will be used.

But this is not desired, since this customed, in all good faith, uploads the images in the right size, and also highest possible .jpg-compression. This results in images being around 300kB in this case.

A pratical, but technicall not flawless way would be to ask him to upload his images in 628×490, so 1px more width, forcing a rescale. Suggestions like this will not be accepted 🙂

So far, I’ve investigated the hooks responsible for image creation, and tracked the responsible function down; here’s the last hook I found:
image_make_intermediate_size()

this is the function responsible for the actual resizing:
image_resize_dimensions().
There’s even a line saying:

// if the resulting image would be the same size or larger we don't want to resize it
if ( $new_w >= $orig_w && $new_h >= $orig_h )
    return false;

So how can I enable this “force resize” feature?

Related posts

Leave a Reply

7 comments

  1. Here is the solution you are looking for, to be placed in your functions.php file:

    function thumbnail_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ){
        if ( !$crop ) return null; // let the wordpress default function handle this
    
        $aspect_ratio = $orig_w / $orig_h;
        $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
    
        $crop_w = round($new_w / $size_ratio);
        $crop_h = round($new_h / $size_ratio);
    
        $s_x = floor( ($orig_w - $crop_w) / 2 );
        $s_y = floor( ($orig_h - $crop_h) / 2 );
    
        return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    }
    add_filter( 'image_resize_dimensions', 'thumbnail_upscale', 10, 6 );
    

    This is found here:
    https://wordpress.stackexchange.com/questions/50649/how-to-scale-up-featured-post-thumbnail

    It will upscale images and generate thumbnails for all image sizes. The only downfall, which probably isn’t a downfall, is that no matter what you’ll get a separate file for each image size for all uploaded images… even if it’s a small icon or something. Hope this helps.

  2. While still not optimal, wouldn’t it be better to use a serverside library like Timthumb and then provide the original source and use the ‘q’ (= quality) parameter?

    That would optimize the image, with the original size.

    Might be some caveats, but really – anything is better than modifying the core.

  3. If disk space is not the issue but just download speed as in my case…

    The issue I was facing was the the client was not paying attention to size or dimension for the feature image uploads. The theme is a responsive theme and works well with the range of image heights and scaling to the desired image width in the page. So it was possible to upload very large files and not notice the size if sitting on a high speed network.

    I added custom image sizes, used a plugin to force regeneration and modified the theme to display the custom size instead.

  4. I’ve had a similar problem: a client website should generate bigger thumbs. I’ve ended up building a plugin with a custom Image Editor, like this:

    require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
    require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
    
    function im_add_image_editor($implementations) {
        array_unshift($implementations, 'IM_Image_Editor_GD');
        return $implementations;
    }
    add_filter('wp_image_editors', 'im_add_image_editor');
    

    The IM_Image_Editor_GD extends WP_Image_Editor_GD, changes the _resize function (who calls image_resize_dimensions) to call an internal version of it, like this:

    class IM_Image_Editor_GD extends WP_Image_Editor_GD {
        public function __destruct() {
            parent::__destruct();
        }
    
        protected function _resize( $max_w, $max_h, $crop = false ) {
            $dims = $this->image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
            if ( ! $dims ) {
                return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
            }
            list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
    
            $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
            imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    
            if ( is_resource( $resized ) ) {
                $this->update_size( $dst_w, $dst_h );
                return $resized;
            }
    
            return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
        }
    
        protected function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
            // Copied original image_resize_dimensions and changed the test
        }
    }
    

    Hope this helps and sorry about my English!

  5. You can manage with this plugin visit https://wordpress.org/plugins/simple-image-sizes/

    Simple Image Sizes

    This plugin allow create custom image sizes for your site. Override your theme sizes directly on the media option page. You can regenerate all the sizes you have just created and choose which one you wanted to regenerate. You can now get all the code to copy and paste to your function theme file. Now you can use the generated sizes directly into your posts and insert images at the right size ! Now you choose if you want display the size in the post insert image. Now you can regenerate the images one by one in the ‘Medias’ general pane. Now you can regenerate the images by bulk action in the ‘Medias’ general pane. Now you can regenerate the image sizes on single attachment edit page.

    enter image description here

  6. The default image sizes of WordPress are “thumbnail”, “medium”, “large” and “full” (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. This is how you can use these default sizes with the_post_thumbnail():

    the_post_thumbnail();                  // without parameter -> 'post-thumbnail'
    the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
    the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
    the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
    the_post_thumbnail('full');            // Full resolution (original size uploaded)
    the_post_thumbnail( array(100,100) );  // Other resolutions
    

    see also full documentation in http://codex.wordpress.org/Function_Reference/the_post_thumbnail