|
Article relates to
|
Sitefinity 3.2 SP1
|
|
Created by
|
Vassil Daskalov
|
Sometimes there are situations when you would like to set additional adjustments to items before or after they are stored in the database. Sitefinity provides two events for the Generic Content based modules suitable for such scenarios. These are the
Executed and
Executing events. Note that they are static and you would not need an instance of a specific manager to use them.
A good example for using one of these events is when restricting the size of the image items you are uploading inside the
Images & Documents module. As we know, photo cameras make really big images with a lot of info on them. In most cases, though, we do not need that much information. It would be rather annoying to view such images as we should use the browser scrolls in order to take a look at the whole image. Perhaps reducing the images to 900x500 proportions would be quite enough.
To achieve the desired behavior, you should do the following:
1. Subscribe from the
Global.asax Application_Start event to the static
Executing event of the
LibraryManager manager like this:
| void Application_Start(object sender, EventArgs e) |
| { |
| Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing); |
| } |
| |
| void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs args) |
| { |
| //The code continues here |
| } |
| |
2. In the
LibraryManager_Executing event, you first need to assure that the event is not fired after deletion of an item as the modification in this case would be illogical. Afterwards, load the item that has just been saved via the
ExecutingEventArgs parameter:
| if (args.CommandName != "DeleteContent") |
| { |
| if (args.CommandArguments is Telerik.Cms.Engine.IContent) |
| { |
| Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)args.CommandArguments); |
| |
| //The code continues here |
| } |
| } |
3. You should assure that the content load is a browseable image, that is, it should have one of the following extensions:
.jpg,
.jpeg,
.gif or
.png. Now get the item’s width and height and check if they are larger than the 900x500 dimensions:
| if (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem)) |
| { |
| long width = 0; |
| long height = 0; |
| |
| object o2 = currentItem.GetMetaData("Width"); |
| if (o2 != null) |
| width = Convert.ToInt64(o2); |
| |
| o2 = currentItem.GetMetaData("Height"); |
| if (o2 != null) |
| height = Convert.ToInt64(o2); |
| |
| if (width > 900 || height > 500) |
| { |
| //The code continues here |
| } |
| } |
| |
4. In this case you should instantiate a
System.Drawing.Image object and call the
Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail() method. This method has three overloads. The one used here generates a new image with the given width and height with proportional dimensions and creates that image only if its width or height is larger than the parameters given to the function:
| System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(currentItem.Content as byte[])); |
| img = Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail(900, 500, img, true, true); |
| |
| //The code continues here |
5. Afterwards, you should save the newly generated image to a memory stream. Set the
Content property of the item’s object to the stream content
and finally update Width, Height and Size meta fields.
| System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
| Telerik.Cms.Engine.ImagesHelper.SaveImageToStream(img, stream, currentItem.MimeType); |
| currentItem.Content = stream.GetBuffer(); |
| currentItem.SetMetaData("Size", ((byte[])currentItem.Content).Length); |
| currentItem.SetMetaData("Width", img.Width); |
| currentItem.SetMetaData("Height", img.Height); |
| |
Finally, the whole code needed is contained in the Global.asax file and looks like the one below:
| void Application_Start(object sender, EventArgs e) |
| { |
| Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing); |
| } |
| |
| void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs args) |
| { |
| if (args.CommandName != "DeleteContent") |
| { |
| if (args.CommandArguments is Telerik.Cms.Engine.IContent) |
| { |
| Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)args.CommandArguments); |
| |
| if (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem)) |
| { |
| long width = 0; |
| long height = 0; |
| |
| object o2 = currentItem.GetMetaData("Width"); |
| if (o2 != null) |
| width = Convert.ToInt64(o2); |
| |
| o2 = currentItem.GetMetaData("Height"); |
| if (o2 != null) |
| height = Convert.ToInt64(o2); |
| |
| if (width > 900 || height > 500) |
| { |
| System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(currentItem.Content as byte[])); |
| img = Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail(900, 500, img, true, true); |
| |
| System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
| Telerik.Cms.Engine.ImagesHelper.SaveImageToStream(img, stream, currentItem.MimeType); |
| currentItem.Content = stream.GetBuffer(); |
| |
| currentItem.SetMetaData("Size", ((byte[])currentItem.Content).Length); |
| currentItem.SetMetaData("Width", img.Width); |
| currentItem.SetMetaData("Height", img.Height); |
| } |
| } |
| } |
| } |
| } |