-2

Is it possible to show orientation of featured image in post on single.php?

For example: Orientation of featured image is: portrait / landscape.


I have found that this could be done by calculating aspect ratio of image (width / height). For example image with dimensions 1600 x 1200 have aspect ratio 1.33333 (landscape) and 1200 x 1600 have aspect ratio 0.75 (portrait).

If you are interested to find more look on:

Get total number of pixels, size in megapixels and aspect ratio based on image width and height?

3
  • What have you already attempted for this, what plugins have you looked at?
    – Aravona
    Jul 1 '14 at 9:25
  • Nothing so far, I have no idea where to start from? Any idea? Jul 2 '14 at 9:32
  • I would assume you would have to add additional metadata to the database when you upload them, allowing you to reference orientation as portrait / landscape, then draw that when you have a post with a featured image.
    – Aravona
    Jul 2 '14 at 9:47
3

Basic check of aspect ratio. Display as landscape, square or portrait

if ( !has_post_thumbnail()) { 
    return 'Error'; 
} else {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '');
    $image_w = $image[1];
    $image_h = $image[2];

    if ($image_w > $image_h) { 
        echo 'landscape';
    }
    elseif ($image_w == $image_h) { 
        echo 'square';
    }
    else { 
        echo 'portrait';
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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