Пример #1
0
        public void Update(AuthorDto item, IFormFile file)
        {
            var oldImageName = Get(item.Id).ImagePath;

            if (file != null && file.Length > 0)
            {
                var cropped = ImageTool.CropMaxSquare(Image.FromStream(file.OpenReadStream()));
                var resized = ImageTool.Resize(cropped, 500, 500);

                var uploads = Path.Combine(_configuration.WebRootPath, "pics\\Authors");
                if (!string.IsNullOrEmpty(oldImageName))
                {
                    var oldPath = Path.Combine(uploads, oldImageName);
                    if (File.Exists(oldPath))
                    {
                        File.Delete(oldPath);
                    }
                }

                var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                var filePath = Path.Combine(uploads, fileName);
                resized.Save(filePath);
                item.ImagePath = fileName;
            }
            else
            {
                item.ImagePath = oldImageName;
            }

            var author = _mapper.Map <AuthorDto, Author>(item);

            _repository.Update(author);
            _repository.Save();
        }
Пример #2
0
        public void CropMaxSquareTest()
        {
            // Act
            var img1 = ImageTool.CropMaxSquare(_wideImage);
            var img2 = ImageTool.CropMaxSquare(_hightImage);
            var img3 = ImageTool.CropMaxSquare(_squareImage);

            // Assert
            Assert.Equal(500, img1.Width);
            Assert.Equal(500, img1.Height);

            Assert.Equal(500, img2.Width);
            Assert.Equal(500, img2.Height);

            Assert.Equal(500, img3.Width);
            Assert.Equal(500, img3.Height);
        }
Пример #3
0
        public void Create(AuthorDto item, IFormFile file)
        {
            if (file != null && file.Length > 0)
            {
                var cropped  = ImageTool.CropMaxSquare(Image.FromStream(file.OpenReadStream()));
                var resized  = ImageTool.Resize(cropped, 500, 500);
                var uploads  = Path.Combine(_configuration.WebRootPath, "pics\\Authors");
                var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                var filePath = Path.Combine(uploads, fileName);
                item.ImagePath = fileName;
                resized.Save(filePath);
            }

            var author = _mapper.Map <AuthorDto, Author>(item);

            _repository.Create(author);
            _repository.Save();
        }