0

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;
}



}

1 Answer 1

0

You 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!!

1
  • Thanks Cristopher, I've to say that also my solution works, the problem was elsewhere, however I've corrected the code to highlight better the parameters and the output. Thanks for your answer the perfectly completes the full frame!
    – AmintaCode
    Jun 10, 2019 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.