private async Task <IActionResult> ReturnFile(string path, int h, int w, string realfileName, bool download, string suggestefFileName)
 {
     try
     {
         if (realfileName.IsStaticImage() && h > 0 && w > 0)
         {
             if (download)
             {
                 return(await this.AiurFile(await _imageCompressor.Compress(path, realfileName, w, h), realfileName, suggestefFileName));
             }
             else
             {
                 return(await this.AiurFile(await _imageCompressor.Compress(path, realfileName, w, h), realfileName));
             }
         }
         else
         {
             if (download)
             {
                 return(await this.AiurFile(path, realfileName, suggestefFileName));
             }
             else
             {
                 return(await this.AiurFile(path, realfileName));
             }
         }
     }
     catch (Exception e) when(e is DirectoryNotFoundException || e is FileNotFoundException)
     {
         return(NotFound());
     }
 }
示例#2
0
        public ActionResult SetAvatar(HttpPostedFileBase file)
        {
            ViewBag.Localize = getLocale();
            if (file != null)
            {
                using (var transaction = _entities.Database.BeginTransaction())
                {
                    try
                    {
                        var             avatarFile = "";
                        ApplicationUser user       = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(User.Identity.GetUserId <int>());
                        if (user != null)
                        {
                            var email  = user.Email;
                            var person = _entities.People.Where(w => w.Email == email).FirstOrDefault();
                            if (person.AvatarFile != null)
                            {
                                try
                                {
                                    System.IO.File.Delete(Server.MapPath("/avatars/") + avatarFile + "_128");
                                    System.IO.File.Delete(Server.MapPath("/avatars/") + avatarFile + "_50");
                                }
                                catch { }
                            }
                            person.AvatarFile = _stringGenerator.Next();
                            avatarFile        = person.AvatarFile;
                            _entities.SaveChanges();
                        }
                        var bitmap = new Bitmap(file.InputStream);

                        var bitmap128 = _imageCompressor.Compress(bitmap, new Size(128, 128));
                        var path      = Server.MapPath("/avatars/") + avatarFile + "_128.png";
                        bitmap128.Save(path, ImageFormat.Png);
                        var bitmap25 = _imageCompressor.Compress(bitmap, new Size(50, 50));
                        path = Server.MapPath("/avatars/") + avatarFile + "_50.png";
                        bitmap25.Save(path, ImageFormat.Png);
                        ViewBag.Avatar_128      = getUserAvatar(avatarFile + "_128");
                        ViewBag.PeopleForMaster = _entities.People.Select(s => s).ToList();
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        return(RedirectToAction("Message", "Home", new { lang = getLocale() }));
                    }
                }
            }
            ViewBag.Email = EMAIL;
            AVATAR        = ViewBag.Avatar_128;
            return(RedirectToAction("UserProfile"));
        }
示例#3
0
        public void CompressTestCase()
        {
            // Test On Linux -------------------------

            var input2Path = "/Users/top/Downloads/Test File 2.png";
            var input3Path = "/Users/top/Downloads/Test File 3.JPG";
            var input4Path = "/Users/top/Downloads/Test File 4.JPG";
            var input5Path = "/Users/top/Downloads/Test File 5.gif";

            ImageCompressor.Compress(input3Path, "/Users/top/Downloads/Test File 3 Compressed.JPG");
            // ImageCompressor.Compress(input4Path, "/Users/top/Downloads/Test File 4 Compressed.JPG");
            ImageCompressor.Compress(input2Path, "/Users/top/Downloads/Test File 2 Compressed.png");
            ImageCompressor.Compress(input5Path, "/Users/top/Downloads/Test File 5 Compressed.gif");

            // Test On Windows -------------------------

            // var input2Path = "D:\\Test File 2.png";
            // var input3Path = "D:\\Test File 3.JPG";
            // var input4Path = "D:\\Test File 4.JPG";
            // var input5Path = "D:\\Test File 5.gif";
            //
            // ImageCompressor.Compress(input2Path, "D:\\Test File 2 Compressed.png");
            // ImageCompressor.Compress(input3Path, "D:\\Test File 3 Compressed.JPG");
            // ImageCompressor.Compress(input5Path, "D:\\Test File 5 Compressed.gif");
        }
        public static byte[] ResizeAndCompressImage(byte[] imageFileBytes,
                                                    bool isResize,
                                                    int maxWidthPx,
                                                    int maxHeightPx,
                                                    bool isCompress)
        {
            var newImageFileBytes = imageFileBytes;

            if (isResize)
            {
                newImageFileBytes = ImageResizeHelper.Resize(imageFileBytes, maxWidthPx, maxHeightPx);
            }

            if (!isCompress)
            {
                return(newImageFileBytes);
            }

            using var streamToCompress = new MemoryStream(newImageFileBytes);

            var compressResult = ImageCompressor.Compress(streamToCompress);

            newImageFileBytes = compressResult.ResultFileStream.ToArray();

            return(newImageFileBytes);
        }
