|
Article relates to
|
Sitefinity 3.2 official release
|
|
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.
It seems quite natural to subscribe to the
Executing event of the
LibraryManager manager and modify the content before it is saved to the database. However, a bug in the official version of Sitefinity 3.2 would prevent you to do so. The bug is connected to the fact that when the
Executing event is executed after an item is uploaded, the content of that item is
null.
Note that this bug is resolved in Sitefinity 3.2 SP1 and the approach is different and can be checked out here:
Restricting the images size in Images & Documents.
To achieve the desired behavior, you should do the following:
1. Subscribe from the
Global.asax Application_Start event to the static
Executed event of the
LibraryManager manager like this:
| void Application_Start(object sender, EventArgs e) |
| { |
| Telerik.Libraries.LibraryManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(LibraryManager_Executed); |
| } |
| void LibraryManager_Executed(object sender, Telerik.ExecutedEventArgs args) |
| { |
| //The code continues from here |
| } |
2. In the
LibraryManager_Executed 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
ExecutedEventArgs parameter:
| if (args.CommandName != "DeleteContent") |
| { |
| if (args.Data is Telerik.Cms.Engine.IContent) |
| { |
| Telerik.Cms.Engine.IContent currentItem = Telerik.Libraries.LibraryManager.Providers["Libraries"].GetContent(((Telerik.Cms.Engine.IContent)args.Data).ID); |
| |
| //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 are 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 preserve the changes through the provider: