Beispiel #1
0
        /// <summary> </summary>
        public static Image ConstrainProportions(Image imgPhoto, int Size, image_handler.Dimensions Dimension)
        {
            log.Info(" in ConstrainProportions ");
            //stellar.Services.LogService.writelog(" in ConstrainProportions ");
            int   sourceWidth  = imgPhoto.Width;
            int   sourceHeight = imgPhoto.Height;
            int   sourceX      = 0;
            int   sourceY      = 0;
            int   destX        = 0;
            int   destY        = 0;
            float nPercent     = 0;

            switch (Dimension)
            {
            case image_handler.Dimensions.Width:
                nPercent = ((float)Size / (float)sourceWidth);
                break;

            default:
                nPercent = ((float)Size / (float)sourceHeight);
                break;
            }

            int destWidth  = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);

            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            log.Info(" in ConstrainProportions mid drawing:");
            //stellar.Services.LogService.writelog(" in ConstrainProportions mid drawing:" );
            grPhoto.DrawImage(imgPhoto,
                              new Rectangle(destX - 1, destY - 1, destWidth + 1, destHeight + 1),
                              new Rectangle(sourceX, sourceY, sourceWidth - 1, sourceHeight - 1),
                              // new Rectangle(destX,destY,destWidth,destHeight),
                              // new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
                              GraphicsUnit.Pixel);
            log.Info(" in ConstrainProportions before Dispose ");
            //stellar.Services.LogService.writelog(" in ConstrainProportions before Dispose");
            //grPhoto.Dispose();
            return(bmPhoto);
        }
        public void Download(int id, int eventsid, int w, int h, int p, string m, bool protect, string pre, string mark, int maxage, bool nocache, bool mug)
        {
            CancelLayout();
            CancelView();
            log.Info("Starting download for image id " + id);
            posting image = ActiveRecordBase <posting> .Find(id);

            string uploadPath = "";

            if (image.static_file != null)
            {
                uploadPath = image.static_file;
                String fullpath = id + ".ext";
                uploadPath = Regex.Replace(uploadPath, "(.*)(\\\\.*?)(.*)", "$1");
                if (!uploadPath.EndsWith("\\"))
                {
                    uploadPath += "\\";
                }
            }
            else
            {
                // build the path for the new image
                uploadPath = Context.ApplicationPath + @"\uploads\";
                if (mug)
                {
                    uploadPath += @"mugshots\";
                }

                if (eventsid != 0)
                {
                    posting events = ActiveRecordBase <posting> .Find(eventsid);

                    uploadPath += @"events\" + events.id + @"\";

                    //check for events level image existence
                    string orgFile = HttpContext.Server.MapPath(uploadPath + id + ".ext");
                    if (!File.Exists(orgFile))
                    {
                        //it didn't so lets take a look at the pool for the image
                        string newuploadPath = Context.ApplicationPath + @"\uploads\";
                        string neworgFile    = HttpContext.Server.MapPath(newuploadPath + id + ".ext");
                        if (File.Exists(neworgFile))
                        {
                            uploadPath = Context.ApplicationPath + @"\uploads\";
                        }
                    }
                }
            }
            if (String.IsNullOrWhiteSpace(m))
            {
                m = "constrain";
                w = 1000;//this will be site prefenece for max served iamges.
                h = 1000;
            }

            string arg = (!String.IsNullOrEmpty(pre) ? "_" + pre + "_" : "");

            arg += (w != 0 ? "w_" + w + "_" : "");
            arg += (h != 0 ? "h_" + h + "_" : "");
            arg += (p != 0 ? "p_" + p + "_" : "");
            arg += (protect != false ? "pro_true_" : "");
            arg += (!String.IsNullOrEmpty(m) ? "m_" + m + "_" : "");
            arg += (!String.IsNullOrEmpty(mark) ? "mark_" + mark + "_" : "");


            string newFile = HttpContext.Server.MapPath(uploadPath + id + arg + ".ext");

            // if the process image doesn't Exist yet create it
            if (!File.Exists(newFile))
            {
                string baseFile = uploadPath + id + ".ext";
                if (!File.Exists(baseFile))
                {
                    baseFile = uploadPath + image.static_file;
                }

                System.Drawing.Image processed_image = System.Drawing.Image.FromFile(HttpContext.Server.MapPath(baseFile));
                //set some defaults
                image_handler.imageMethod methodChoice = image_handler.imageMethod.Percent;
                image_handler.Dimensions  dimensional  = image_handler.Dimensions.Width;

                //choose medth of sizing and set their defaults
                switch (m)
                {
                case "percent":
                    methodChoice = image_handler.imageMethod.Percent;
                    break;

                case "constrain":
                    methodChoice = image_handler.imageMethod.Constrain;
                    dimensional  = w != 0 ? image_handler.Dimensions.Width : image_handler.Dimensions.Height;
                    break;

                case "fixed":
                    methodChoice = image_handler.imageMethod.Fixed;
                    break;

                case "crop":
                    methodChoice = image_handler.imageMethod.Crop;
                    break;
                }
                new image_handler().process(id, processed_image, newFile, methodChoice, p, h, w, dimensional, protect, mark, image.get_meta("ext"));
            }

            // Read in the file into a byte array
            byte[] contents = null;
            try {
                contents = File.ReadAllBytes(HttpContext.Server.MapPath(uploadPath + id + arg + ".ext"));
            } catch (Exception ex) {
                log.Error("Error uploading file", ex);
            }

            HttpContext.Response.ClearContent();
            HttpContext.Response.ClearHeaders();
            if (contents != null)
            {
                String contentDisposition = "inline; filename=\"" + image.get_meta("filename") + arg + "." + image.get_meta("ext") + "\"";

                HttpContext.Response.Clear();
                String contentType = "applicaton/image";
                switch (image.get_meta("ext").ToLower())
                {
                case "gif":
                    contentType = "image/gif";
                    break;

                case "png":
                    contentType = "image/png";
                    break;

                case "jpg":
                case "jpe":
                case "jpeg":
                    contentType = "image/jpeg";
                    break;

                case "bmp":
                    contentType = "image/bmp";
                    break;

                case "tif":
                case "tiff":
                    contentType = "image/tiff";
                    break;

                case "eps":
                    contentType = "application/postscript";
                    break;

                default:
                    contentDisposition = "attachment; filename=\"" + image.get_meta("filename") + arg + "." + image.get_meta("ext") + "\"";
                    contentType        = "application/" + image.get_meta("ext").ToLower();
                    break;
                }

                // Setup the response
                HttpContext.Response.Buffer = true;
                HttpContext.Response.AddHeader("Content-Length", contents.Length.ToString());
                DateTime dt = DateTime.Now.AddYears(1);
                HttpContext.Response.Cache.SetExpires(dt);
                HttpContext.Response.Cache.SetMaxAge(new TimeSpan(dt.ToFileTime()));
                HttpContext.Response.Cache.SetValidUntilExpires(true);
                HttpContext.Response.Cache.SetCacheability(HttpCacheability.Public);
                HttpContext.Response.Expires     = 0;
                HttpContext.Response.ContentType = contentType;
                //HttpContext.Response.AddHeader("Content-Disposition", "inline; filename=\"" + image.FileName + arg + "." + image.Ext + "\"");
                HttpContext.Response.Cache.SetMaxAge(new TimeSpan(84, 0, 0, 0, 0));
                // Write the file to the response
                HttpContext.Response.BinaryWrite(contents);
            }
            log.Info("Finished download for image id " + id + ", length: " + contents.Length.ToString() + " bytes");
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
Beispiel #3
0
        /// <summary> </summary>
        public static String image(int id, int w, int h, int p, string m, bool protect, string pre, string mark, bool nocache)
        {
            posting image = ActiveRecordBase <posting> .Find(id);

            string uploads_path = file_info.site_uploads_path();

            string[] generalized_file_path = image.static_file.Split(new String[] { "uploads/", "images/" }, StringSplitOptions.RemoveEmptyEntries);
            string   file_path             = file_handler.normalize_path(uploads_path.Trim('/') + "/images/" + generalized_file_path[generalized_file_path.Length - 1].Trim('/'));

            String[] fileparts = file_path.Split('.');
            string   ext       = fileparts[fileparts.Length - 1];

            string cache_path  = file_info.site_cache_path().Trim('/') + "/uploads/images/";
            string cached_file = cache_path + generalized_file_path[generalized_file_path.Length - 1].Trim('/');

            string cache_url  = file_info.relative_site_cache_path().Trim('/') + "/uploads/images/";
            string cached_url = cache_url + generalized_file_path[generalized_file_path.Length - 1].Trim('/');


            if (String.IsNullOrWhiteSpace(m))
            {
                m = "constrain";
                w = 1000;//this will be site prefenece for max served iamges.
                h = 1000;
            }

            string arg = (!String.IsNullOrEmpty(pre) ? "_" + pre + "_" : "");

            arg += (w != 0 ? "w_" + w + "_" : "");
            arg += (h != 0 ? "h_" + h + "_" : "");
            arg += (p != 0 ? "p_" + p + "_" : "");
            arg += (protect != false ? "pro_true_" : "");
            arg += (!String.IsNullOrEmpty(m) ? "m_" + m + "_" : "");
            arg += (!String.IsNullOrEmpty(mark) ? "mark_" + mark + "_" : "");



            String[] c_file_parts     = cached_file.Split(new string[] { "." + ext }, StringSplitOptions.None);
            string   cached_file_path = c_file_parts[0] + id + arg + "." + ext;

            String[] c_cached_url    = cached_url.Split(new string[] { "." + ext }, StringSplitOptions.None);
            string   cached_file_url = c_cached_url[0] + id + arg + "." + ext;


            // if the process image doesn't Exist yet create it
            if (!File.Exists(cached_file_path))
            {
                System.Drawing.Image processed_image = null;
                try{
                    processed_image = System.Drawing.Image.FromFile(file_path);
                } catch (Exception ex) {
                    throw new IOException(ex.Message.Insert(ex.Message.Length, " " + file_path));
                }
                if (processed_image != null)
                {
                    //set some defaults
                    image_handler.imageMethod methodChoice = image_handler.imageMethod.Percent;
                    image_handler.Dimensions  dimensional  = image_handler.Dimensions.Width;

                    //choose medth of sizing and set their defaults
                    switch (m)
                    {
                    case "percent":
                        methodChoice = image_handler.imageMethod.Percent;
                        break;

                    case "constrain":
                        methodChoice = image_handler.imageMethod.Constrain;
                        dimensional  = w != 0 ? image_handler.Dimensions.Width : image_handler.Dimensions.Height;
                        break;

                    case "fixed":
                        methodChoice = image_handler.imageMethod.Fixed;
                        break;

                    case "crop":
                        methodChoice = image_handler.imageMethod.Crop;
                        break;
                    }
                    new image_handler().process(id, processed_image, cached_file_path, methodChoice, p, h, w, dimensional, protect, mark, image.get_meta("ext"));
                }
            }
            return(cached_file_url);
        }