Пример #1
0
        public override bool IsValid(object value)
        {
            if (Height < 1 && Width < 1)
            {
                ErrorMessage = "The allowed image height or image width must be specified within the validation attribute";
                return(false);
            }
            var contentReference = value as ContentReference;

            if (contentReference == null)
            {
                ErrorMessage = "RequiredImageSize attribute should only be applied to ContentReference properties";
                return(false);
            }

            var content = ServiceLocator.Current.GetInstance <IContentLoader>().Get <ContentData>(contentReference);

            if (content == null)
            {
                ErrorMessage = "The content selected for property '{0}' cannot be found!";
                return(false);
            }

            var imageData = content as IContentImage;

            if (imageData == null)
            {
                ErrorMessage = "RequiredImageSize attribute applied to property '{0}' can only be applied to ContentReferences that inherit from IContentImage";
                return(false);
            }

            var dimensions = ImageBlobUtility.GetDimensions(imageData.BinaryData);

            if (IsNotRequiredHeight(dimensions) && IsNotRequiredWidth(dimensions))
            {
                ErrorMessage = "The image used in reference '{0}' must have a size of ({1},{2}) pixels";
                return(false);
            }


            if (IsNotRequiredHeight(dimensions))
            {
                ErrorMessage = "The image used in property '{0}' must have a height of {2} pixels";
                return(false);
            }

            if (IsNotRequiredWidth(dimensions))
            {
                ErrorMessage = "The image used in property '{0}' must have a width of {1} pixels";
                return(false);
            }

            return(true);
        }
Пример #2
0
        public void contentEvents_PublishingContent(object sender, ContentEventArgs e)
        {
            var content = e.Content;

            if (!(content is ImageData))
            {
                return;
            }

            var        image           = content as ImageData;
            Dimensions imageDimensions = ImageBlobUtility.GetDimensions(image.BinaryData);

            PropertyInfo[] properties    = image.GetType().GetProperties();
            int            requiredWidth = imageDimensions.Width;

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.PropertyType != typeof(Blob))
                {
                    continue;
                }

                //get attribute name
                var imageWidthAttribute = (ImageWidthDescriptorAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(ImageWidthDescriptorAttribute));
                var imageScaleAttribute = (ImageScaleDescriptorAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(ImageScaleDescriptorAttribute));

                if (imageWidthAttribute != null)
                {
                    requiredWidth = imageWidthAttribute.Width;
                }
                else if (imageScaleAttribute != null)
                {
                    requiredWidth = (int)(imageDimensions.Width * imageScaleAttribute.DimensionMultiplier);
                }
                else
                {
                    continue;
                }

                var calculatedDimensions = ImageResizeUtility.ResizeWidthMaintainAspectRatio(imageDimensions, requiredWidth);
                var imageDescriptor      = new ImageDescriptorAttribute(calculatedDimensions.Height, calculatedDimensions.Width);

                var thumbnailManager = ServiceLocator.Current.GetInstance <ThumbnailManager>();
                var resizedBlob      = thumbnailManager.CreateImageBlob(image.BinaryData, propertyInfo.Name, imageDescriptor);

                //assign blob to current property
                propertyInfo.SetValue(image, resizedBlob, null);
            }
        }
Пример #3
0
        public override bool IsValid(object value)
        {
            if (MinimumHeight < 1 && MaximumHeight < 1 &&
                MinimumWidth < 1 && MaximumWidth < 1)
            {
                ErrorMessage = "The allowed image height or width limits must be specified within the validation attribute";
                return(false);
            }

            var contentReference = value as ContentReference;

            if (contentReference == null)
            {
                ErrorMessage = "RequiredImageSize attribute should only be applied to ContentReference properties";
                return(false);
            }

            var content = ServiceLocator.Current.GetInstance <IContentLoader>().Get <ContentData>(contentReference);

            if (content == null)
            {
                ErrorMessage = "The content selected for property '{0}' cannot be found!";
                return(false);
            }

            var imageData = content as IContentImage;

            if (imageData == null)
            {
                ErrorMessage = "RequiredImageSize attribute applied to property '{0}' can only be applied to ContentReferences that inherit from IContentImage";
                return(false);
            }

            var dimensions = ImageBlobUtility.GetDimensions(imageData.BinaryData);

            if (!IsHeightWithinRange(dimensions) || !IsWidthWithinRange(dimensions))
            {
                ErrorMessage = "The image used in reference '{0}' must have the following dimensions:" + GetRequiredSizes();
                return(false);
            }

            return(true);
        }