Esempio n. 1
0
        public LocalizedString DisplayFilter(FilterContext context) {
            string mode = context.State.Mode;

            switch (mode) {
                case "pad": return T("Pad to {0}x{1}", context.State.Height, context.State.Width);
                case "crop": return T("Crop to {0}x{1}", context.State.Height, context.State.Width);
                case "stretch": return T("Stretch to {0}x{1}", context.State.Height, context.State.Width);
                default: return T("Resize to {0}x{1}", context.State.Height, context.State.Width); 

            } 
        }
Esempio n. 2
0
        public void ApplyFilter(FilterContext context) {
            int witdh = context.State.Width;
            int height = context.State.Height;
            string mode = context.State.Mode;
            string alignment = context.State.Alignment;
            string padcolor = context.State.PadColor;

            var settings = new ResizeSettings {
                Mode = FitMode.Max,
                Height = height,
                Width = witdh
            };

            switch (mode) {
                case "max": settings.Mode = FitMode.Max; break;
                case "pad": 
                    settings.Mode = FitMode.Pad; 
                    settings.Scale = ScaleMode.Both; 
                    break;
                case "crop": settings.Mode = FitMode.Crop; break;
                case "stretch": settings.Mode = FitMode.Stretch; break;
            }

            switch (alignment) {
                case "topleft": settings.Anchor = ContentAlignment.TopLeft; break;
                case "topcenter": settings.Anchor = ContentAlignment.TopCenter; break;
                case "topright": settings.Anchor = ContentAlignment.TopRight; break;
                case "middleleft": settings.Anchor = ContentAlignment.MiddleLeft; break;
                case "middlecenter": settings.Anchor = ContentAlignment.MiddleCenter; break;
                case "middleright": settings.Anchor = ContentAlignment.MiddleRight; break;
                case "bottomleft": settings.Anchor = ContentAlignment.BottomLeft; break;
                case "bottomcenter": settings.Anchor = ContentAlignment.BottomCenter; break;
                case "bottomright": settings.Anchor = ContentAlignment.BottomRight; break;
            }

            if (!String.IsNullOrWhiteSpace(padcolor)) {
                if (padcolor.StartsWith("#")) {
                    settings.BackgroundColor = ColorTranslator.FromHtml(padcolor);
                }
                else {
                    settings.BackgroundColor = Color.FromName(padcolor);
                }
            }

            var result = new MemoryStream();
            if (context.Media.CanSeek) {
                context.Media.Seek(0, SeekOrigin.Begin);
            }
            ImageBuilder.Current.Build(context.Media, result, settings, true);
            context.Media = result;
        }
