# Adding a Rotating-Image Functionality to the ImageEditor

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC ImageEditor</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2022.2.621 version</td>
 </tr>
</table>

## Description

How can I add a functionality for rotating images in the Telerik UI for ASP.NET MVC ImageEditor?

## Solution

To achieve the desired scenario:

1. Add a custom button in the ToolBar of the ImageEditor that executes a custom command.
2. When the command is triggered, execute custom logic to rotate the image.

```Razor CustomCommand
        .Toolbar(toolbar => toolbar.Items(i =>
        {
            i.Add().Command("RotateImageRightImageEditorCommand").Type("button").Text("Rotate Image");

        }))         
```

```JS script.js
    $(document).ready(function () {
        var imageEditor = $("#imageEditor").getKendoImageEditor();
        imageEditor.one("imageRendered", function () {
            imageEditor.executeCommand({ command: "ZoomImageEditorCommand", options: imageEditor.getZoomLevel() - 5.0 });
        });
    });


    var imageEditorNS = kendo.ui.imageeditor;
    imageEditorNS.commands.RotateImageRightImageEditorCommand = imageEditorNS.ImageEditorCommand.extend({
        exec: function () {
            var that = this,
                imageeditor = that.imageeditor,
                canvas = imageeditor.getCanvasElement(),
                ctx = imageeditor.getCurrent2dContext(),
                image = imageeditor.getCurrentImage();

            let degrees = 90; //rotate right

            canvas.width = image.height;
            canvas.height = image.width;

            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.translate(image.height / 2, image.width / 2);

            ctx.rotate(degrees * Math.PI / 180);
            ctx.drawImage(image, -image.width / 2, -image.height / 2);

            imageeditor.drawImage(canvas.toDataURL()).done(function (image) {
                imageeditor.drawCanvas(image);
            }).fail(function (ev) {
                imageeditor.trigger("error", ev);
            });
        }
    });
```

For the complete implementation of approach suggested above, refer to the following [Telerik REPL](https://netcorerepl.telerik.com/cwEtcMkf58Vhoi8t58).

## More ASP.NET MVC ImageEditor Resources

* [ASP.NET MVC ImageEditor Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/imageeditor/overview)

* [ASP.NET MVC ImageEditor Demos](https://demos.telerik.com/aspnet-mvc/imageeditor)

* [ASP.NET MVC ImageEditor Product Page](https://www.telerik.com/aspnet-mvc/image-editor)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Adding a Rotating-Image Functionality to the ImageEditor](https://netcorerepl.telerik.com/cwEtcMkf58Vhoi8t58)
* [Client-Side API Reference of the ImageEditor for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/imageeditor)
* [Server-Side API Reference of the ImageEditor for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/imageeditor)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
