Axe
Axe

Reputation: 739

Orchard CMS ResizeMedia url from Controller.

I'm trying to use the

@Display.ResizeMediaUrl() 

In Orchard 1.7.2, However I need to get the value of the resized Media url in a controller so I can return it to a javascript function.

I can see in there is a

[shape]
public void ResizeMediaUrl

Shape in MediaShapes.cs (Orchard.MediaProcessing.Shapes) but I'm not sure how to use this. from a controller.

Upvotes: 0

Views: 593

Answers (1)

Mark
Mark

Reputation: 137

You'll need a reference to IImageProfileManager in your controller which you set up in the Constructor

private readonly IImageProfileManager _imageProfileManager;

Within the Controller Action you can call GetImageProfileUrl which will resize the image for you.

I've hacked this out of the ResizeMediaUrl shape and hardcoded the Mode, Alignment and Colour settings.

e.g.

private string DoTheResize(int Width, int Height, string path)
{
    var Mode = "pad";
    var Alignment = "middlecenter";
    var PadColor = "000000";

    var state = new Dictionary<string, string> {
        {"Width", Width.ToString(CultureInfo.InvariantCulture)},
        {"Height", Height.ToString(CultureInfo.InvariantCulture)},
        {"Mode", Mode},
        {"Alignment", Alignment},
        {"PadColor", PadColor},
    };

    var filter = new FilterRecord
    {
        Category = "Transform",
        Type = "Resize",
        State = FormParametersHelper.ToString(state)
    };

    var profile = "Transform_Resize"
        + "_w_" + Convert.ToString(Width)
        + "_h_" + Convert.ToString(Height)
        + "_m_" + Convert.ToString(Mode)
        + "_a_" + Convert.ToString(Alignment)
        + "_c_" + Convert.ToString(PadColor);

    var resizedImagePath = _imageProfileManager.GetImageProfileUrl(path, profile, filter);
    return resizedImagePath;
}

Upvotes: 3

Related Questions