In our previous post we saw how we can use Sitecore Task Scheduler to read an RSS Feed and sync items in the Sitecore content tree.
In this post, we will work on creating a custom button for media items that compresses the images with our custom image compression logic.

This article is intended to demonstrate the use of a Sitecore Custom Command and how it can be extended. Hence, I have added a very simple compression logic. However, the logic can be extended to different requirement.
Also, if you are planning on optimizing the Media Items in entire Sitecore Media Library, you should have a look at https://kamsar.net/index.php/2014/07/automatic-sitecore-image-optimization-with-dianoga/
To create a Custom Command, we will first be adding the command name in a config as a patch and map that command to a class name/assembly.
To create a custom command, we need to have a class that extends Sitecore.Shell.Framework.Commands.Command and overrides it’s Execute method. So that, once that command is triggered the control will be passed to this method with the Context Item.
It is a best practice to create all our Custom Commands in a single config file and place them in the App_Config folder.

The Name we give for the command is the key here. This same “contentimage:compress” will be updated in the message field of the relevant item we create.
In this scenario, we will accomplishing the image compression by adding a new button – Compress under the Context Menues. To add a new item under Context Menu, we need to create a Menu Item in Core Database under “/sitecore/content/Applications/Content Editor/Context Menues/Default”. We will name it as Compress.
![Content Editor
Applications
Context Menues
Default
Insert
Search
Compress
Divider 3
cut
Copy
Paste
Divider 2
Duplicate
Delete
Rename
Divider I
Copying
Compress
Quick Info
Item ID:
Item name:
Item path:
Template:
Created from:
Item owner:
Action [shared):
{091 CSS02-8997-4E3F-94F&609500E051 B9)
Compress
/sitecore/contentJApplications/Content Editor/Context Menues}Default/Compress
/sitecore/tempIates/System/MenusJMenu item (998B965E6AB84568-810F8101 D60DOCC3}
[unknown]
sitecore\Admin](https://pushpaganan.home.blog/wp-content/uploads/2019/11/compress-menu-item.png?w=1024)
Now, we can update the relevant fields – Display Name and Message. Display Name is the name of the option displayed in the Menu and Message is the command name (The Name we have give in the command config patch)
![Display name [unvers.onedl:
Compress
Hotkey LunversionedJ:
Icon (shared]:
Open icon
ID [shared]:
Clear
Message [shared):](https://pushpaganan.home.blog/wp-content/uploads/2019/11/menu-item-field-values.png?w=697)
Now, let’s move on to the logic part.
Here, when the compress option is clicked, we will read the context item(Media Item) and then it’s image, convert it to a stream, use System.Drawing namespace and it’s associated methods to compress the image, reducing it’s size without losing it’s quality. Once that image is compressed, it will created as a new Sitecore Media Item and will be added back to the Sitecore Media Library. Again, the image compression logic can be created based on any compression algorithms. To support this, we are converting the image to a stream. This stream can be passed on to any image compression algorithms and can be processed further.

Update the scale Factor with values to have different ratio of compression


Points to be Noted:
- Once you have created a new Menu option in Core DB, under /sitecore/content/Applications/Content Editor/Context Menues/Default this Option will be displayed for all items in the Content tree.
- But it doesn’t make any sense for every Sitecore item to have a compress option and it is not a best practice as well.
- So that leads us to, Only the Image and it relevant items should only have this Compress option enabled.
This can be achieved by overriding the QueryState method. Here we handle the scenarios when we have to enable the Compress Option and when we have to hide it.

The Template IDs we refer here are Sitecore Image/Jpeg media item’s template. So this option will be shown only for images in Sitecore.


4 thoughts on “Sitecore Command – Compress Image in Sitecore”