Exemplo n.º 1
0
        public void ResizeImage(HttpContext context)
        {
            Stream imageStream   = null;
            var    imageNodePath = HttpContext.Current.Request.FilePath;

            var content     = Content.Create(PortalContext.Current.ContextNode);
            var contentPath = "";

            var contentFileName = "";
            var contentId       = -1;

            CheckCacheFolder();

            if (!string.IsNullOrEmpty(imageNodePath))
            {
                if (ImageType == "Binary")
                {
                    var contentBinary = content.Fields["Binary"].GetData() as BinaryData;

                    if (contentBinary == null)
                    {
                        throw new Exception("Can not read Binary field from the given Content. Doesn't exists?");
                    }

                    imageStream     = contentBinary.GetStream();
                    contentFileName = content.Name;
                    contentId       = content.Id;
                }
                else if (ImageType == "ImageData")
                {
                    if (!String.IsNullOrEmpty(ImageFieldName))
                    {
                        try
                        {
                            var contentImageFieldData = content.Fields[ImageFieldName].GetData() as SenseNet.ContentRepository.Fields.ImageField.ImageFieldData;

                            if (contentImageFieldData.ImgData.Size > 0)
                            {
                                imageStream     = contentImageFieldData.ImgData.GetStream();
                                contentFileName = new FileInfo(contentImageFieldData.ImgData.FileName.FullFileName).Name;
                                contentId       = contentImageFieldData.ImgData.Id;
                            }
                            else
                            {
                                imageStream     = contentImageFieldData.ImgRef.Binary.GetStream();
                                contentFileName = new FileInfo(contentImageFieldData.ImgRef.Path).Name;
                                contentId       = contentImageFieldData.ImgRef.Id;
                            }
                        }
                        catch (Exception)
                        {
                            throw new Exception("Invalid Image Field Name was given in the application.");
                        }
                    }
                    else
                    {
                        throw new Exception("There was no ImageFieldName specified when using ImageData as ImageType.");
                    }
                }
                else if (ImageType == "Reference")
                {
                    if (!String.IsNullOrEmpty(ImageFieldName))
                    {
                        try
                        {
                            // ami még ide hiányzik:
                            // a GetData visszatérhet "null"-al, "Node"-ként illetve "List<Node>"-ként
                            // ebből már egy meg van valósítva, a többit kell még lekezelni
                            var referenceField   = content.Fields[ImageFieldName].GetData() as List <Node>; //.GetType();// as ReferenceField;
                            var refContent       = Content.Create(referenceField[0]);
                            var refContentBinary = refContent.Fields["Binary"].GetData() as BinaryData;
                            imageStream     = refContentBinary.GetStream();
                            contentFileName = refContent.Name;
                            contentId       = refContent.Id;
                        }
                        catch (Exception)
                        {
                            //TODO: empty catch block
                        }
                    }
                    else
                    {
                        throw new Exception("There was no ImageFieldName specified when using ImageData as ImageType.");
                    }
                }
                else if (ImageType == "Attachment")
                {
                    if (!String.IsNullOrEmpty(ImageFieldName))
                    {
                        try
                        {
                            var binary = content.Fields[ImageFieldName].GetData() as BinaryData;
                            imageStream     = binary.GetStream();
                            contentFileName = new FileInfo(binary.FileName.FullFileName).Name;
                            contentId       = binary.Id;
                        }
                        catch (Exception)
                        {
                            throw new Exception(
                                      String.Format("The given image field field '{0}' is not a valid binary field of an image.", ImageFieldName));
                        }
                    }
                }

                // generating contentPath
                int lastDotIndex = contentFileName.LastIndexOf('.');
                contentPath = lastDotIndex != -1
                                  ? contentFileName.Insert(lastDotIndex, String.Format("_{1}{0}", contentId.ToString(), ResizeType == ResizeTypeList.Resize ? "R_" : "C_"))
                                  : String.Format("{0}_{2}{1}", contentFileName, contentId.ToString(), ResizeType == ResizeTypeList.Resize ? "R_" : "C_");

                if (IsCached(contentPath))
                {
                    FlushCachedFile(contentPath, context);
                    return;
                }

                if (ResizeType == ResizeTypeList.Resize)
                {
                    //Resize image
                    imageStream = ImageResizer.CreateResizedImageFile(imageStream, Width, Height, 0, Stretch,
                                                                      IsAutoOutputFormat ? GetImageFormat(GetMimeType(contentPath)) : this.ResizeOutputFormat,
                                                                      this.ResizeSmoothingMode,
                                                                      this.ResizeInterpolationMode,
                                                                      this.ResizePixelOffsetMode);
                }
                else
                {
                    double verticalDiff;
                    double horizontalDiff;

                    switch (CropVAlign.ToLower())
                    {
                    case "top":
                        verticalDiff = 0;
                        break;

                    case "center":
                        verticalDiff = -1;
                        break;

                    case "bottom":
                        verticalDiff = double.MaxValue;
                        break;

                    default:
                        try
                        {
                            verticalDiff = Convert.ToDouble(CropVAlign);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                            verticalDiff = 0;
                        }
                        break;
                    }

                    switch (CropHAlign.ToLower())
                    {
                    case "left":
                        horizontalDiff = 0;
                        break;

                    case "center":
                        horizontalDiff = -1;
                        break;

                    case "right":
                        horizontalDiff = double.MaxValue;
                        break;

                    default:
                        try
                        {
                            horizontalDiff = Convert.ToDouble(CropHAlign);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                            horizontalDiff = 0;
                        }
                        break;
                    }
                    //Crop image
                    imageStream = ImageResizer.CreateCropedImageFile(imageStream, Width, Height, 0,
                                                                     IsAutoOutputFormat ? GetImageFormat(GetMimeType(contentPath)) : this.ResizeOutputFormat,
                                                                     this.ResizeSmoothingMode,
                                                                     this.ResizeInterpolationMode,
                                                                     this.ResizePixelOffsetMode, verticalDiff, horizontalDiff);
                }

                Cache(imageStream, GetImageCachePath(contentPath));
                FlushStream(imageStream, context, GetMimeType(contentPath));
                return;
            }
            else
            {
                throw new Exception("There was no image in the requested file path.");
            }
        }