I just did a GTmetrix test for my website to check how it's going, and I noticed that I have many images without height & width size specified.

So I start thinking about a Wordpress function that get the height and width of the current image and echo it.

I found it but didn't work that s why I m here to share with you the peace of code I made and see if you can help me please.

<img src="<?php echo aq_resize(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), 120, 90, true); ?>" alt="<?php the_title();?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>/>    

Here is the result when i use this code

<img src="http://xxxx.com/wp-content/uploads/2014/12/marina-120x90.jpg" alt="marina : La construction de la plus haute tour d’Afrique débutera en juin" width="" height="/&gt;

                                &lt;div class='text'&gt;
                                        &lt;a href=" http:="" xxxx.com="" marina-la-construction-de-la-plus-haute-tour-dafrique-debutera-en-juin="" "="">    

Thanks for help

1 Answer 1


Instead of your code for image, use this following.

<?php $thumb_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 120, 90, true ); ?>
<img src="<?php echo $thumb_image[0]; ?>" alt="<?php echo esc_html( get_the_title() ); ?>" width="<?php echo $thumb_image[1]; ?>" height="<?php echo $thumb_image[2]; ?>" />    

Let me explain a little what it's doing. We are returning PHP array of attachment image with WordPress function wp_get_attachment_image_src.

This PHP array containing: (from Codex)

  • [0] => url
  • [1] => width
  • [2] => height
  • [3] => boolean: true if $url is a resized image, false if it is the original.

So we added those values in our image tag. Try it and let me know how it goes.

  • This will use the closest available size, but with add_image_size you can define a new image size that's exactly what's needed
    – Tom J Nowell
    Dec 17, 2014 at 11:47
  • 1
    Actually I was about to add a pre-define size instead of 120, 90 in wp_get_attachment_image_src but OP has these values so I guess he wanted to use that. But your suggestion is good and I hope OP follows.
    – Robert hue
    Dec 17, 2014 at 11:52
  • Thanks for ur Answers guys just about Robert solution i will have to dont use "aq_resize" what can be a probléme for my theme and Tom can u tell us more about ur solution ? Thank you
    – Dolomats
    Dec 20, 2014 at 14:33

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.