2

I am using ImageGen on Umbraco v7.1. So far so good and am able to get crop urls through my Razor code.

However, I need to get the crop url on my Api controller but am unsure how to do this.

This is how I am currently trying to get it.

ModelImage = Services.MediaService.GetById(galleryId).GetValue("modelImage").ToString()

...where ModelImage is an alias for the "Image Cropper" datatype. This returns the image url in the src property along with crop information (line breaks added):

ModelImage: "{
  "focalPoint": {
    "left": 0.5,
    "top": 0.5
  },
 "src": "/media/1828/bob-marley-thumbnail-update.jpg",
  "crops": [
    {
      "alias": "modelListCrop",
      "width": 298,
      "height": 380,
     "coordinates": {
        "x1": 0.071221447830289469,
        "y1":  .051177864855964005,
        "x2": 0.42344542232622806,
        "y2": 0.45016601603464318
      }
    }
  ]
}"

Does anyone know how I can get the crop url instead of the original image url?

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
Asher
  • 393
  • 3
  • 16
  • 1
    A little off topic, but in general, the only reason you would want to use the ContentService or MediaService is if you wanted to get an old version of the content or if you wanted to create, update, or delete. ContentService and MediaService both hit the database. You want to try to leverage the UmbracoHelper calls like `Umbraco.TypedContent` and `Umbraco.TypedMedia` whenever possible. Both of those calls hit the cache instead of the database. – bowserm Sep 17 '15 at 20:42
  • ImageGen? With the ImageCropper? Why? the cropper is powered by ImageProcessor. – James South Sep 23 '15 at 13:40

1 Answers1

6

You can get the crop URL if you retrieve the image node as IPublishedContent instead of IMedia. This allows you to use the GetCropUrl() method, passing in the alias of the Image Cropper property and the alias of the crop that you need.

var cropUrl = Umbraco.TypedMedia(galleryId).GetCropUrl("modelImage", "modelListCrop");
Rob Purcell
  • 1,285
  • 1
  • 9
  • 19
  • 1
    I am getting this error... 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'GetCropUrl' and no extension method 'GetCropUrl' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?) – Asher Apr 15 '15 at 10:25
  • 1
    Fixed this error by adding the Umbraco.Web namespace. Silly me :/ Thanks – Asher Apr 15 '15 at 10:32