Copied to clipboard

Flag this post as spam?

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


  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 13:33
    Eduardo
    0

    Generating thumbnails backend

    Hi,

    I want to generate three formats(different sizes) of pictures when uploading an image throught the backend.

    How can I do this?

    Thanks beforehand.

    Sincere regards,
    Eduardo Macho

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 19, 2010 @ 13:40
    Jeroen Breuer
    0

    You could add 3 extra properties to the image media type and set those properties in the media event. Here is a sample I'm using for resizing the image in 1 format.

    Media.BeforeSave+=new Media.SaveEventHandler(Media_BeforeSave);
    
    protected void Media_BeforeSave(Media media, SaveEventArgs e)
    {
        //Check if the media item is an image.
        if (media.ContentType.Id == DefaultObjects.MediaTypeImage.Id)
        {
            try
            {
                //Get the image path.
                string umbracoFile = media.getProperty("umbracoFile").Value.ToString();
    
                //Remove the first slash from the path.
                umbracoFile = umbracoFile.Remove(0, 1);
    
                //Get the server path.
                string serverPath = HttpContext.Current.Server.MapPath("~");
    
                //Get some strings needed for building the paths.
                string umbracoFilePath = Path.Combine(serverPath, umbracoFile.Replace("/", "\\"));
                string fileName = Path.GetFileName(umbracoFile);
                string extension = Path.GetExtension(umbracoFile);
                string shortFileName = fileName.Replace(extension, string.Empty);
                string mediaPath = umbracoFile.Substring(0, umbracoFile.LastIndexOf("/"));
    
                //Get the image.
                byte[] file = File.ReadAllBytes(umbracoFilePath);
    
                //Resize the image to 800x600 and save it.
                string format800x600File = string.Concat(mediaPath, "/", shortFileName, "_resized", extension);
                string format800x600Path = Path.Combine(serverPath, format800x600File.Replace("/", "\\"));
                DefaultMethods.ResizeImage(file, format800x600Path, 800, 600, "HW");
    
                //Store the paths inside the properties.
                media.getProperty("resized").Value = string.Concat("/", format800x600File);
            }
            catch (Exception ex)
            {
                DigibizException.LogException(ex);
            }
        }
    }

    Or you just use the ImageCropper in Umbraco 4.5 :).

    Jeroen

  • jaygreasley 416 posts 403 karma points
    Oct 19, 2010 @ 13:40
    jaygreasley
    1

    Hey Eduardo,

    I'd start by looking into ImageGen http://our.umbraco.org/projects/website-utilities/imagegen

    hth

    Jay

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 13:43
    Eduardo
    0

    Hi guys,

    Thank you for your prompt answers.

    I am having a look at your examples.

    Regards,
    Eduardo Macho

  • Eduardo 106 posts 130 karma points
    Oct 19, 2010 @ 13:47
    Eduardo
    0

    Jeroen,

    Which file within Umbraco must contain that piece of code?

    Thanks beforehand.

    Regards,
    Eduardo

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 19, 2010 @ 13:57
    Jeroen Breuer
    0

    You need to put this piece of code in the ApplictionBase class. More info: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events.

    Jeroen

  • Rich Green 2246 posts 4008 karma points
    Oct 19, 2010 @ 14:38
    Rich Green
    0

    Eduardo,

    As Jay stated, I would strongly suggest you look at ImageGen, it is designed exactly for this purpose, you hold one image and you can have as many sizes/formats of it as you want.

    Rich

  • Eduardo 106 posts 130 karma points
    Oct 20, 2010 @ 11:19
    Eduardo
    0

    Jeroen,

    The image that I am uploading is a property of a document. When I create a new Document(node) I select an image from the hard disk with the upload data type.

    How could I resize this image?

    Regards,
    Eduardo Macho

  • Rich Green 2246 posts 4008 karma points
    Oct 20, 2010 @ 14:21
    Rich Green
    0

    Umbraco does this out the box, watch this video http://stream.umbraco.org/video/862329/umbraco-uk-festival-doug at @12:57

    Rich

  • Eduardo 106 posts 130 karma points
    Oct 20, 2010 @ 14:31
    Eduardo
    0

    Hi,

     

    I need to upload an image with a resolution of 300ppp, with this image I must be able to generate a version with a resolution of 72ppp.

    That is the problem.

    Regards,
    Eduardo

  • Rich Green 2246 posts 4008 karma points
    Oct 20, 2010 @ 14:33
    Rich Green
    0

    Are you sure ImageGen cannot do that?

    Rich

  • Eduardo 106 posts 130 karma points
    Oct 21, 2010 @ 14:18
    Eduardo
    0

    Hi,

    I used imagen for resizing images. It works perfectly.

    Thanks everyone.

    Regards,
    Eduardo Macho

  • jaygreasley 416 posts 403 karma points
    Oct 23, 2010 @ 13:00
    jaygreasley
    1

    Hey,

    Cool, glad it worked. feel free to high five me ;-)

    j

  • Eduardo 106 posts 130 karma points
    Oct 23, 2010 @ 13:29
    Eduardo
    0

    Hi Jay.

    I'd high five you If I could!! :)

    Regards,
    Eduardo Macho

  • John 88 posts 112 karma points
    Nov 08, 2010 @ 01:37
    John
    0

    So ImageGen generates the different image sizes on the fly? This could potentially add a lot of load to the web server...

  • John C Scott 473 posts 1183 karma points
    Nov 09, 2010 @ 16:15
    John C Scott
    0

    no as far as i understand it it only generates the image once and then saves that to use each time it is requested

    i've used imagegen in very high load websites and seen it cause no performance issues except the very first time ever an image size is requested and even then it doesn't take very much processor at all - seems to be very efficiently written

Please Sign in or register to post replies

Write your reply to:

Draft