Exemple #1
0
        void OnImageChanged()
        {
            if (Image != null)
            {
                ImageUrl   = Image.Url;
                SourceType = Image.SourceType;
                Data       = Image.LoadImage();
                Text       = Image.Name;
                EmbedIn    = Image.EmbedIn;

                if (SizeType == PictureSizeType.Thumb && ThumbImage == null)
                {
                    CreateThumbImage();
                }
            }
            else
            {
                ImageUrl   = null;
                SourceType = PictureSource.File;
                Data       = null;

                if (ThumbImage != null)
                {
                    ThumbImage.Dispose();
                    ThumbImage = null;
                }
            }
        }
Exemple #2
0
        // GET: Image
        public ActionResult Index(int count = 10, int page = 0)
        {
            // If page is lower than 1, reset
            if (page < 0)
            {
                page = -1;
            }

            int total = _db.SourceImages.Count();

            // If page is higher than total pages count
            if (page > total / count)
            {
                page = -1;
            }

            if (page == -1)
            {
                // return empty list which shows warning
                return(View(new ListSourceImageViewModel {
                    Page = 0, ImageItems = new List <ThumbImage>()
                }));
            }

            // Load 30 SourceImages according to current page
            List <SourceImage> sourceImages = _db.SourceImages
                                              .Include(i => i.Thumbnails)
                                              .OrderBy(i => i.ID)
                                              .Skip(page * count)
                                              .Take(count)
                                              .ToList();

            // Save References to thumbnails
            List <ThumbImage> thumbnails = new List <ThumbImage>();

            foreach (var sourceImage in sourceImages)
            {
                ThumbImage thumbnail = sourceImage.GetImage(Format.JPEG, 200, sourceImage.Thumbnails, q: 75);

                SaveThumb(sourceImage, thumbnail, sourceImage.Thumbnails);
                _db.SaveChanges();

                // Add it to colletion of thumbnails
                thumbnails.Add(thumbnail);
            }

            // Set Data to viewmodel
            var vm = new ListSourceImageViewModel
            {
                Page       = page,
                ImageItems = thumbnails
            };

            return(View(vm));
        }
 public object ToJson()
 {
     return(new
     {
         Title = Name,
         Url = Url.ToLower(),
         Path = Path.ToLower(),
         Image = ThumbImage.ToLower(),
         Type = Convert.ToInt16(FileType),
         Checked = false
     });
 }
Exemple #4
0
        private void SaveThumb(SourceImage sourceImage, ThumbImage thumbnail, List <ThumbImage> collection)
        {
            if (!_db.ThumbImages.AsEnumerable().Contains(thumbnail))
            {
                // Save new thumbnail to db
                _db.ThumbImages.Add(thumbnail);

                _db.MarkAsModified(sourceImage);
            }

            if (!collection.AsEnumerable().Contains(thumbnail))
            {
                collection.Add(thumbnail);
                _db.MarkAsModified(sourceImage);
            }
        }
        internal void ClearImage()
        {
            if (OriginalImage != null)
            {
                OriginalImage.Dispose();
                OriginalImage = null;
            }

            if (ThumbImage != null)
            {
                ThumbImage.Dispose();
                ThumbImage = null;
            }

            // this.gridControl1.DataSource = null;
        }
        internal void ShowOriginalImage()
        {
            this.picEditOriginal.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            if (OriginalImage != null)
            {
                OriginalImage.Dispose();
            }

            if (ThumbImage != null)
            {
                ThumbImage.Dispose();
            }

            //OriginalImage = m_Record.OriginalPic == null ? null : m_Record.OriginalPic.Clone() as Image;
            //ThumbImage = m_Record.ObjectPic == null ? null : m_Record.ObjectPic.Clone() as Image;

            AutoFit();
        }
