Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robin Hansen 135 posts 368 karma points
    Jan 13, 2015 @ 13:22
    Robin Hansen
    0

    Access all crops in ContentService

    Hi,

    I stumpled upon this service while searching on the net - it really fulfill my needs - short story long: I use the ImageCropper one of my doctypes. Every time I publish a noce of that doctype I would like to generade a list of all the crops associated to the cropper - I do this by using this ContentService http://our.umbraco.org/documentation/reference/events-v6/ContentService-Events

    My Code looks like this:

    using System;
    using System.IO;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;
    using umbraco.cms.businesslogic.web;
    namespace My.WebSite
    {
        public class createIcons : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentServicePublished;
            }

            private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                foreach (var node in args.PublishedEntities)
                {
                    if (node.ContentType.Alias == "RadioChannel" || node.ContentType.Alias == "Podcast")
                    {
                        var fileName = String.Format("{0}test.txt", AppDomain.CurrentDomain.BaseDirectory);

                        using (StreamWriter writer = new StreamWriter(fileName))
                        {
                            var doc = new Document(node.Id);
                            var icon = doc.getProperty("PlayerIcon").Value;
                            writer.Write("{0}", node.Name + " - " + node.Id + " - " + icon);
                        }
                    }
                }
            }
        }
    }

    test.txt looks like this:

    my-node - 1282 - {
      "focalPoint": {
        "left": 0.5,
        "top": 0.5
      },
      "src": "/media/563971/webplayer_ikoner_165.png",
      "crops": [
        {
          "alias": "icon_57",
          "width": 57,
          "height": 57
        },
        {
          "alias": "icon_72",
          "width": 72,
          "height": 72
        },
        {
          "alias": "icon_114",
          "width": 114,
          "height": 114
        },
        {
          "alias": "icon_144",
          "width": 144,
          "height": 144
        }
      ]
    }

    The documentation says I can iterate through the crops - but How do I do this in a ContentService?

    @foreach(var crop in CurrentPage.image.crops){
        <img src="@CurrentPage.GetCropUrl("image", crop.alias)">    
    } 

     

     

     

     

     

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 13, 2015 @ 17:46
    Jeavon Leopold
    0

    Hi Robin,

    I'm not 100% on what you are doing, do you want to use GetCropUrl with IContent or just iterate the crops?

    I do a ton of stuff with the setting the ImageCropper values in my Crop Healer package, so you might find something useful in here

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft