Filling image width and height automatically in ImageData

Vote:
 

I need to have some properties on my images like their original size (width and height), so I've inherited from ImageData like this:

[ContentType(DisplayName = "ImageFile", GUID = "...." )]
[MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
public class ImageFile : ImageData
{

public virtual int Height { get; set; }
public virtual int Width { get; set; }

}

But of course those files are not filled. How can I fill them everytime someone uploads an image, or edit the one related?

I was hoping to find a class that I can extend for this but ContentMediaHttpHandler that has been mentioned in the documentation is not there and I don't like to implement a media handler from scratch. 

Where shall I fill the fileds?

#176047
Mar 08, 2017 12:42
Vote:
 

I've done the following for saving the file size, guess you could do something similar to populate Height and Width.

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class FileInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var eventRegistry =
            ServiceLocator.Current.GetInstance<IContentEvents>();

            eventRegistry.CreatingContent += OnCreatingContent;
            eventRegistry.SavingContent += OnSavingContent;
        }
private void OnSavingContent(object sender, ContentEventArgs e)
        {
            var content = e.Content as MediaData;
            if (content == null || !(content is ISearchableFile))
                return;
            var fs = content.GetFileSize();
            ((ISearchableFile)content).FileSizeInKb = (int)fs;
        }

        private static void OnCreatingContent(object sender, ContentEventArgs e)
        {
            var content = e.Content as MediaData;
            if (content == null || !(content is ISearchableFile))
                return;
            var fs = content.GetFileSize();
            ((ISearchableFile)content).FileSizeInKb = (int)fs;
        }
}
#176048
Mar 08, 2017 13:24
Vote:
 

I would go with Erik's approach. Mark Everard has a good example on how to get the dimensions: https://github.com/markeverard/Chief2moro.ImageDataExtensions/blob/master/src/Chief2moro.ImageDataExtensions/ImageBlobUtility.cs

Also, I would add the [Editable(false)] attribute to the properties, as this would be read-only properties.

#176049
Mar 08, 2017 13:44
Vote:
 

This is awesome! exactly what I was looking for.

Thanks guys! Thanks a lot!

#176050
Mar 08, 2017 14:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.