Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

function to scale up Wordpress images

I found this great topic but am not allowed to comment due to my rep...... So I found myself forced to open this new topic (am I right?)

It states this code in order to scale up my images, it works perfectly!

function image_crop_dimensions($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', 'image_crop_dimensions', 10, 6);

however It does not work for image sizes defined like this:

add_image_size( '800', 800 ); of course, cause there's no $new_h right? how can I fix this function to work on add_image_size( '800', 800 ); images?

Thanks!

EDIT Changed my add_image_size( '800', 800 ); to add_image_size( '800', 800, 800 ); and it still does not work. this is because crop is set to false, if I do this, add_image_size( '800', 800, 800, true ); it works, but I don't want the crop! Thanks guys

3 Answers

If you already uploaded the images then you need to regenerate the thumbnails. I think this plugin should do that for you:

https://wordpress.org/plugins/regenerate-thumbnails/

Hopefully I'm understanding the question correctly. Also, I think that you run into issues trying to scale images into proportions that don't match unless you use crop (a square image into a rectangle). I think that if you are simply trying to make sure they are 800px wide, and the height doesn't matter, that you can specify only one value and it will just size the images to that width and whatever the height would be naturally.

Are you trying to take existing images and create new thumbnails at the new sizes?

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Thanks for sharing Boris! Glad you got it sorted out :)

Boris Kamp
Boris Kamp
16,660 Points

Zac Gordon what do you mean? I think you misread my question, it's a question, not a solution. Can you give me some help on this?

Thanks!