Copied to clipboard

Flag this post as spam?

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


  • Sherry Ann Hernandez 320 posts 344 karma points
    Feb 12, 2011 @ 09:37
    Sherry Ann Hernandez
    0

    extending the file upload

    I'm creating a custom media file uploader because we wanted to add some custom validation. Question is how do I set the image thumbnail size generated by it?

    I'm a little confused on where to put the custom validation and the generation of thumbsize.

    Here's my code

    using

     

     

    System;

    using

     

     

    System.Collections.Generic;

    using

     

     

    System.Linq;

    using

     

     

    System.Text;

    using

     

     

    System.Web;

    using

     

     

    System.Drawing;

    using

     

     

    System.Drawing.Imaging;

    using

     

     

    System.Drawing.Drawing2D;

    using

     

     

    System.IO;

    using

     

     

    umbraco.cms.businesslogic.datatype;

    namespace

     

     

    DataEditorSettings.CustomDataEditor

    {

     

     

    class DataType : umbraco.cms.businesslogic.datatype.AbstractDataEditor

    {

     

     

    private MediaUploadControl control = new MediaUploadControl();

    [

     

    DataEditorSetting("Media Upload", description = "Media upload with custom validation")]

     

     

    public string Text { get; set; }

     

     

    // Set ID, needs to be unique

     

     

    public override Guid Id

    {

     

     

    get

    {

     

     

    return new Guid("49bcd88b-e2fe-4fde-93df-99e4b48cf04e");

    }

    }

     

     

    //Set name, (is what appears in data editor dropdown)

     

     

    public override string DataTypeName

    {

     

     

    get

    {

     

     

    return "Custom Media Upload";

    }

    }

     

     

    public DataType()

    {

     

     

    //set rendercontrol

     

     

    base.RenderControl = control;

     

     

    //init event

    control.Init +=

     

    new EventHandler(control_Init);

     

     

     

    //save event

     

     

    base.DataEditorControl.OnSave += new umbraco.cms.businesslogic.datatype.AbstractDataEditorControl.SaveEventHandler(DataEditorControl_OnSave);

    }

     

     

    void control_Init(object sender, EventArgs e)

    {

    }

     

     

    void DataEditorControl_OnSave(EventArgs e)

    {

     

     

    if (getGifOrJpg(control.Filename) || (validateFileSize(control.Filename, 100)))

    {

     

     

    base.Data.Value = control.Filename;

    }

    }

     

     

    public void createThumb(Stream srcImage, int thumbWidth, int thumbHeight, string fileName)

    {

     

     

    Size reqSize = new Size();

    reqSize.Width = thumbWidth;

    reqSize.Height = thumbHeight;

     

     

    Bitmap sourceImg = new Bitmap(srcImage);

     

     

    ImageFormat imgFormat = sourceImg.RawFormat;

     

     

    Size thumbSize = getSize(reqSize, sourceImg.Size);

     

     

    Bitmap thumbImage = null;

    thumbImage =

     

    new Bitmap(thumbSize.Width, thumbSize.Height);

     

     

    Graphics canvas = Graphics.FromImage(thumbImage);

    canvas.SmoothingMode =

     

    SmoothingMode.AntiAlias;

    canvas.InterpolationMode =

     

    InterpolationMode.HighQualityBicubic;

    canvas.PixelOffsetMode =

     

    PixelOffsetMode.HighQuality;

    canvas.DrawImage(sourceImg,

     

    new Rectangle(new Point(0, 0), thumbSize));

    thumbImage.Save(fileName);

    }

     

     

    private Size getSize(Size reqSize, Size currSize)

    {

     

     

    Size newSize = new Size();

     

     

    int reqWidth = reqSize.Width, reqHeight = reqSize.Height;

     

     

    int currWidth = currSize.Width;

     

     

    int currHeight = currSize.Height;

     

     

    int temp = 0;

     

     

    if (currWidth > reqWidth || currHeight > reqHeight)

    {

     

     

    if (currWidth > currHeight)

    {

    newSize.Width = reqWidth;

    temp = currHeight * newSize.Width;

    newSize.Height = temp / currWidth;

     

     

    if (newSize.Height > reqHeight)

    {

    newSize.Height = reqHeight;

    temp = currWidth * newSize.Height;

    newSize.Width = temp / currHeight;

    }

    }

     

     

    if (currWidth < currHeight)

    {

    newSize.Height = reqHeight;

    temp = currWidth * newSize.Height;

    newSize.Width = temp / currHeight;

     

     

    if (newSize.Width > reqWidth)

    {

    newSize.Width = reqWidth;

    temp = currHeight * newSize.Width;

    newSize.Height = temp / currWidth;

    }

    }

     

     

    if (currWidth == currHeight)

    {

    newSize.Width = reqWidth;

    temp = currHeight * newSize.Width;

    newSize.Height = temp / currWidth;

     

     

    if (newSize.Height > reqHeight)

    {

    newSize.Height = reqHeight;

    temp = currWidth * newSize.Height;

    newSize.Width = temp / currHeight;

    }

    }

    }

     

     

    else

    {

    newSize = currSize;

    }

     

     

    return newSize;

    }

     

     

    private bool getGifOrJpg(System.Web.UI.WebControls.FileUpload fle)

    {

     

     

    if (!fle.PostedFile.FileName.Trim().Equals(""))

    {

     

     

    string type = fle.PostedFile.ContentType;

     

     

    if (type == "image/pjpeg" || type == "image/gif" || type == "image/jpeg")

    {

     

     

    return true;

    }

     

     

    else

    {

     

     

    return false;

    }

    }

     

     

    else

    {

     

     

    return true;

    }

    }

     

     

    private bool validateFileSize(System.Web.UI.WebControls.FileUpload file, int size)

    {

     

     

    if (file.PostedFile.ContentLength <= size)

    {

     

     

    return true;

    }

     

     

    else

    {

     

     

    return false;

    }

    }

     

     

    private bool validateFileSizePath(HttpPostedFile file, int size)

    {

     

     

    if (file.ContentLength <= size)

    {

     

     

    return true;

    }

     

     

    else

    {

     

     

    return false;

    }

    }

     

     

    public bool validwidth(System.Web.UI.WebControls.FileUpload file, int wdt, int hgt)

    {

    System.Drawing.

     

    Image objimage = System.Drawing.Image.FromStream(file.PostedFile.InputStream);

     

     

    if ((objimage.Width == wdt) && (objimage.Height == hgt))

    {

     

     

    return true;

    }

     

     

    else

    {

     

     

    return false;

    }

    }

     

     

    public bool validdimension(HttpPostedFile file, int wdt, int hgt)

    {

    System.Drawing.

     

    Image objimage = System.Drawing.Image.FromStream(file.InputStream);

     

     

    if ((objimage.Width == wdt) && (objimage.Height == hgt))

    {

     

     

    return true;

    }

     

     

    else

    {

     

     

    return false;

    }

    }

    }

    }

  • Sherry Ann Hernandez 320 posts 344 karma points
    Feb 12, 2011 @ 13:21
    Sherry Ann Hernandez
    0

    Can somebody help me in working on this customized image file upload. When I uploaded the bin inside the bin folder the custom datatype is not displaying on my datatype list. :(

    This custom image file upload is somehow the same with the image resizer datatype in a way I need to generate a bigger thumbnail but at the same time I need to add custom validation for it like the valid image dimension, valid file size and etc.

     

Please Sign in or register to post replies

Write your reply to:

Draft