Example #1
0
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Versions()
        {
            var dictionary = new Dictionary <string, IImageVersion>(3);
            var thumb      = new ImageVersion
            {
                Width  = 100,
                Format = new GifFormat {
                    Quality = 50
                },
            };

            dictionary.Add("thumb", thumb);

            var medium = new ImageVersion
            {
                Width  = 640,
                Format = new JpegFormat {
                    Quality = 70
                },
            };

            dictionary.Add("medium", medium);

            var large = new ImageVersion
            {
                Width  = 1080,
                Format = new JpegFormat {
                    Quality = 85
                },
            };

            dictionary.Add("large", large);

            this.versions = new ReadOnlyDictionary <string, IImageVersion>(dictionary);
        }
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Versions()
        {
            var dictionary = new Dictionary<string, IImageVersion>(3);
            var thumb = new ImageVersion
            {
                Width = 100,
                Format = new GifFormat { Quality = 50 },
            };
            dictionary.Add("thumb", thumb);

            var medium = new ImageVersion
            {
                Width = 640,
                Format = new JpegFormat { Quality = 70 },
            };
            dictionary.Add("medium", medium);

            var large = new ImageVersion
            {
                Width = 1080,
                Format = new JpegFormat { Quality = 85 },
            };
            dictionary.Add("large", large);

            this.versions = new ReadOnlyDictionary<string, IImageVersion>(dictionary);
        }
        /// <summary>
        /// Resize Image
        /// </summary>
        /// <param name="file">File</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="format">Format</param>
        /// <param name="quality">Quality</param>
        /// <param name="cache">Cache</param>
        /// <returns>Image Data</returns>
        public async Task <ImageData> Resize(string file, ushort width, ushort height = 0, string format = Naming.DefaultExtension, byte quality = Imaging.DefaultImageQuality, bool cache = true)
        {
            var wasCached = false;
            var imgFormat = this.imaging.Get(format, quality);

            var data = new ImageData()
            {
                MimeType = imgFormat.MimeType,
            };

            var identifier     = this.naming.FromFileName(file);
            var versionName    = this.naming.DynamicVersion(imgFormat.DefaultExtension, quality, width, height);
            var cachedFileName = this.naming.FileName(identifier, versionName, imgFormat.DefaultExtension);

            var streamer = this.Streamer;

            if (cache)
            {
                data.Raw = await streamer.Bytes(cachedFileName);

                wasCached = null != data.Raw;
            }

            if (!wasCached)
            {
                var version = new ImageVersion
                {
                    Height = height,
                    Width  = width,
                    Format = imgFormat,
                };

                var toResize = await streamer.Bytes(file);

                data.Raw = this.imaging.Resize(toResize, version);
            }

            if (cache && !wasCached)
            {
                await this.Save(cachedFileName, data.Raw, versionName, imgFormat.MimeType, identifier, false, null, quality);
            }

            return(data);
        }
        /// <summary>
        /// Resize Image
        /// </summary>
        /// <param name="file">File</param>
        /// <param name="width">Width</param>
        /// <param name="height">Height</param>
        /// <param name="format">Format</param>
        /// <param name="quality">Quality</param>
        /// <param name="cache">Cache</param>
        /// <returns>Image Data</returns>
        public async Task<ImageData> Resize(string file, ushort width, ushort height = 0, string format = Naming.DefaultExtension, byte quality = Imaging.DefaultImageQuality, bool cache = true)
        {
            var wasCached = false;
            var imgFormat = this.imaging.Get(format, quality);

            var data = new ImageData()
            {
                MimeType = imgFormat.MimeType,
            };

            var identifier = this.naming.FromFileName(file);
            var versionName = this.naming.DynamicVersion(imgFormat.DefaultExtension, quality, width, height);
            var cachedFileName = this.naming.FileName(identifier, versionName, imgFormat.DefaultExtension);

            var streamer = this.Streamer;

            if (cache)
            {
                data.Raw = await streamer.Bytes(cachedFileName);
                wasCached = null != data.Raw;
            }

            if (!wasCached)
            {
                var version = new ImageVersion
                {
                    Height = height,
                    Width = width,
                    Format = imgFormat,
                };

                var toResize = await streamer.Bytes(file);
                data.Raw = this.imaging.Resize(toResize, version);
            }

            if (cache && !wasCached)
            {
                await this.Save(cachedFileName, data.Raw, versionName, imgFormat.MimeType, identifier, false, null, quality);
            }

            return data;
        }