Exemple #7
0
        // GET: Image/FormatTest/5?format=1?strip=true&quality=90
        public ActionResult FormatTest(int?id, Format format, bool strip = false, int quality = 100)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SourceImage sourceImage = _db.SourceImages.Find(id);

            if (sourceImage == null)
            {
                return(HttpNotFound());
            }

            // Convert the Image
            ThumbImage image = sourceImage.GetImage(format, sourceImage.Width, sourceImage.Thumbnails, q: quality, strip: strip);

            SaveThumb(sourceImage, image, sourceImage.Thumbnails);
            _db.SaveChanges();

            // Return the file back
            return(File(image.RelativePath, "image/" + image.getFormat().ToLower()));
        }
Exemple #8
0
 public override void CopyTo(Widget widget)
 {
     base.CopyTo(widget);
     if (widget is PictureWidget)
     {
         var pic = (PictureWidget)widget;
         pic.Data         = Data;
         pic.CustomWidth  = CustomWidth;
         pic.CustomHeight = CustomHeight;
         pic.SourceType   = SourceType;
         pic.SizeType     = SizeType;
         if (Image != null)
         {
             pic.Image = Image.Clone();
         }
         else if (ThumbImage != null)
         {
             pic.ThumbImage = ThumbImage.Clone() as Image;
         }
         pic.ImageUrl     = ImageUrl;
         pic.OriginalSize = OriginalSize;
         pic.EmbedIn      = EmbedIn;
     }
 }
