Ejemplo n.º 1
0
        public async Task <PhotoViewModel> Handle(EditUserPhotoUploadCommand request, CancellationToken cancellationToken)
        {
            ImageUploadOutput uploadOutput = _imageService.UploadUserPhotoImageAsync(request.UserId, request.File);

            var photo = _mapper.Map <Photo>(uploadOutput);

            User user = await _context.Users
                        .Include(t => t.Photos)
                        .SingleOrDefaultAsync(u => u.Id == request.UserId);

            if (!user.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            user.Photos.Add(photo);

            await _context.SaveChangesAsync();

            return(_mapper.Map <PhotoViewModel>(photo));
        }
Ejemplo n.º 2
0
        public ImageUploadOutput UploadImage()
        {
            ImageUploadOutput output = new ImageUploadOutput();

            var file = Request.Form.Files;

            foreach (var item in file)
            {
                if (item != null && item.Length > 0)
                {
                    string   id       = Guid.NewGuid().ToString();
                    string[] ext      = item.FileName.Split('.');
                    string   part1    = "wwwroot";
                    string   part2    = "recognizer\\" + id + "." + ext[ext.Length - 1];
                    string   fileName = Path.Combine(part1, part2);
                    string   url      = _hostingEnvironment.ContentRootPath;
                    string   path     = Path.Combine(url, fileName);

                    ISupportedImageFormat format = new PngFormat {
                        Quality = 70, IsIndexed = false
                    };
                    System.Drawing.Image img = System.Drawing.Image.FromStream(item.OpenReadStream(), true, true);
                    ResizeLayer          rl  = new ResizeLayer(new System.Drawing.Size(512, 1024), ResizeMode.Crop);
                    using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                    {
                        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                        {
                            imageFactory.Load(img)
                            .Resize(rl)
                            .Format(format)
                            .Save(fs);
                        }
                        output.Url = "recognizer/" + id + "." + ext[ext.Length - 1];
                    }
                }
            }
            output.Result = "OK";

            return(output);
        }
Ejemplo n.º 3
0
        public ImageUploadOutput UploadFromStream(IFormFile file)
        {
            ImageUploadResult uploadResult = null;
            var imgUploadOutput            = new ImageUploadOutput();

            using (var stream = file.OpenReadStream())
            {
                var uploadParams = new ImageUploadParams()
                {
                    File           = new FileDescription(file.Name, stream),
                    Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                };

                uploadResult = _cloudinary.Upload(uploadParams);

                if (uploadResult != null)
                {
                    imgUploadOutput.Url      = uploadResult.Uri.ToString();
                    imgUploadOutput.PublicId = uploadResult.PublicId;
                }
            }

            return(imgUploadOutput);
        }