Skip to content

Instantly share code, notes, and snippets.

@bradleykronson
Last active July 17, 2017 06:21
Show Gist options
  • Save bradleykronson/3874540006482917ee15c6e70bffe247 to your computer and use it in GitHub Desktop.
Save bradleykronson/3874540006482917ee15c6e70bffe247 to your computer and use it in GitHub Desktop.
Media service event that prebuilds for imageprocessor.web (from http://24days.in/umbraco-cms/2014/all-your-images-are-belong-to-umbraco/)
public class ApplicationEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
// Tap into the Saving event
MediaService.Saving += (sender, args) =>
{
MediaFileSystem mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
IContentSection contentSection = UmbracoConfig.For.UmbracoSettings().Content;
IEnumerable<string> supportedTypes = contentSection.ImageFileTypes.ToList();
foreach (IMedia media in args.SavedEntities)
{
if (media.HasProperty("umbracoFile"))
{
// Make sure it's an image.
string path = media.GetValue<string>("umbracoFile");
string extension = Path.GetExtension(path).Substring(1);
if (supportedTypes.InvariantContains(extension))
{
// Resize the image to 1920px wide, height is driven by the
// aspect ratio of the image.
string fullPath = mediaFileSystem.GetFullPath(path);
using (ImageFactory imageFactory = new ImageFactory(true))
{
ResizeLayer layer = new ResizeLayer(new Size(1920, 0), ResizeMode.Max)
{
Upscale = false
};
imageFactory.Load(fullPath)
.Resize(layer)
.Save(fullPath);
}
}
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment