Exemple #1
0
        protected virtual Size GetPictureSize(Stream input, bool leaveOpen = true)
        {
            Guard.NotNull(input, nameof(input));

            var size = Size.Empty;

            try
            {
                using (var reader = new BinaryReader(input, Encoding.UTF8, true))
                {
                    size = ImageHeader.GetDimensions(reader);
                }
            }
            catch (Exception)
            {
                // something went wrong with fast image access,
                // so get original size the classic way
                input.Seek(0, SeekOrigin.Begin);
                using (var b = new Bitmap(input))
                {
                    size = new Size(b.Width, b.Height);
                }
            }
            finally
            {
                if (!leaveOpen)
                {
                    input.Dispose();
                }
            }

            return(size);
        }
        /// <summary>
        /// Refreshes file metadata like size, dimensions etc.
        /// </summary>
        /// <param name="stream">The file stream (can be null)</param>
        public static void RefreshMetadata(this MediaFile file, Stream stream)
        {
            Guard.NotNull(file, nameof(file));

            file.Size      = stream != null ? (int)stream.Length : 0;
            file.Width     = null;
            file.Height    = null;
            file.PixelSize = null;

            if (stream != null && file.MediaType == MediaType.Image)
            {
                try
                {
                    var size = ImageHeader.GetDimensions(stream, file.MimeType);
                    file.Width     = size.Width;
                    file.Height    = size.Height;
                    file.PixelSize = size.Width * size.Height;
                }
                catch
                {
                    // Don't attempt again
                    file.Width     = 0;
                    file.Height    = 0;
                    file.PixelSize = 0;
                }
            }
        }
Exemple #3
0
        internal Size GetPictureSize(byte[] pictureBinary)
        {
            if (pictureBinary == null || pictureBinary.Length == 0)
            {
                return(new Size());
            }

            var stream = new MemoryStream(pictureBinary);

            Size size;

            try
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
                {
                    size = ImageHeader.GetDimensions(reader);
                }
            }
            catch (Exception)
            {
                // something went wrong with fast image access,
                // so get original size the classic way
                using (var b = new Bitmap(stream))
                {
                    size = new Size(b.Width, b.Height);
                }
            }
            finally
            {
                stream.Dispose();
            }

            return(size);
        }
Exemple #4
0
        private void EnsureMetadataResolved(MediaFile file, bool saveOnResolve)
        {
            var mediaType = _typeResolver.Resolve(file.Extension, file.MimeType);

            var resolveDimensions = mediaType == MediaType.Image && file.Width == null && file.Height == null;
            var resolveSize       = file.Size <= 0;

            Stream stream = null;

            if (resolveDimensions || resolveSize)
            {
                stream = _storageProvider.OpenRead(file);
            }

            // Resolve image dimensions
            if (stream != null)
            {
                try
                {
                    if (resolveSize)
                    {
                        file.Size = (int)stream.Length;
                    }

                    if (resolveDimensions)
                    {
                        var size = ImageHeader.GetDimensions(stream, file.MimeType, true);
                        file.Width     = size.Width;
                        file.Height    = size.Height;
                        file.PixelSize = size.Width * size.Height;
                    }

                    if (saveOnResolve)
                    {
                        try
                        {
                            _fileRepo.Update(file);
                        }
                        catch (InvalidOperationException ioe)
                        {
                            // Ignore exception for pictures that already have been processed.
                            if (!ioe.IsAlreadyAttachedEntityException())
                            {
                                throw;
                            }
                        }
                    }
                }
                finally
                {
                    stream.Dispose();
                }
            }
        }
        public virtual byte[] ValidatePicture(byte[] pictureBinary, string mimeType, out Size size)
        {
            Guard.NotNull(pictureBinary, nameof(pictureBinary));
            Guard.NotEmpty(mimeType, nameof(mimeType));

            size = Size.Empty;

            var originalSize = ImageHeader.GetDimensions(pictureBinary, mimeType);

            if (mimeType == "image/svg+xml")
            {
                size = originalSize;
                return(pictureBinary);
            }

            var maxSize = _mediaSettings.MaximumImageSize;

            var query = new ProcessImageQuery(pictureBinary)
            {
                Quality          = _mediaSettings.DefaultImageQuality,
                Format           = MimeTypes.MapMimeTypeToExtension(mimeType),
                IsValidationMode = true
            };

            if (originalSize.IsEmpty || (originalSize.Height <= maxSize && originalSize.Width <= maxSize))
            {
                // Give subscribers the chance to (pre)-process
                var evt = new ImageUploadValidatedEvent(query, originalSize);
                _eventPublisher.Publish(evt);

                if (evt.ResultBuffer != null)
                {
                    // Maybe subscriber forgot to set this, so check
                    size = evt.ResultSize.IsEmpty ? originalSize : evt.ResultSize;
                    return(evt.ResultBuffer);
                }
                else
                {
                    size = originalSize;
                    return(pictureBinary);
                }
            }

            query.MaxWidth  = maxSize;
            query.MaxHeight = maxSize;

            using (var result = _imageProcessor.ProcessImage(query))
            {
                size = new Size(result.Width, result.Height);
                var buffer = result.OutputStream.ToArray();
                return(buffer);
            }
        }
