0

Is there a way to find Dimensions (Height and Width) for a Content Document uploaded on a custom object? The Content Document could be JPG, PNG or GIF

The use case is: When users upload a content document (image) through the partner community. The content document is uploaded into a custom object. And I need to to get the Height and Width of that image and put it in a custom field.

Thank You

1 Answer 1

1

ContentDocument doesn't directly expose this information. After all, it just contains generic data about the file. I can think of at least three possible options.

In Apex, you could read each of the file specs for JPEG, PNG, and GIF, learn how those dimensions are stored, and read them through the use of EncodingUtils.hexDecode to read the individual hex digits to find the size. Since each format specifies the dimensions in the first few hundred bytes, and are at known locations. I'd say it'd be reasonable that most developers could probably puzzle this out in a few hours.

If you want to do this in browser JavaScript, such as in LWC or Aura, you could use an Image to read the ContentDocument's data, and then use the values from the Image. This could be done by reading the files after they've been uploaded so you can parse the response.

As a final alternative, you could also use Heroku, AWS EB, etc to parse the file contents with ImageMagick, which will return the information for you. This solution has a non-zero cost, but for most use cases, you'd probably only need to spend $5 USD a month to keep the service running.

4
  • So I build LWC component that runs a javascript function after uploading an image file. When accessing the file like this: const file = event.target.files[0] - I am getting: contentBodyId,contentVersionId,documentId,mimeType - Correct me if I am wrong, but I do not get the file itself, because it is stored somewhere on Salesforce - I am having trouble reading the image or getting a direct link to it, so I can finally run the JS code that will find the image dimensions - I need help with lwc that will fetch the image from the ContentVersion ID or produce a direct link to the image
    – Moe Najim
    Commented Jun 1, 2023 at 12:39
  • @MoeNajim You are correct. You can retrieve the file contents with the contentVersionId if you need the data back on the client. However, that sounds a bit like an X-Y Problem. Why do you need the dimensions? Why are you using lightning-file-upload if you need client-side access to this data? What is your end objective?
    – sfdcfox
    Commented Jun 1, 2023 at 15:48
  • We built a community portal for clients to be able to upload images, and the images will be saved on our Salesforce. But we have rules regarding the dimensions of these images. We want to validate the image dimensions and ask him for a new image in case of wrong dimensions
    – Moe Najim
    Commented Jun 5, 2023 at 13:17
  • @MoeNajim You'd want to use lightning-input type="file" so you can read the data locally. You can always upload the file later.
    – sfdcfox
    Commented Jun 5, 2023 at 19:50

You must log in to answer this question.

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