Find your content:

Search form

You are here

Getting the width and height of an image attachment

 
Share

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"/>

Attribution to: Joey Chan

Possible Suggestion/Solution #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

<apex:page>

  <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';

  </script>
</apex:page>

Another approach might be to do like this

<apex:page>

<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 () {
        $('img[id$=testImg]')             
            .attr('src', 'https://c.ap1.content.force.com/sfc/servlet.shepherd/version/download/06890000000MkxY')
            .load(function(){        
            // do what you want with height width here
                alert($(this).height() + ' X ' + $(this).width());

            });
    });
  </script>
</apex:page>

Hope this helps


Attribution to: Abhinav Gupta
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1126

My Block Status

My Block Content