Exemplo n.º 1
0
        public async Task <IReadOnlyDictionary <Guid, MediaThumbnail> > GetThumbnailsByMediaIdsAsync(
            IEnumerable <Guid> mediaIds,
            ThumbnailSizeName size,
            CancellationToken cancellationToken)
        {
            FilterDefinition <Media> filter = Builders <Media> .Filter
                                              .In(x => x.Id, mediaIds.ToList());

            filter = filter & Builders <Media> .Filter.ElemMatch(x =>
                                                                 x.Thumbnails,
                                                                 Builders <MediaThumbnail> .Filter.Eq(t => t.Size, size));

            List <Media> medias = await _mediaStoreContext.Medias.Find(filter)
                                  .ToListAsync(cancellationToken);

            var result = new Dictionary <Guid, MediaThumbnail>();

            foreach (Media media in medias)
            {
                MediaThumbnail?thumb = media !.Thumbnails !.Where(x =>
                                                                  x.Size == size &&
                                                                  x.Format == "webp")
                                       .FirstOrDefault();

                if (thumb != null)
                {
                    thumb.Data = await Thumbnails.GetAsync(thumb.Id, cancellationToken);

                    result.Add(media.Id, thumb);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public async Task <MediaThumbnail?> GetThumbnailAsync(
            Album album,
            ThumbnailSizeName size,
            CancellationToken cancellationToken)
        {
            Guid?mediaId;

            if (album.CoverMediaId.HasValue)
            {
                mediaId = album.CoverMediaId.Value;
            }
            else
            {
                mediaId = (await GetMediaIdsAsync(album, cancellationToken))
                          .FirstOrDefault();
            }

            if (mediaId.HasValue)
            {
                IReadOnlyDictionary <Guid, MediaThumbnail>?thumbs = await _mediaStore
                                                                    .GetThumbnailsByMediaIdsAsync(
                    new[] { mediaId.Value },
                    size,
                    cancellationToken);

                return(thumbs.Values.FirstOrDefault());
            }

            return(null);
        }
Exemplo n.º 3
0
 public async Task <MediaThumbnail?> GetThumbnailAsync(
     Album album,
     ThumbnailSizeName size,
     CancellationToken cancellationToken)
 {
     return(await _albumService.GetThumbnailAsync(album, size, cancellationToken));
 }
Exemplo n.º 4
0
        private async Task <BoxExtractionResult> ExtractBoxAsync(
            Image image,
            BoxExtractionInput input,
            ThumbnailSizeName thumbnailSize,
            CancellationToken cancellationToken)
        {
            var face   = new BoxExtractionResult();
            int width  = input.Box.Right - input.Box.Left;
            int height = input.Box.Bottom - input.Box.Top;

            double multi    = 1.95;
            var    widthAdd = (int)((width * multi - width) / 2);
            //Make height and width same
            var heighAdd = Math.Abs(widthAdd + width - height);

            var faceBox = new ImageBox();

            faceBox.Left = input.Box.Left - widthAdd;
            if (faceBox.Left < 0)
            {
                faceBox.Left = 0;
            }

            faceBox.Right = input.Box.Right + widthAdd;
            if (faceBox.Right > image.Width)
            {
                faceBox.Right = image.Width;
            }

            faceBox.Top = input.Box.Top - heighAdd;
            if (faceBox.Top < 0)
            {
                faceBox.Top = 0;
            }

            faceBox.Bottom = input.Box.Bottom + heighAdd;
            if (faceBox.Bottom > image.Height)
            {
                faceBox.Bottom = image.Height;
            }

            var rect = new Rectangle(
                faceBox.Left,
                faceBox.Top,
                faceBox.Right - faceBox.Left,
                faceBox.Bottom - faceBox.Top);

            Image croped = image.Clone(x => x.Crop(rect));

            ThumbnailResult faceThumb = await _thumbnailService.GenerateThumbnailAsync(
                croped,
                thumbnailSize,
                cancellationToken);

            return(new BoxExtractionResult
            {
                Id = input.Id,
                Thumbnail = faceThumb
            });
        }
Exemplo n.º 5
0
        public async Task <IEnumerable <BoxExtractionResult> > ExtractBoxesAsync(
            Image image,
            IEnumerable <BoxExtractionInput> inputs,
            ThumbnailSizeName thumbnailSize,
            CancellationToken cancellationToken)
        {
            var faces = new List <BoxExtractionResult>();

            foreach (BoxExtractionInput input in inputs)
            {
                try
                {
                    BoxExtractionResult face = await ExtractBoxAsync(
                        image,
                        input,
                        thumbnailSize,
                        cancellationToken);

                    faces.Add(face);
                }
                catch (Exception ex)
                {
                }
            }

            return(faces);
        }
Exemplo n.º 6
0
        public async Task <ThumbnailResult> GenerateThumbnailAsync(
            Stream stream,
            ThumbnailSizeName size,
            CancellationToken cancellationToken)
        {
            Image image = await Image.LoadAsync(stream);

            return(await GenerateThumbnailAsync(image, size, cancellationToken));
        }
Exemplo n.º 7
0
        public MediaThumbnail?GetThumbnail(Media media, ThumbnailSizeName size)
        {
            IEnumerable <MediaThumbnail>?thumbs = media.Thumbnails
                                                  .Where(x => x.Size == size);

            MediaThumbnail?thumb = thumbs.Where(x => x.Format == "webp").FirstOrDefault() ??
                                   thumbs.FirstOrDefault();

            return(thumb);
        }
Exemplo n.º 8
0
        public async Task <IEnumerable <BoxExtractionResult> > ExtractBoxesAsync(
            Stream stream,
            IEnumerable <BoxExtractionInput> inputs,
            ThumbnailSizeName thumbnailSize,
            CancellationToken cancellationToken)
        {
            Image image = await Image.LoadAsync(stream);

            return(await ExtractBoxesAsync(image, inputs, thumbnailSize, cancellationToken));
        }
Exemplo n.º 9
0
 public MediaThumbnail(
     System.Guid id,
     ThumbnailSizeName size,
     string dataUrl,
     IMediaDimension1 dimensions)
 {
     Id         = id;
     Size       = size;
     DataUrl    = dataUrl;
     Dimensions = dimensions;
 }
Exemplo n.º 10
0
        public async Task <MediaThumbnail?> GetThumbnailAsync(Guid mediaId, ThumbnailSizeName size, CancellationToken cancellationToken)
        {
            Media media = await _mediaStore.GetByIdAsync(mediaId, cancellationToken);

            MediaThumbnail?thumb = GetThumbnail(media, size);

            if (thumb != null)
            {
                thumb.Data = await _mediaStore.Thumbnails.GetAsync(thumb.Id, cancellationToken);
            }

            return(thumb);
        }
Exemplo n.º 11
0
        public async Task <IActionResult> ThumbnailByMediaAsync(
            Guid id,
            ThumbnailSizeName size = ThumbnailSizeName.M,
            CancellationToken cancellationToken = default)
        {
            MediaThumbnail?thumb = await _mediaService.GetThumbnailAsync(id, size, cancellationToken);

            if (thumb == null)
            {
                return(NotFound());
            }

            return(new FileContentResult(thumb?.Data, "image/jpg"));
        }
Exemplo n.º 12
0
        public async Task <MediaThumbnail?> GetThumbnailAsync(
            Media media,
            ThumbnailDataDataLoader thumbnailLoader,
            ThumbnailSizeName size,
            CancellationToken cancellationToken)
        {
            MediaThumbnail?thumb = _mediaService.GetThumbnail(media, size);

            if (thumb != null)
            {
                return(await thumbnailLoader.LoadAsync(thumb, cancellationToken));
            }

            return(null);
        }
Exemplo n.º 13
0
        public async Task <ThumbnailResult> GenerateThumbnailAsync(
            Image image,
            ThumbnailSizeName size,
            CancellationToken cancellationToken)
        {
            ThumbnailSizeDefinition?def = _sizeDefinitions.Single(x => x.Name == size);

            if (def.IsSquare)
            {
                image = CropSqare(image);
            }

            var width    = image.Width;
            var newWidth = def.Width;

            if (newWidth > width)
            {
                newWidth = width;
            }
            var ratio  = image.Width / (double)newWidth;
            var height = image.Height / ratio;

            Image        resized = image.Clone(ctx => ctx.Resize(newWidth, (int)height));
            MemoryStream thumb   = new MemoryStream();
            await resized.SaveAsync(thumb, new JpegEncoder(), cancellationToken);

            thumb.Position = 0;

            return(new ThumbnailResult
            {
                Size = size,
                Data = thumb.ToArray(),
                Format = "jpg",
                Dimensions = new MediaDimension()
                {
                    Height = resized.Height,
                    Width = resized.Width
                }
            });
        }