Exemple #6
0
        protected bool ProcessImage(MediaFile file, Stream inStream, out IImage outImage)
        {
            outImage = null;

            var originalSize = Size.Empty;
            var format       = _imageProcessor.Factory.GetImageFormat(file.Extension) ?? new UnsupportedImageFormat(file.MimeType, file.Extension);

            try
            {
                originalSize = ImageHeader.GetDimensions(inStream, file.MimeType);
            }
            catch { }

            if (format is UnsupportedImageFormat)
            {
                outImage = new ImageWrapper(inStream, originalSize, format);
                return(true);
            }

            var maxSize = _mediaSettings.MaximumImageSize;

            var query = new ProcessImageQuery(inStream)
            {
                Quality              = _mediaSettings.DefaultImageQuality,
                Format               = file.Extension,
                DisposeSource        = true,
                ExecutePostProcessor = ImagePostProcessingEnabled,
                IsValidationMode     = true
            };

            if (originalSize.IsEmpty || (originalSize.Height <= maxSize && originalSize.Width <= maxSize))
            {
                // Give subscribers the chance to (pre)-process
                var evt = new ImageUploadedEvent(query, originalSize);
                _eventPublisher.Publish(evt);
                outImage = evt.ResultImage ?? new ImageWrapper(inStream, originalSize, format);

                return(true);
            }

            query.MaxSize = maxSize;

            using (var result = _imageProcessor.ProcessImage(query, false))
            {
                outImage = result.Image;
                return(true);
            }
        }
        /// <summary>
        /// Refreshes file metadata like size, dimensions rtc.
        /// </summary>
        /// <param name="blob">The file binary (can be null)</param>
        public static void RefreshMetadata(this MediaFile file, byte[] blob)
        {
            Guard.NotNull(file, nameof(file));

            file.Size      = blob != null ? blob.Length : 0;
            file.Width     = null;
            file.Height    = null;
            file.PixelSize = null;

            if (blob != null && file.MediaType == MediaType.Image)
            {
                try
                {
                    var size = ImageHeader.GetDimensions(blob, file.MimeType);
                    file.Width     = size.Width;
                    file.Height    = size.Height;
                    file.PixelSize = size.Width * size.Height;
                }
                catch { }
            }
        }
Exemple #8
0
        private void EnsurePictureSizeResolved(Picture picture, bool saveOnResolve)
        {
            if (picture.Width == null && picture.Height == null)
            {
                var mediaItem = picture.ToMedia();
                var stream    = _storageProvider.Value.OpenRead(mediaItem);

                if (stream != null)
                {
                    try
                    {
                        var size = ImageHeader.GetDimensions(stream, picture.MimeType, true);
                        picture.Width        = size.Width;
                        picture.Height       = size.Height;
                        picture.UpdatedOnUtc = DateTime.UtcNow;

                        if (saveOnResolve)
                        {
                            try
                            {
                                _pictureRepository.Update(picture);
                            }
                            catch (InvalidOperationException ioe)
                            {
                                // Ignore exception for pictures that already have been processed.
                                if (!ioe.IsAlreadyAttachedEntityException())
                                {
                                    throw;
                                }
                            }
                        }
                    }
                    finally
                    {
                        stream.Dispose();
                    }
                }
            }
        }
Exemple #9
0
 public virtual Size GetPictureSize(byte[] pictureBinary, string mimeType = null)
 {
     return(ImageHeader.GetDimensions(pictureBinary, mimeType));
 }
 public static Size GetPictureSize(this IPictureService pictureService, Picture picture)
 {
     return(ImageHeader.GetDimensions(pictureService.OpenPictureStream(picture), picture.MimeType, false));
 }