Ejemplo n.º 1
0
        public async Task <IActionResult> UploadUserPhoto(IFormFile file)
        {
            if ((file == null) || (!imageWriter.IsImageFile(file)))
            {
                return(BadRequest());
            }

            //Create unique UUID file name
            string fileName       = Path.GetFileName(file.FileName);
            string extension      = Path.GetExtension(fileName);
            string uniqueFileName = Guid.NewGuid().ToString() + extension;
            //Image url on the server
            string imageUrl = "\\images\\users\\" + uniqueFileName;

            //Absolute path to the image on the server
            string uploads = Path.Combine(_appHostingEnv.WebRootPath, "images", "users");
            string path    = Path.Combine(uploads, uniqueFileName);

            //Upload image on the server
            await imageWriter.UploadImageAsync(file, path);

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            foreach (var prop in image.PropertyItems)
            {
                if (prop.Id == 0x0112)
                {
                    int orientationValue = image.GetPropertyItem(prop.Id).Value[0];
                    if (orientationValue != 1)
                    {
                        imageWriter.RotateImage(image, orientationValue);
                        imageWriter.DeleteImage(path);
                        image.Save(path, ImageFormat.Jpeg);
                        break;
                    }
                    break;
                }
            }

            return(Ok(imageUrl));
        }