Is it possible to get the width and height of an image in an Attachment?

I'm looking for a way to make it show properly based on the image.

<apex:image value="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!a.Id}" width="XXX" height="XXX"/>
  • This seems like a great place where VF should be able to fill them in automatically... If you post on the IdeaExchange and update here with the link, I'll vote for it.
    – Benj
    Sep 4, 2012 at 15:28

1 Answer 1


I just tried following code and its alerting the width and height correctly. One approach would be to load images like this and add them to the dom as required


  <script type="text/javascript">
    var img = new Image();
    img.onload = function() {
      // you can append this img tag in desired DOM location now with height/width as required.
      alert(this.width + 'x' + this.height);
    img.src = 'https://c.ap1.content.force.com/sfc/servlet.shepherd/version/download/06890000000MkxY';


Another approach might be to do like this


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<apex:image id="testImg" value="/s.gif" />
  <script type="text/javascript">
    $(document).ready(function () {
            .attr('src', 'https://c.ap1.content.force.com/sfc/servlet.shepherd/version/download/06890000000MkxY')
            // do what you want with height width here
                alert($(this).height() + ' X ' + $(this).width());


Hope this helps

  • Can this be done solely from the controller side without using javascript? Mar 20, 2017 at 6:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .