Exemplo n.º 1
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()));
        }
Exemplo n.º 2
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));
        }