WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 6 weeks ago

#32313 new enhancement

Image upscale as Media's settings option

Reported by: odie2 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.3
Component: Media Keywords:
Focuses: administration Cc:

Description

Hello,

I had to searching for hours to find image upscale (because unusually keyword for me as non-English). I am creating new school's website and custom featured images are up to 1200px width (in fact ~876px when with sidebar), however old images are only 800px width and I had problem with deal it.

I found so simple solution to set image to required width/height if not enought, then crop.

/*  Thumbnail upscale
/* ------------------------------------ */ 
function alx_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', 'alx_thumbnail_upscale', 10, 6 );

http://alxmedia.se/code/2013/10/thumbnail-upscale-correct-crop-in-wordpress/

In my opinion it should be included as option for each/globally for image sizes in Media's settings page and also as fifth parameter for add_image_size(), because this can make big headache.

Greetings

Change History (2)

#2 @acha5066
4 years ago

I can see why people don't want images to be upscaled as it would make the image fuzzy. However, I'm currently doing a rebuild of an old site in which the container size has increased in width. As a result some of their images (there are 1600) look odd in the layout. If you wanted for example images to be of the same height, so it looked balanced in a two column layout and you had an old image that was larger enough to be resized but one that was too small and therefore wasn't, the layout would look odd.

Some images are only a little bit too small and a tiny bit of stretching is not too bad, when the alternative is manually resizing all images. I think adding this as a parameter to add_image_size would be a good idea. Those who don't want them upscaled can choose not too (it could be the default) and those who do can opt in.

Note: See TracTickets for help on using tickets.