+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How can I crop images using the handler?

How can I crop images using the handler?

7 posts, 1 answered
  1. Bruno Michels
    Bruno Michels avatar
    125 posts
    Registered:
    16 Apr 2010
    28 May 2010
    Link to this post
    Is it possible to crop a image through the current handler (Sitefinity 3.7 SP3) ?

    For example:

    /Libraries/MyLib/my_image.tmb.ashx?CROPX=150&CROPY=150&width=50&height=50
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    28 May 2010
    Link to this post
    Hi Bruno Michels,

    You could create a cusom class that derives from Telerik.Cms.Engine.ContentHttpHandler. Override  ProcessContentItem method which loads an IContent item and fulfil the current response with that information.
    Out of the box there are four query strings that are available only for image files:
    • width, height -> the strings provide measures and the image is stretched to fit in them. Example:

      http://www.my_domain.com/MyFile.sflb?width=100&height=100

    • decreaseOnly -> when true, the image could only be decreased to fit in the measures set by the width and height strings. Still, if the image is smaller, it would not be enlarged. Example:

      http://www.my_domain.com/MyFile.sflb?decreaseOnly=true&width=100&height=100

    • proportional -> when true (by default), the image is stretched only proportionally until it fits in the measures set by the width and height strings.  Example: 

      http://www.my_domain.com/MyFile.sflb?proportional=true&width=100&height=100 


    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    02 Feb 2011
    Link to this post
    Hi Ivan,

    Could it be possible to extend the current handler for images so that the existing querystring options are preserved? I would like a handler that does all of this stuff including cropping.

    Regards,
    Daniel
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Feb 2011
    Link to this post
    Hi Daniel,

    ContentHttpHandler has a public method ResizeImage which you can override and implement your logic.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    07 Feb 2011
    Link to this post
    Hi Ivan,

    Would you be so kind to give me an example of how to achieve this? Not the logic for the crop, but the handler and how to register it?

    Regards,
    Daniel
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Feb 2011
    Link to this post
    Hello Daniel,

    Here is a basic sample

    using System.Web;
    using System;
     
    public class ContentHttpHandlerCustom : Telerik.Cms.Engine.ContentHttpHandler
    {
     
        protected override byte[] ResizeImage(byte[] imageData, string mimeType, HttpRequest request)
        {
            return base.ResizeImage(imageData, mimeType, request);
        }
         
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    08 Feb 2011
    Link to this post
    I Ivan,

    I used the same HttpHandler as for the .mp3 files. I override the ResizeImage() method, but it never fires.

    I added these two lines in the web.config:
    <add verb="*" path="*.sflb.ashx" type="CustomCmsContentHandler, App_Code" />

    <add name="sflbAshxHandler" path="*.sflb.ashx" verb="*" type="CustomCmsContentHandler, App_Code" resourceType="Unspecified" preCondition="integratedMode" />

    This is the code of my handler:
    /// <summary>  
    /// Summary description for CustomCmsContentHandler  
    /// </summary>  
    public class CustomCmsContentHandler : ContentHttpHandler
    {
        protected override object ProcessContentItem(IContent content, HttpRequest request)
        {
            string extension = VirtualPathUtility.GetExtension(HttpContext.Current.Request.Url.AbsolutePath);
            if (extension.Equals("*.mp3", StringComparison.OrdinalIgnoreCase) && !content.MimeType.StartsWith("audio/"))
            {
                throw new HttpException(404, "Not found");
            }
              
            return base.ProcessContentItem(content, HttpContext.Current.Request);
        }
      
        protected override byte[] ResizeImage(byte[] imageData, string mimeType, HttpRequest request)
        {
              
            return base.ResizeImage(imageData, mimeType, request);
        }
    }

    I can't get it to work :(

    Regards,
    Daniel
Register for webinar
7 posts, 1 answered