Esempio n. 3
0
        public void ApplyFilter(FilterContext context) {
            string format = context.State.Format;
            int quality = context.State.Quality;

            var settings = new ResizeSettings {
                Quality = quality,
                Format = format
            };

            var result = new MemoryStream();
            if (context.Media.CanSeek) {
                context.Media.Seek(0, SeekOrigin.Begin);
            }
            
            ImageBuilder.Current.Build(context.Media, result, settings);

            context.FilePath = Path.ChangeExtension(context.FilePath, format);
            context.Media = result;
        }
        public string GetImageProfileUrl(string path, string profileName, FilterRecord customFilter, ContentItem contentItem) {

            // path is the publicUrl of the media, so it might contain url-encoded chars

            // try to load the processed filename from cache
            var filePath = _fileNameProvider.GetFileName(profileName, System.Web.HttpUtility.UrlDecode(path));
            bool process = false;

            //after reboot the app cache is empty so we reload the image in the cache if it exists in the _Profiles folder
            if (string.IsNullOrEmpty(filePath)) {
                var profileFilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, System.Web.HttpUtility.UrlDecode(path)));

                if (_storageProvider.FileExists(profileFilePath)) {
                    _fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), profileFilePath);
                    filePath = profileFilePath;
                }
            }
            
            // if the filename is not cached, process it
            if (string.IsNullOrEmpty(filePath)) {
                Logger.Debug("FilePath is null, processing required, profile {0} for image {1}", profileName, path);

                process = true;
            }

                // the processd file doesn't exist anymore, process it
            else if (!_storageProvider.FileExists(filePath)) {
                Logger.Debug("Processed file no longer exists, processing required, profile {0} for image {1}", profileName, path);

                process = true;
            }

            // if the original file is more recent, process it
            else {
                DateTime pathLastUpdated;
                if (TryGetImageLastUpdated(path, out pathLastUpdated)) {
                    var filePathLastUpdated = _storageProvider.GetFile(filePath).GetLastUpdated();

                    if (pathLastUpdated > filePathLastUpdated) {
                        Logger.Debug("Original file more recent, processing required, profile {0} for image {1}", profileName, path);

                        process = true;
                    }
                }
            }

            // todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries.
            if (process) {
                Logger.Debug("Processing profile {0} for image {1}", profileName, path);

                ImageProfilePart profilePart;

                if (customFilter == null) {
                    profilePart = _profileService.GetImageProfileByName(profileName);
                    if (profilePart == null)
                        return String.Empty;
                }
                else {
                    profilePart = _services.ContentManager.New<ImageProfilePart>("ImageProfile");
                    profilePart.Name = profileName;
                    profilePart.Filters.Add(customFilter);
                }

                // prevent two requests from processing the same file at the same time
                // this is only thread safe at the machine level, so there is a try/catch later
                // to handle cross machines concurrency
                lock (String.Intern(path)) {
                    using (var image = GetImage(path)) {

                        var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, System.Web.HttpUtility.UrlDecode(path))) };

                        if (image == null) {
                            return filterContext.FilePath;
                        }

                        var tokens = new Dictionary<string, object>();
                        // if a content item is provided, use it while tokenizing
                        if (contentItem != null) {
                            tokens.Add("Content", contentItem);
                        }

                        foreach (var filter in profilePart.Filters.OrderBy(f => f.Position)) {
                            var descriptor = _processingManager.DescribeFilters().SelectMany(x => x.Descriptors).FirstOrDefault(x => x.Category == filter.Category && x.Type == filter.Type);
                            if (descriptor == null)
                                continue;

                            var tokenized = _tokenizer.Replace(filter.State, tokens);
                            filterContext.State = FormParametersHelper.ToDynamic(tokenized);
                            descriptor.Filter(filterContext);
                        }

                        _fileNameProvider.UpdateFileName(profileName, System.Web.HttpUtility.UrlDecode(path), filterContext.FilePath);

                        if (!filterContext.Saved) {
                            try {
                                var newFile = _storageProvider.OpenOrCreate(filterContext.FilePath);
                                using (var imageStream = newFile.OpenWrite()) {
                                    using (var sw = new BinaryWriter(imageStream)) {
                                        if (filterContext.Media.CanSeek) {
                                            filterContext.Media.Seek(0, SeekOrigin.Begin);
                                        }
                                        using (var sr = new BinaryReader(filterContext.Media)) {
                                            int count;
                                            var buffer = new byte[8192];
                                            while ((count = sr.Read(buffer, 0, buffer.Length)) != 0) {
                                                sw.Write(buffer, 0, count);
                                            }
                                        }
                                    }
                                }
                            }
                            catch(Exception e) {
                                Logger.Error(e, "A profile could not be processed: " + path);
                            }
                        }

                        filterContext.Media.Dispose();
                        filePath = filterContext.FilePath;
                    }
                }
            }

            // generate a timestamped url to force client caches to update if the file has changed
            var publicUrl = _storageProvider.GetPublicUrl(filePath);
            var timestamp = _storageProvider.GetFile(filePath).GetLastUpdated().Ticks;
            return publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture);
        }
        public LocalizedString DisplayFilter(FilterContext context) {
            string mode = context.State.Mode;

            switch (mode) {
                case "pad": return T("Format to {0} and Pad to {1}x{2}", context.State.Format, context.State.Height, context.State.Width);
                case "crop": return T("Format to {0} and Crop to {1}x{2}", context.State.Format, context.State.Height, context.State.Width);
                case "stretch": return T("Format to {0} and Stretch to {1}x{2}", context.State.Format, context.State.Height, context.State.Width);
                default: return T("Format to {0} and resize to Max {1}x{2}", context.State.Format, context.State.Height, context.State.Width); 

            } 
        }
        public void ApplyFilter(FilterContext context) {
            int width = context.State.Width;
            int height = context.State.Height;
            string mode = context.State.Mode;
            string alignment = context.State.Alignment;
            string padcolor = context.State.PadColor;
            int quality = context.State.Quality;
            string format = context.State.Format;

            // Set the format and extension correctly
            // ImageResizer automagically changes the format from tiff to jpeg
            if (string.Compare(format, "(any)", true) == 0)
                if (string.Compare(Path.GetExtension(context.FilePath), ".tif", true) == 0)
                    format = "jpg";
                else
                    format = Path.GetExtension(context.FilePath);
            context.FilePath = Path.ChangeExtension(context.FilePath, format);
            
            var settings = new ResizeSettings {
                Mode = FitMode.Max,
                Height = height,
                Width = width,
                Format = format,
                Quality = quality
            };

            switch (mode) {
                case "max": settings.Mode = FitMode.Max; break;
                case "pad": settings.Mode = FitMode.Pad; break;
                case "crop": settings.Mode = FitMode.Crop; break;
                case "stretch": settings.Mode = FitMode.Stretch; break;
            }

            switch (alignment) {
                case "topleft": settings.Anchor = ContentAlignment.TopLeft; break;
                case "topcenter": settings.Anchor = ContentAlignment.TopCenter; break;
                case "topright": settings.Anchor = ContentAlignment.TopRight; break;
                case "middleleft": settings.Anchor = ContentAlignment.MiddleLeft; break;
                case "middlecenter": settings.Anchor = ContentAlignment.MiddleCenter; break;
                case "middleright": settings.Anchor = ContentAlignment.MiddleRight; break;
                case "bottomleft": settings.Anchor = ContentAlignment.BottomLeft; break;
                case "bottomcenter": settings.Anchor = ContentAlignment.BottomCenter; break;
                case "bottomright": settings.Anchor = ContentAlignment.BottomRight; break;
            }

            if (!String.IsNullOrWhiteSpace(padcolor)) {
                if (padcolor.StartsWith("#")) {
                    settings.BackgroundColor = ColorTranslator.FromHtml(padcolor);
                }
                else {
                    settings.BackgroundColor = Color.FromName(padcolor);
                }
            }

            var result = new MemoryStream();
            if (context.Media.CanSeek) {
                context.Media.Seek(0, SeekOrigin.Begin);
            }
            ImageBuilder.Current.Build(context.Media, result, settings);
            context.Media = result;
        }
