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

Forums / Developing with Sitefinity / Batch Resize Images

Batch Resize Images

3 posts, 0 answered
  1. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    15 Oct 2010
    Link to this post

    Hey folks, just had a client upload a lot of images at 1000px when they needed to be 256px. Is there a quick call you already have I can make from code to resize an image and then resave it?  If not, is it feasable to load each image as a a bitmap using the sitefinity resize querystring params and then just save that bitmap back as the Content of the IContent object I get from the LibraryManager?

    Thanks

    Drew

    ----------------------------
    Edit


    This is what Ive come up with so far based on another forum post, but the actual metadata for Size, Height, and Width dont seem to be updating in the db.  I think I climbed the major hurdle, but I would like to figure out why the meta data is not saving. 

    ---Code Snipped-- Solution below 
  2. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    15 Oct 2010
    Link to this post
    Consider this one solved. The below code will proportionally resize a library of images to a width of 256px
    I needed to get a stagedcontent object in order to persist the metadata. When using GetContent instead of GetStagedContent, only the content was saved, not the metadata.

    var libMgr = new LibraryManager();
           IList images = libMgr.GetImages(0, 0, "", new Guid[] { new Guid("a88c4230-df06-4197-a30a-7adf3199a6da") });
           foreach (IContent currentItem in images)
           {
               object data = currentItem.Content;
               if (data is byte[])
               {
                   if (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem))
                   {
                       long width = 0;
                       long height = 0;
                       object tstObj = currentItem.GetMetaData("Width");
                       if (tstObj != null)
                           width = Convert.ToInt64(tstObj);
                       if (width > 256)
                       {
                           System.Drawing.Image img =
                               System.Drawing.Image.FromStream(new System.IO.MemoryStream(data as byte[]));
                           img = Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail(256, int.MaxValue, img, true, true);
                             
                           var saveItem = libMgr.GetStagedContent(currentItem.ID) as StagedContent;
                           System.IO.MemoryStream stream = new System.IO.MemoryStream();
                           Telerik.Cms.Engine.ImagesHelper.SaveImageToStream(img, stream, saveItem.MimeType);
                           saveItem.Content = stream.GetBuffer();
                           saveItem.SetMetaData("Size", ((byte[])saveItem.Content).Length);
                           saveItem.SetMetaData("Width", img.Width);
                           saveItem.SetMetaData("Height", img.Height);
                           libMgr.SaveContent(saveItem, ContentStatus.Published);
                       }
                   }
               }
           }
  3. WebGeek
    WebGeek avatar
    186 posts
    Registered:
    20 Dec 2006
    06 Jul 2011
    Link to this post
    This looks like an excellent solution - did you insert your code in the LibraryItemUpload.ascx template?  If so, can you post your template file?
Register for webinar
3 posts, 0 answered