Let's have some thumbnail sizes like that:
add_image_size('home-slide-medium', 1000, 504, true);
add_image_size('home-slide-sm', 500, 252, true);
add_image_size('video-poster', 780, 512);
And let's upload a portrait image with size like 1000x3000px
How can avoid the creation of the landscape thumbnails (like 1000x504
or 500x252
) for this portrait image?
I tried something like this but it doesn't work:
add_filter( 'image_resize_dimensions', 'custom_image_resize_dimensions', 10, 6 );
function custom_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
// ie parameters: null, 1000, 3000, 1000, 500, true
// ie payload: array(0, 0, 0, 1248, 1000, 504, 1000, 504))
// if $crop is true...
if($crop ) {
// ...and if src img is portrait, skip it unless is same aspect ratio
if($dest_w > $dest_h) {
return false;
}
// else continue
else {
return $payload;
}
}
else {
return $payload;
}
}
Let's have some thumbnail sizes like that:
add_image_size('home-slide-medium', 1000, 504, true);
add_image_size('home-slide-sm', 500, 252, true);
add_image_size('video-poster', 780, 512);
And let's upload a portrait image with size like 1000x3000px
How can avoid the creation of the landscape thumbnails (like 1000x504
or 500x252
) for this portrait image?
I tried something like this but it doesn't work:
add_filter( 'image_resize_dimensions', 'custom_image_resize_dimensions', 10, 6 );
function custom_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
// ie parameters: null, 1000, 3000, 1000, 500, true
// ie payload: array(0, 0, 0, 1248, 1000, 504, 1000, 504))
// if $crop is true...
if($crop ) {
// ...and if src img is portrait, skip it unless is same aspect ratio
if($dest_w > $dest_h) {
return false;
}
// else continue
else {
return $payload;
}
}
else {
return $payload;
}
}
Share
Improve this question
edited Jun 10, 2019 at 22:58
AmintaCode
asked Jun 10, 2019 at 18:48
AmintaCodeAmintaCode
1771 gold badge4 silver badges16 bronze badges
1 Answer
Reset to default 0You were headed in the right direction. There is another filter out there that handles intermediate_image_sizes_advanced
. You can add or remove custom sized thumbnails in there. Rather than NOT creating your landscape custom sizes on a Portrait upload, maybe try to only create the landscape custom sizes on a landscape upload.
add_filter( 'intermediate_image_sizes_advanced', function( $sizes, $metadata ) {
// Let's make sure we have the meta data we need before proceeding
if(!empty($metadata['width']) && !empty($metadata['height'])){
// Now let's make sure we have a Landscape being uploaded
if($metadata['width'] > $metadata['height']){
$sizes['home-slide-medium'] = array(
'width' => 1000,
'height' => 504,
'crop' => true
);
$sizes['home-slide-sm'] = array(
'width' => 500,
'height' => 252,
'crop' => tru
e);
$sizes['video-poster'] = array(
'width' => 780,
'height' => 512,
'crop' =>false
);
}
}
return $sizes;
}, 10, 2);
I tested it a few times on my end, but let me know whatcha think!!