Exemple #9
0
        // GET: Image/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SourceImage sourceImage = _db.SourceImages.Find(id);

            if (sourceImage == null)
            {
                return(HttpNotFound());
            }

            // Update Data on Detail view, which cant be inserted into db at seed time
            Image vipsImage = Image.NewFromFile(sourceImage.AbsolutePath);

            sourceImage.Width  = vipsImage.Width;
            sourceImage.Height = vipsImage.Height;
            vipsImage.Dispose();

            List <ThumbImage> thumbs = new List <ThumbImage>();

            // Generate set of 8 thumbnails
            foreach (var size in _sizes)
            {
                ThumbImage thumbnail = sourceImage.GetImage(Format.JPEG, size, sourceImage.Thumbnails);
                //ThumbImage thumbnailx = sourceImage.GetImage(Format.WebP, size, sourceImage.Thumbnails);

                SaveThumb(sourceImage, thumbnail, sourceImage.Thumbnails);
                //SaveThumb(sourceImage, thumbnailx, sourceImage.Thumbnails);

                thumbs.Add(thumbnail);
            }


            Format[] formats = { Format.GIF, Format.JPEG, Format.PNG, Format.WebP };

            // Generate format images
            List <Format[]> formatTransform = new List <Format[]> {
                new[] { Format.GIF, Format.JPEG },
                new[] { Format.GIF, Format.WebP },
                new[] { Format.PNG, Format.JPEG },
                new[] { Format.PNG, Format.WebP },
                new[] { Format.JPEG, Format.WebP },
                new[] { Format.WebP, Format.JPEG }
            };

            List <CompareImage> formatThumb       = new List <CompareImage>();
            List <CompareImage> formatChangeThumb = new List <CompareImage>();

            foreach (Format[] format in formatTransform)
            {
                ThumbImage formatImage  = sourceImage.GetImage(format[0], sourceImage.Width, sourceImage.Formats, sourceImage.Height);
                ThumbImage formatImagex = sourceImage.GetImage(format[1], sourceImage.Width, sourceImage.Formats, sourceImage.Height);

                if (formatImage.Format == Format.SVG)
                {
                    continue;
                }

                // Check if compare Image already exists
                CompareImage compare = null;
                if (_db.CompareImages.Any())
                {
                    compare = _db.CompareImages
                              .FirstOrDefault(i => i.Image1.AbsolutePath == formatImage.AbsolutePath &&
                                              i.Image2.AbsolutePath == formatImagex.AbsolutePath
                                              );
                }

                // If not, create one
                if (compare == null)
                {
                    compare = new CompareImage
                    {
                        Image1 = formatImage,
                        Image2 = formatImagex,
                        SSIM   = ImageService.GetSSIM(formatImage.AbsolutePath, formatImagex.AbsolutePath)
                    };

                    _db.CompareImages.Add(compare);
                    _db.SaveChanges();
                }

                SaveThumb(sourceImage, formatImage, sourceImage.Formats);
                SaveThumb(sourceImage, formatImagex, sourceImage.Formats);

                formatThumb.Add(compare);
            }

            // Generate compression images
            foreach (Format format in formats)
            {
                ThumbImage compressImage  = sourceImage.GetImage(format, sourceImage.Width, sourceImage.Compression, sourceImage.Height);
                ThumbImage compressImagex = sourceImage.GetImage(format, sourceImage.Width, sourceImage.Compression, sourceImage.Height, 75);

                if (compressImage.Format == Format.SVG)
                {
                    continue;
                }

                // Check if compare Image already exists
                CompareImage compare = null;
                if (_db.CompareImages.Any())
                {
                    compare = _db.CompareImages
                              .FirstOrDefault(i => i.Image1.AbsolutePath == compressImage.AbsolutePath && i.Image2.AbsolutePath == compressImagex.AbsolutePath);
                }

                // If not, create one
                if (compare == null)
                {
                    compare = new CompareImage
                    {
                        Image1 = compressImage,
                        Image2 = compressImagex,
                        SSIM   = ImageService.GetSSIM(compressImage.AbsolutePath, compressImagex.AbsolutePath)
                    };

                    _db.CompareImages.Add(compare);
                    _db.SaveChanges();
                }

                SaveThumb(sourceImage, compressImage, sourceImage.Compression);
                SaveThumb(sourceImage, compressImagex, sourceImage.Compression);

                formatChangeThumb.Add(compare);
            }

            // Generate stripped images (remove metadata)
            foreach (Format format in formats)
            {
                ThumbImage strippedImage  = sourceImage.GetImage(format, sourceImage.Width, sourceImage.Metadata, sourceImage.Height);
                ThumbImage strippedImagex = sourceImage.GetImage(format, sourceImage.Width, sourceImage.Metadata, sourceImage.Height, strip: true);

                SaveThumb(sourceImage, strippedImage, sourceImage.Metadata);
                SaveThumb(sourceImage, strippedImagex, sourceImage.Metadata);
            }

            // Save Changes if any
            if (_db.IsModified(sourceImage))
            {
                _db.SaveChanges();
            }


            // Create ViewModel
            SourceImageViewModel sourceImageViewModel = ImageService.GetSourceImageViewModel(sourceImage, thumbs);

            sourceImageViewModel.Formats     = formatThumb;
            sourceImageViewModel.Compression = formatChangeThumb;

            return(View("Details", sourceImageViewModel));
        }