Esempio n. 7
0
 public LocalizedString DisplayFilter(FilterContext context) {
     return T("Format the image to {0}", (string)context.State.Format);
 }
Esempio n. 8
0
        public string GetImageProfileUrl(string path, string profileName, FilterRecord customFilter, ContentItem contentItem) {

            // try to load the processed filename from cache
            var filePath = _fileNameProvider.GetFileName(profileName, path);
            bool process = false;

            // if the filename is not cached, process it
            if (string.IsNullOrEmpty(filePath)) {
                Logger.Debug("FilePath is null, processing required, profile {0} for image {1}", profileName, path);

                process = true;
            }

                // the processd file doesn't exist anymore, process it
            else if (!_storageProvider.FileExists(filePath)) {
                Logger.Debug("Processed file no longer exists, processing required, profile {0} for image {1}", profileName, path);

                process = true;
            }

            // if the original file is more recent, process it
            else {
                DateTime pathLastUpdated;
                if (TryGetImageLastUpdated(path, out pathLastUpdated)) {
                    var filePathLastUpdated = _storageProvider.GetFile(filePath).GetLastUpdated();

                    if (pathLastUpdated > filePathLastUpdated) {
                        Logger.Debug("Original file more recent, processing required, profile {0} for image {1}", profileName, path);

                        process = true;
                    }
                }
            }

            // todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries.
            if (process) {
                Logger.Debug("Processing profile {0} for image {1}", profileName, path);

                ImageProfilePart profilePart;

                if (customFilter == null) {
                    profilePart = _profileService.GetImageProfileByName(profileName);
                    if (profilePart == null)
                        return String.Empty;
                }
                else {
                    profilePart = _services.ContentManager.New<ImageProfilePart>("ImageProfile");
                    profilePart.Name = profileName;
                    profilePart.Filters.Add(customFilter);
                }

                using (var image = GetImage(path)) {

                    var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", _storageProvider.Combine(profileName, CreateDefaultFileName(path))) };

                    var tokens = new Dictionary<string, object>();
                    // if a content item is provided, use it while tokenizing
                    if (contentItem != null) {
                        tokens.Add("Content", contentItem);
                    }

                    foreach (var filter in profilePart.Filters.OrderBy(f => f.Position)) {
                        var descriptor = _processingManager.DescribeFilters().SelectMany(x => x.Descriptors).FirstOrDefault(x => x.Category == filter.Category && x.Type == filter.Type);
                        if (descriptor == null)
                            continue;

                        var tokenized = _tokenizer.Replace(filter.State, tokens);
                        filterContext.State = FormParametersHelper.ToDynamic(tokenized);
                        descriptor.Filter(filterContext);
                    }

                    _fileNameProvider.UpdateFileName(profileName, path, filterContext.FilePath);

                    if (!filterContext.Saved) {
                        _storageProvider.TryCreateFolder(_storageProvider.Combine("_Profiles", profilePart.Name));
                        var newFile = _storageProvider.OpenOrCreate(filterContext.FilePath);
                        using (var imageStream = newFile.OpenWrite()) {
                            using (var sw = new BinaryWriter(imageStream)) {
                                if (filterContext.Media.CanSeek) {
                                    filterContext.Media.Seek(0, SeekOrigin.Begin);
                                }
                                using (var sr = new BinaryReader(filterContext.Media)) {
                                    int count;
                                    var buffer = new byte[8192];
                                    while ((count = sr.Read(buffer, 0, buffer.Length)) != 0) {
                                        sw.Write(buffer, 0, count);
                                    }
                                }
                            }
                        }
                    }

                    filterContext.Media.Dispose();
                    filePath = filterContext.FilePath;
                }
            }

            // generate a timestamped url to force client caches to update if the file has changed
            var publicUrl = _storageProvider.GetPublicUrl(filePath);
            var timestamp = _storageProvider.GetFile(filePath).GetLastUpdated().Ticks;
            return publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture);
        }