GetPathToResizedImage() public method

public GetPathToResizedImage ( string originalPath, bool getThumbnail = false ) : string
originalPath string
getThumbnail bool
return string
 public void GetTinyImage_DoesNotChangeSize()
 {
     using (var cache = new RuntimeImageProcessor(new BookRenamedEvent()) { TargetDimension = 100 })
     using (var file = MakeTempPNGImage(10,10))
     {
         using (var img = ImageUtils.GetImageFromFile(cache.GetPathToResizedImage(file.Path)))
         {
             Assert.AreEqual(10, img.Width);
         }
     }
 }
 public void GetWideImage_ReturnsShrunkImageWithCorrectProportions()
 {
     using (var cache = new RuntimeImageProcessor(new BookRenamedEvent()) { TargetDimension = 100 })
     using (var file = MakeTempPNGImage(200,80))
     {
         using(var img = ImageUtils.GetImageFromFile(cache.GetPathToResizedImage(file.Path)))
         {
             Assert.AreEqual(100, img.Width);
             Assert.AreEqual(40, img.Height);
         }
     }
 }
        public void GetJPG_ReturnsShrunkJPG()
        {
            using (var cache = new RuntimeImageProcessor(new BookRenamedEvent()) { TargetDimension = 100 })
            using (var file = MakeTempJPGImage(200, 80))
            {
                var pathToResizedImage = cache.GetPathToResizedImage(file.Path);
                using (var img = ImageUtils.GetImageFromFile(pathToResizedImage))
                {
                    Assert.AreEqual(".jpg", Path.GetExtension(pathToResizedImage));

                    //TODO: why does this always report PNG format? Checks by hand of the file show it as jpg
                    //Assert.AreEqual(ImageFormat.Jpeg.Guid, img.RawFormat.Guid);

                    Assert.AreEqual(100, img.Width);
                    Assert.AreEqual(40, img.Height);
                }
            }
        }
Exemplo n.º 4
0
        protected override bool ProcessRequest(IRequestInfo info)
        {
            if (base.ProcessRequest(info))
            {
                return(true);
            }

            if (!_useCache)
            {
                return(false);
            }

            var imageFile = GetLocalPathWithoutQuery(info);

            // only process images
            var isSvg = imageFile.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);

            if (!IsImageTypeThatCanBeDegraded(imageFile) && !isSvg)
            {
                return(false);
            }

            imageFile = imageFile.Replace("thumbnail", "");

            var processImage = !isSvg;

            if (imageFile.StartsWith(OriginalImageMarker + "/"))
            {
                processImage = false;
                imageFile    = imageFile.Substring((OriginalImageMarker + "/").Length);

                if (!RobustFile.Exists(imageFile))
                {
                    // We didn't find the file here, and don't want to use the following else if or we could errantly
                    // find it in the browser root. For example, this outer if (imageFile.StartsWith...) was added because
                    // we were accidentally finding license.png in a template book. See BL-4290.
                    return(false);
                }
            }
            // This happens with the new way we are serving css files
            else if (!RobustFile.Exists(imageFile))
            {
                var fileName  = Path.GetFileName(imageFile);
                var sourceDir = FileLocator.GetDirectoryDistributedWithApplication(BloomFileLocator.BrowserRoot);
                imageFile = Directory.EnumerateFiles(sourceDir, fileName, SearchOption.AllDirectories).FirstOrDefault();

                // image file not found
                if (string.IsNullOrEmpty(imageFile))
                {
                    return(false);
                }

                // BL-2368: Do not process files from the BloomBrowserUI directory. These files are already in the state we
                //          want them. Running them through _cache.GetPathToResizedImage() is not necessary, and in PNG files
                //          it converts all white areas to transparent. This is resulting in icons which only contain white
                //          (because they are rendered on a dark background) becoming completely invisible.
                processImage = false;
            }

            var originalImageFile = imageFile;

            if (processImage)
            {
                // thumbnail requests have the thumbnail parameter set in the query string
                var thumb = info.GetQueryParameters()["thumbnail"] != null;
                imageFile = _cache.GetPathToResizedImage(imageFile, thumb);

                if (string.IsNullOrEmpty(imageFile))
                {
                    return(false);
                }
            }

            info.ReplyWithImage(imageFile, originalImageFile);
            return(true);
        }