Support » Developing with WordPress » add_image_size crop only portrait pictures

  • Hi I’m hoping for a quick solution to this:
    Is there any way that add_image_size can resize landscape images to a width of say 300px without cropping but crop portrait images to 300px by 300px? I know how to do those in 2 separate instances so:

    add_image_size( '300-landscape', 300 );
    add_image_size( '300-square', 300, 300, true );

    but I either need to have one instance of add_image_size that resizes or crops depending on dimensions or find a way on my page to deliver the 300-landscape image or the 300-square image depending on original picture dimensions.

    Any pointers would be much appreciated. Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I haven’t tried this, but it should work. Use the ‘image_resize_dimensions’ filter in conjunction with a hard cropped image size. Naturally for portrait images, nothing needs to be done, it’s handled correctly already. For landscape (determined by comparing width and height), return the desired image sizes while maintaining the exact aspect ratio. From what I understand of how cropping works, when the aspect ratio does not change, the image is merely resized and not cropped, even if hard cropping is specified.

    I hope this works for you, as it’s not possible to dynamically specify cropping based on orientation.

Viewing 1 replies (of 1 total)
  • The topic ‘add_image_size crop only portrait pictures’ is closed to new replies.