function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash KaltariAvinash Kaltari 

Validating Image Dimensions (width and height) of an image saved as an attachment

Hello, I need to validate the image dimensions, i.e. its width and height, in APEX Code. Is there any way in which i can do this after retrieving the image which is saved as an Attachment record. Please suggest some alternative ways to do this if you know. Any help would be appreciated. Thanks, Avinash Kaltari.
Avinash KaltariAvinash Kaltari
Hello, This is my scenario in detail : 1. I have a VF Page with a apex:inputFile tag (through which the user would be able to browse and select for an image) 2. I will get the image saved once the user hits "Save" button. 3. I need to check the dimensions of this image (saved as an attachment) and display the appropraite error message through either JavaScript or the Apex Code. 4. So, coming back to my question, Is there any way in which i can check the dimensions of this image (saved as an attachment) either in Javascript or Apex code ??
SteveBowerSteveBower

Hi, I'm not 100% sure of this approach by any means, so take it with a grain of salt...

 

There is an Exif jquery plugin which you can use to parse a file and extract various bits of data including dimensions.  

 

However, it fetches the image file so it can read it.  So, n once you've loaded it as an attachment you have to dynamically build an <img> tag using the URL to read the file and then use the plugin to re-read it, something like:

 

.... once you've loaded that attachment with new id of AttachId you need to get that ID back to the javascript somehow..

 

In your Controller I'd presume that's where you're saving the attachment.  So store the Id of the new attachment in a string so the javascript can access it:

 

public ID attachmentId {get; set;}

 

In the javascript, 

 

imgsrc='/servlet/servlet.FileDownload?file={!attachmentId}'

 

put some sort of DIV in your VF page:    <div id="UploadedImageDiv"></div>

 

 

and later:

 

$('div#UploadedImageDiv').append('<img/>', {id:UploadedImage, src: imgSrc});

 

Then you can use the jquery plugin below on the $('img#UploadedImage') to fetch Dimensions, etc.

 

http://blog.nihilogic.dk/2008/05/jquery-exif-data-plugin.html

 

Best, Steve.

 

p.s. Again, big grain of salt... I was just curious about your question and so I looked into it... I've not tried this.

 

 

SteveBowerSteveBower

Actually, now that I re-read your posting, you can use the jquery once your user has browsed and given you the URL.  You don't need to create the attachment first at all, just build an IMG tag on the URL the user returns and go from there.

-Steve