示例#5
0
        public void CompressorUnitTest()
        {
            var imageCompressJpegResult = ImageCompressor.Compress("<file name>.jpg", "<outlet file name>.jpg");

            var imageCompressGifResult = ImageCompressor.Compress("<file name>.gif", "<outlet file name>.gif");

            var imageCompressPngResult = ImageCompressor.Compress("<file name>.png", "<outlet file name>.png");
        }
示例#6
0
 private async Task <IActionResult> FileWithImageCompressor(string path, string extension)
 {
     int.TryParse(Request.Query["w"], out int width);
     bool.TryParse(Request.Query["square"], out bool square);
     if (width > 0)
     {
         if (square)
         {
             return(this.WebFile(await _imageCompressor.Compress(path, width, width), extension));
         }
         else
         {
             return(this.WebFile(await _imageCompressor.Compress(path, width, 0), extension));
         }
     }
     else
     {
         return(this.WebFile(await _imageCompressor.ClearExif(path), extension));
     }
 }
示例#7
0
 private async Task <IActionResult> FileWithImageCompressor(string path, string extension)
 {
     int.TryParse(Request.Query["w"], out int w);
     int.TryParse(Request.Query["h"], out int h);
     if (h >= 0 && w >= 0 && h + w > 0)
     {
         return(this.WebFile(await _imageCompressor.Compress(path, w, h), extension));
     }
     else
     {
         return(this.WebFile(await _imageCompressor.ClearExif(path), extension));
     }
 }
示例#8
0
        public ActionResult Index(HttpPostedFileBase file)
        {
            if (file != null)
            {
                using (var transaction = _entities.Database.BeginTransaction())
                {
                    try
                    {
                        var sharedCode = _stringGenerator.Next();
                        var fileName   = Path.GetFileNameWithoutExtension(file.FileName);
                        var image      = new Image();
                        //if (_entities.Image.ToList().Count > 0)
                        //    image.id = _entities.Image.Max(s => s.id) + 1;
                        //else image.id = 1;
                        image.isPublic        = false;
                        image.folderId        = _entities.Folder.Where(f => f.name.Equals("General")).Select(fol => fol.id).FirstOrDefault();
                        image.sharedCode      = sharedCode;
                        image.name            = fileName;
                        image.publicationDate = DateTime.Now;
                        _entities.Image.Add(image);
                        _entities.SaveChanges();

                        transaction.Commit();

                        var bitmap = new Bitmap(file.InputStream);

                        var path = Path.Combine(Server.MapPath("~/img/"), sharedCode + ".png");
                        bitmap.Save(path, ImageFormat.Png);

                        var compressedBitmap = _imageCompressor.Compress(bitmap, new Size(128, 128));
                        path = Path.Combine(Server.MapPath("~/img/"), sharedCode + "_compressed.png");
                        compressedBitmap.Save(path, ImageFormat.Png);
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                    }
                }
            }
            return(View());
        }