Sitecore Command – Compress Image in Sitecore

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.

Media Library 
Default Website 
Experience Explorer 
Featu 
Files 
Founc 
Image 
prc 
Projex 
Systet 
Exten 
Content Editor 
Insert 
Search 
Compress 
Duplicate 
Delete 
Rename 
Copying 
Sorting 
Edit 
Experience Editor 
Publish Item 
Console 
Scripts 
Refresh

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.

PC New Volume (D:) > Websites Markl > scg10.sc > App_Config 
Name 
SitecoreCustom.Commands.config 
Date modified 
11/1/2019 12:15 PM 
> Include > Z.Foundation 
Type 
XML 
1 KB

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

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):

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.

O rete 
public cla55 : Connand 
/ 'Trying it via Command 
O references 
public override void context) 
AS er•t. (context , "context " ) ; 
'kdialtem iygeltem — null; 
if (context. Items. Length I) 
imagelt& = context. 
else { 
Log. Item Returned - Image Compress" 
/ Target Path/ Target 
this); 
public string GetTargetPathWithFiIeNaæ(mediaItem image) 
String E?rgetpath = E.ty 
targetpath image _ path; 
int startlndex — targetpath. ? • 
int count — targetpath. Length • targetpath.IndexOf(•?'); 
targetpath = targetpath.Remve(startIndex, count); 
return targetpath;

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.

Images 
Favicons 
Guardian 
Authors 
Logo 
Sports 
Profile Pictu 
Push 
SnakeRiv 
wp3157$ 
u 
Edit 
'G Project 
System 
Extension Ther 
Base Themes 
Themes 
tent Editor 
Media Lib 
(3 
X 
Insert 
Search 
Compress 
Duplicate 
Delete 
Rename 
Copying 
Sorting 
Experience Editor 
Publish Item 
Console 
Scripts 
Refresh
Foundation 
Images 
to Favicons 
Guardian 
Authors 
Logo 
to Sports 
Profile Pictures 
Push 
SnakeRiver5mb 
wp3157988png 
Project 
System 
Quic 
Item 
Item 
Item 
Tern I 
Crea 
Compressed I 
Item

4 thoughts on “Sitecore Command – Compress Image in Sitecore

Leave a comment

Design a site like this with WordPress.com
Get started