Exemple #10
0
        protected override void Seed(ImageContext context)
        {
            String absoluteDir = MapPath("images");

            string[] fileEntries = FileService.GetAllFilesInDir(absoluteDir);

            SourceImage[] imageEntities = new SourceImage[fileEntries.Length];
            ThumbImage[]  thumbEntities = new ThumbImage[fileEntries.Length];

            int i = 0;

            foreach (string path in fileEntries)
            {
                var image = new SourceImage
                {
                    ID           = i + 1,
                    AbsolutePath = path,
                    FileName     = Path.GetFileName(path),
                    RelativePath = "/images/" + Path.GetFileName(path),
                    AltText      = Path.GetFileNameWithoutExtension(path),
                    Thumbnails   = new List <ThumbImage>(),
                    Formats      = new List <ThumbImage>(),
                    Metadata     = new List <ThumbImage>(),
                    Compression  = new List <ThumbImage>(),
                    Format       = FileService.ParseFileFormat(Path.GetExtension(path)),
                    FileSize     = new FileInfo(path).Length
                };

                if (image.Format == Format.SVG)
                {
                    var minPath = absoluteDir + "/out/" + Path.GetFileNameWithoutExtension(path) + ".min.svg";

                    var optimized = new ThumbImage
                    {
                        FileName     = Path.GetFileName(minPath),
                        AltText      = image.AltText,
                        SourceID     = image.ID,
                        RelativePath = "/images/out/" + Path.GetFileName(minPath),
                        AbsolutePath = minPath,
                        Format       = Format.SVG,
                        FileSize     = new FileInfo(minPath).Length,
                        Stripped     = false
                    };

                    image.Thumbnails.Add(optimized);
                    thumbEntities.SetValue(optimized, i);
                }

                imageEntities.SetValue(image, i);
                i++;
            }

            // Wrap everything in transaction
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    context.SourceImages.AddOrUpdate(imageEntities);
                    context.ThumbImages.AddOrUpdate(thumbEntities);
                    context.SaveChanges();

                    transaction.Commit();

                    Console.WriteLine("Seed succesfull.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message, e.InnerException);
                    transaction.Rollback();
                }
            }

            // Create Directory for thumbnails
            Directory.CreateDirectory(MapPath("thumbnails"));
        }
        /// <summary>
        /// Generates thumbnail using vips of the source image, always downscales (no upscale), saves the generated
        /// file to thumbnails folder and returns new ThumbImage Model
        /// </summary>
        /// <param name="src">SourceImage</param>
        /// <param name="width">Width of the image</param>
        /// <param name="height">Optional Height of the image</param>
        /// <returns>Empty ThumbImage if no file exists or Created ThumbImage</returns>
        internal static ThumbImage CreateImage(SourceImage src, int width, int?height = null, int q = 100, Format format = Format.JPEG, bool strip = false)
        {
            // Checks, if the source file exists
            if (src == null && File.Exists(src.AbsolutePath))
            {
                return(new ThumbImage());
            }

            // No uknown formats shall pass!
            if (src.Format == Format.Unknown || format == Format.Unknown)
            {
                return(new ThumbImage());
            }

            // Load Image to Vips
            Image image = Image.Thumbnail(src.AbsolutePath, width, height, "down", true);
            //image = image.Copy();
            int h = image.Height;
            int w = image.Width;

            // Create FileName and Path
            String fileName = CreateThumbFilename(src, w, h, q, format.ToString().ToLower());
            String filePath = FileService.CombineDirectoryAndFilename(GetThumbnailPath(), fileName);

            switch (format)
            {
            case Format.JPEG:
                image.Jpegsave(filePath, null, q, null, true, true, false, true, null, true, null, strip, new[] { 255.0, 255.0, 255.0 });
                break;

            case Format.GIF:
                using (MagickImage imImage = new MagickImage(src.AbsolutePath))
                {
                    imImage.Resize(w, h);
                    imImage.Quality = q;

                    if (strip)
                    {
                        imImage.Strip();
                    }
                    imImage.Write(filePath);
                }
                break;

            case Format.PNG:
                // Compression ratio is inverse of quality between 0 - 9
                int pngQ = (q / 10 - 10) * -1;

                image.Pngsave(filePath, pngQ, false, strip: strip);
                break;

            case Format.WebP:
                if (q == 100)
                {
                    image.Webpsave(filePath, null, q, true, nearLossless: true, strip: strip);
                }
                else
                {
                    image.Webpsave(filePath, null, q, false, smartSubsample: true, strip: strip);
                }
                break;

            case Format.TIFF:
                image.Tiffsave(filePath, strip: strip);
                break;

            case Format.SVG:
                return(src.GetOptimizedSVG());
            }

            // Create new ThumbImage Model
            var thumb = new ThumbImage
            {
                AbsolutePath = filePath,
                AltText      = src.AltText,
                FileName     = fileName,
                RelativePath = $"/thumbnails/{fileName}",
                Height       = h,
                Width        = w,
                SourceID     = src.ID,
                Format       = format,
                FileSize     = new FileInfo(filePath).Length,
                Quality      = q,
                Stripped     = strip
            };

            return(thumb);
        }