Hi Alex,
I'm already using it.
The images are uploaded as expected if I'm not trying to resize them, the thing is that I want to preprocess them before they get saved in order to reduce the image size.
Here is my modified version of that code which works with Azure Blob storage:
public void PreProcessMediaItem(IMedia item, IMediaService mediaService, MediaFileSystem mediaFileService)
{
var contentSection = UmbracoConfig.For.UmbracoSettings().Content;
var supportedTypes = contentSection.ImageFileTypes.ToList();
if (!item.HasProperty(Constants.Conventions.Media.File))
return;
if (string.IsNullOrEmpty(item.GetValue<string>(Constants.Conventions.Media.File)))
return;
var umbracoFileProp = item.GetValue(Constants.Conventions.Media.File);
string relativeImagePath;
if (umbracoFileProp.ToString().DetectIsJson())
{
var cropObject = JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFileProp.ToString());
relativeImagePath = cropObject.Src;
}
else
{
relativeImagePath = item.GetValue<string>(Constants.Conventions.Media.File);
}
// Make sure it's an image.
var extension = Path.GetExtension(relativeImagePath).Substring(1);
if (supportedTypes.InvariantContains(extension))
{
// Resize the image to _maxWidth wide, height is driven by the
// aspect ratio of the image.
using (var imageFactory = new ImageFactory(true))
{
var layer = new ResizeLayer(new Size(_maxWidth, 0), ResizeMode.Max)
{
Upscale = false
};
using (var imageFileStream = mediaFileService.OpenFile(relativeImagePath))
{
imageFactory.Load(imageFileStream).Resize(layer);
}
var resizedImage = imageFactory.Image;
var newWidth = resizedImage.Width;
var newHeight = resizedImage.Height;
long bytes;
using (var fileStream = new MemoryStream())
{
imageFactory.Save(fileStream);
fileStream.Position = 0;
mediaFileService.AddFile(relativeImagePath, fileStream, true);
bytes = fileStream.Length;
}
// Set the correct meta values for the image.
item.SetValue(Constants.Conventions.Media.Width, newWidth);
item.SetValue(Constants.Conventions.Media.Height, newHeight);
item.SetValue(Constants.Conventions.Media.Bytes, bytes);
}
}
}
public void PreProcessMediaItem(IMedia item, IMediaService mediaService, MediaFileSystem mediaFileService)
{
var contentSection = UmbracoConfig.For.UmbracoSettings().Content;
var supportedTypes = contentSection.ImageFileTypes.ToList();
if (!item.HasProperty(Constants.Conventions.Media.File))
return;
if (string.IsNullOrEmpty(item.GetValue<string>(Constants.Conventions.Media.File)))
return;
var umbracoFileProp = item.GetValue(Constants.Conventions.Media.File);
string relativeImagePath;
if (umbracoFileProp.ToString().DetectIsJson())
{
var cropObject = JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFileProp.ToString());
relativeImagePath = cropObject.Src;
}
else
{
relativeImagePath = item.GetValue<string>(Constants.Conventions.Media.File);
}
var extension = Path.GetExtension(relativeImagePath).Substring(1);
if (supportedTypes.InvariantContains(extension))
{
using (var imageFactory = new ImageFactory(true))
{
var layer = new ResizeLayer(new Size(1920, 0), ResizeMode.Max)
{
Upscale = false
};
using (var imageFileStream = mediaFileService.OpenFile(relativeImagePath))
{
imageFactory.Load(imageFileStream).Resize(layer);
}
var resizedImage = imageFactory.Image;
var newWidth = resizedImage.Width;
var newHeight = resizedImage.Height;
long bytes;
using (var fileStream = new MemoryStream())
{
imageFactory.Save(fileStream);
fileStream.Position = 0;
mediaFileService.AddFile(relativeImagePath, fileStream, true);
bytes = fileStream.Length;
}
item.SetValue(Constants.Conventions.Media.Width, newWidth);
item.SetValue(Constants.Conventions.Media.Height, newHeight);
item.SetValue(Constants.Conventions.Media.Bytes, bytes);
}
}
}
Which is using the accepted answer (thanks Tom), but the width and height don't update on the media file properties. The image is resized and the file size updates, but the width and height don't.
Image Preprocessing on upload to media
Hi everybody, I have the following code from: https://24days.in/umbraco-cms/2014/all-your-images-are-belong-to-umbraco/
The code works perfectly on my local env. but doesn't work with Azure blob on stage env.
Can you help me detecting what I'm doing wrong?
Thank you in advance.
Hi Dmitry
Welcome to our forum!
You have to use UmbracoFileSystemProviders.Azure for working with images in Azure, have a look - https://our.umbraco.org/projects/collaboration/umbracofilesystemprovidersazure/
/Alex
Hi Alex, I'm already using it. The images are uploaded as expected if I'm not trying to resize them, the thing is that I want to preprocess them before they get saved in order to reduce the image size.
Could someone give me a hint?
Hey Dmitry,
Here is my modified version of that code which works with Azure Blob storage:
Hope that helps.
Tom
Thank you very much! looks like it works PERFECTLY on my test env.
Hi Tom
Where do you call PreProcessMediaItem?
Greg
It is getting called in the "ApplicationStarting" event when you subscribe to the media saving event.
Hi Dmitry
Ah yeah I figured that out. I did change it slightly to not send in the mediaService and then removed the save.
So ended up with this:
And then removed this line in PreProcessMediaItem, as I'm already subscribing to the Save event:
This is really great, but there is a slight issue with the width / height. I'm hooking into the MediaService saving event.
then have:
Which is using the accepted answer (thanks Tom), but the width and height don't update on the media file properties. The image is resized and the file size updates, but the width and height don't.
Any ideas?
is working on a reply...