Is it possible to show total number of pixels, size in MP, aspect ratio of current featured image?

For example I use this to echo image dimensions of full sized featured image of current post.

if ( has_post_thumbnail()) {
   $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');

// echo image width
echo $thumb_image_url[1];
// echo image height
echo $thumb_image_url[2];

And I get for example dimensions: 2048 x 1536.

Is it possible to make separate functions to calculate:

  1. Total number of pixels width x height? (example 2048 x 1536 = 3.145.728 pixels)
  2. Transform total number of pixels in megapixels? (3.14 megapixel)
  3. Function to calculate aspect ratio based on image width and height? (1,33:1 - 4:3 (screen))

More info about megapixel and aspect ratio calculator on: http://web.forret.com/tools/megapixel.asp

I would go little further also. I think it is possible to get aspect ratio as: standard (lansdcape), portrait or square (approximately).

Standard (most used) image aspect ratio width / height is 4:3, landscape, which is mathematically also 1:1.3333333333333 (example 1600 x 1200 pixels photo).

Portrait would be 0.75 (example 1600 x 1200 pixels photo).

And square would be 1:1 ((example 1200 x 1200 pixels photo).

What I think would be great is to make function which will check result of aspect ratio.

If it is 1.33 (or even better if it is more than 1.2 in case image is little cropped) than to return "Landscape image".

If it is 0.75 (or even better if it is less than 0.8 in case image is little cropped) than to return "Portrait image".

If it is more that 0.8 but less than 1.2 than to return "Square like".

Any suggestions?

1 Answer 1


Sure - php can definitely handle simple math like this. All you'd have to do is reference the numbers coming back from the wp_get_attachment_image_src call:


if ( has_post_thumbnail()) {
 $full_image_info = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
 $img_h = $full_image_info[1];
 $img_w = $full_image_info[2];

 $total_pixels =  $img_w * $img_h ;

 $megapixels = round($total_pixels)
 /* see http://at2.php.net/manual/en/function.round.php -- 
    they get into the details of this method there. There's also number_format() as an option. */

 $ratio = $img_w / $img_h;

From there, you can do what you like with $total_pixels, $megapixels, and $ratio. And of course, you could wrap any of these math operations up as a function by putting something like this, for example, in your functions.php:

function get_total_pixels() {

  if ( !has_post_thumbnail()) { return 'Error - no image!'; }
  else {
    $image_info = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
    $img_h = $image_info[1];
    $img_w = $image_info[2];

    $total_pixels =  $img_w * $img_h ;
    return $total_pixels;

and then calling <?php echo get_total_pixels(); ?> in your template file (inside the loop).

  • completely agree... only thing I could add would be then / 1000000 for MP and maybe then add 'megapixel' or MP automatically by return $total_pixels." megapixels"
    – Bysander
    Commented Jul 7, 2014 at 14:42
  • Yes, that is exactly what I have done. Like this: $megapixels = '' .round($total_pixels / 1000000). ' MP (megapixel)'; Result is for example 2 MP (megapixel). Commented Jul 8, 2014 at 7:01
  • But, I have problem with "round" function. Like it is now there is no decimals (one decimal place would be great), because "0.8" MP is shown as "0", "1.9" as "1". Any suggestion how to make it round by one decimal place? Commented Jul 8, 2014 at 7:04
  • I have updated my question to go little further with options, thanks to @jack contribution. Commented Jul 8, 2014 at 7:23
  • Here is option to round number to one decimal place: $megapixels = '' .round($total_pixels / 1000000, 1). ' MP (megapixel)'; Commented Jul 8, 2014 at 8:20

Your Answer

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

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