Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeremiah Sulewski 1 post 71 karma points
    Feb 14, 2023 @ 19:11
    Jeremiah Sulewski
    0

    mediaItem.GetValue - umbracoWidth umbracoHeight sometimes return null values

    We're hooking some code into MediaService.Saving in order to check if an image is over a certain width or height, and if so resizing it to be smaller. In order to perform the check, we get the values umbracoWidth and umbracoHeight of the image being uploaded.

    However, every so often those values are randomly null, which causes our code to error out. Attached is a sample image that's giving trouble, our modified code in Events/MediaEvents.cs, and a screenshot of the error. Error

    private void MediaService_Saving(Umbraco.Core.Services.IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
        {
            int width = int.Parse(WebConfigurationManager.AppSettings["ImageResizeWidth"]);
            int height = int.Parse(WebConfigurationManager.AppSettings["ImageResizeHeight"]);
            string fileNameSuffix = WebConfigurationManager.AppSettings["ImageResizeSuffix"];
    
            foreach (var mediaItem in e.SavedEntities)
            {
                if (!string.IsNullOrEmpty(mediaItem.ContentType.Alias) && mediaItem.ContentType.Alias == "Image")
                {
                    bool isNew = !mediaItem.HasIdentity;
    
                    int currentWidth = int.Parse(mediaItem.GetValue("umbracoWidth").ToString());
                    int currentHeight = int.Parse(mediaItem.GetValue("umbracoHeight").ToString());
    
Please Sign in or register to post replies

Write your reply to:

Draft