You could add 3 extra properties to the image media type and set those properties in the media event. Here is a sample I'm using for resizing the image in 1 format.
Media.BeforeSave+=new Media.SaveEventHandler(Media_BeforeSave);
protected void Media_BeforeSave(Media media, SaveEventArgs e)
{
//Check if the media item is an image.
if (media.ContentType.Id == DefaultObjects.MediaTypeImage.Id)
{
try
{
//Get the image path.
string umbracoFile = media.getProperty("umbracoFile").Value.ToString();
//Remove the first slash from the path.
umbracoFile = umbracoFile.Remove(0, 1);
//Get the server path.
string serverPath = HttpContext.Current.Server.MapPath("~");
//Get some strings needed for building the paths.
string umbracoFilePath = Path.Combine(serverPath, umbracoFile.Replace("/", "\\"));
string fileName = Path.GetFileName(umbracoFile);
string extension = Path.GetExtension(umbracoFile);
string shortFileName = fileName.Replace(extension, string.Empty);
string mediaPath = umbracoFile.Substring(0, umbracoFile.LastIndexOf("/"));
//Get the image.
byte[] file = File.ReadAllBytes(umbracoFilePath);
//Resize the image to 800x600 and save it.
string format800x600File = string.Concat(mediaPath, "/", shortFileName, "_resized", extension);
string format800x600Path = Path.Combine(serverPath, format800x600File.Replace("/", "\\"));
DefaultMethods.ResizeImage(file, format800x600Path, 800, 600, "HW");
//Store the paths inside the properties.
media.getProperty("resized").Value = string.Concat("/", format800x600File);
}
catch (Exception ex)
{
DigibizException.LogException(ex);
}
}
}
Or you just use the ImageCropper in Umbraco 4.5 :).
As Jay stated, I would strongly suggest you look at ImageGen, it is designed exactly for this purpose, you hold one image and you can have as many sizes/formats of it as you want.
The image that I am uploading is a property of a document. When I create a new Document(node) I select an image from the hard disk with the upload data type.
no as far as i understand it it only generates the image once and then saves that to use each time it is requested
i've used imagegen in very high load websites and seen it cause no performance issues except the very first time ever an image size is requested and even then it doesn't take very much processor at all - seems to be very efficiently written
Generating thumbnails backend
Hi,
I want to generate three formats(different sizes) of pictures when uploading an image throught the backend.
How can I do this?
Thanks beforehand.
Sincere regards,
Eduardo Macho
You could add 3 extra properties to the image media type and set those properties in the media event. Here is a sample I'm using for resizing the image in 1 format.
Or you just use the ImageCropper in Umbraco 4.5 :).
Jeroen
Hey Eduardo,
I'd start by looking into ImageGen http://our.umbraco.org/projects/website-utilities/imagegen
hth
Jay
Hi guys,
Thank you for your prompt answers.
I am having a look at your examples.
Regards,
Eduardo Macho
Jeroen,
Which file within Umbraco must contain that piece of code?
Thanks beforehand.
Regards,
Eduardo
You need to put this piece of code in the ApplictionBase class. More info: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events.
Jeroen
Eduardo,
As Jay stated, I would strongly suggest you look at ImageGen, it is designed exactly for this purpose, you hold one image and you can have as many sizes/formats of it as you want.
Rich
Jeroen,
The image that I am uploading is a property of a document. When I create a new Document(node) I select an image from the hard disk with the upload data type.
How could I resize this image?
Regards,
Eduardo Macho
Umbraco does this out the box, watch this video http://stream.umbraco.org/video/862329/umbraco-uk-festival-doug at @12:57
Rich
Hi,
I need to upload an image with a resolution of 300ppp, with this image I must be able to generate a version with a resolution of 72ppp.
That is the problem.
Regards,
Eduardo
Are you sure ImageGen cannot do that?
Rich
Hi,
I used imagen for resizing images. It works perfectly.
Thanks everyone.
Regards,
Eduardo Macho
Hey,
Cool, glad it worked. feel free to high five me ;-)
j
Hi Jay.
I'd high five you If I could!! :)
Regards,
Eduardo Macho
So ImageGen generates the different image sizes on the fly? This could potentially add a lot of load to the web server...
no as far as i understand it it only generates the image once and then saves that to use each time it is requested
i've used imagegen in very high load websites and seen it cause no performance issues except the very first time ever an image size is requested and even then it doesn't take very much processor at all - seems to be very efficiently written
is working on a reply...