Linus Karlsson
Linus Karlsson

Reputation: 35

Get height and width from image in wordpress

I'm trying to make an info block on my WordPress website I am working on. I want to get the height X width of the photo:

get_the_post_thumbnail_url() 

I want it printed out on the page something like:

image size: 1234x123 1.2mb

I just don't know how to make it. I have tried a lot of different examples I found on the web but I always get different errors.

Hope someone has an idea of a good way to do this. :)

Upvotes: 1

Views: 5960

Answers (3)

Shital Marakana
Shital Marakana

Reputation: 2887

you can try below code

            if ( has_post_thumbnail() ) {
                    //thumbnail
                    echo  get_the_post_thumbnail(get_the_ID(),"thumbnail"); //thumbnail,medium,large,full,array(1234,123)
                    //medium
                    echo  get_the_post_thumbnail(get_the_ID(),"medium");
                    //large
                    echo  get_the_post_thumbnail(get_the_ID(),"large"); 
                    //full
                    echo  get_the_post_thumbnail(get_the_ID(),"full"); 
                    //fixed size image(width, height)
                    echo  get_the_post_thumbnail(get_the_ID(),array(1234,123)); 

                }

Upvotes: 0

Muhammad Asad
Muhammad Asad

Reputation: 3793

This WP function wp_get_attachment_image_src, provides image width and height:

Read here more: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

From docs, it says that it Returns:

Returns an array (url, width, height, is_intermediate), or false, if no img.

For Example:

  $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), "full" );

Now

$url = $img[0];
$width = $img[1];
$height = $img[2];

Upvotes: 8

anthonytherockjohnson
anthonytherockjohnson

Reputation: 627

Try

<?php
    $size = getimagesize(get_the_post_thumbnail_url());
?>

and then echo out the size

<?php
    echo $size;
?>

taken from the docs here

Upvotes: 0

Related Questions