Exemple #1
0
        public void Test_Optimize()
        {
            IScriptData scriptData = new TestScriptData()
            {
                OutputFormat = MagickFormat.Jpg,
                Script       = XElement.Load(Files.Scripts.Draw).CreateNavigator(),
            };

            IImageData imageData = new FileImageData(Files.ImageMagickJPG, JpgFormatInfo);

            Test_Optimize(imageData, scriptData);

            TestStreamUrlResolver resolver = new TestStreamUrlResolver(Files.ImageMagickJPG);

            imageData = new StreamImageData(resolver, JpgFormatInfo);
            Test_Optimize(imageData, scriptData);
        }
Exemple #2
0
        public void Test_ProcessRequest()
        {
            TestScriptData scriptData = new TestScriptData()
            {
                OutputFormat = MagickFormat.Png,
                Script       = XElement.Load(Files.Scripts.Resize).CreateNavigator(),
            };

            IImageData imageData = new FileImageData(Files.ImageMagickJPG, JpgFormatInfo);

            Test_ProcessRequest(imageData, scriptData);

            scriptData.OutputFormat = MagickFormat.Png;

            TestStreamUrlResolver resolver = new TestStreamUrlResolver(Files.ImageMagickJPG);

            imageData = new StreamImageData(resolver, JpgFormatInfo);
            Test_ProcessRequest(imageData, scriptData);
        }
Exemple #3
0
        private void Test_ProcessRequest(IImageData imageData, TestScriptData scriptData)
        {
            using (TemporaryDirectory directory = new TemporaryDirectory())
            {
                string tempDir = directory.FullName;

                string config = $@"<magick.net.web cacheDirectory=""{tempDir}"" tempDirectory=""{tempDir}""/>";

                MagickWebSettings settings = TestSectionLoader.Load(config);

                HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty);

                string outputFile = Path.Combine(tempDir, "output.png");

                using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    MagickScriptHandler handler = new MagickScriptHandler(settings, imageData, scriptData);
                    handler.ProcessRequest(context);
                }

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Png, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }

                Assert.AreEqual(2, tempDir.GetFiles().Count());

                File.Delete(outputFile);

                FileInfo cacheFile = tempDir.GetFiles().First();
                File.WriteAllText(cacheFile.FullName, string.Empty);

                using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    MagickScriptHandler handler = new MagickScriptHandler(settings, imageData, scriptData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(0, File.ReadAllBytes(outputFile).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                cacheFile.LastWriteTimeUtc = new DateTime(1979, 11, 19);

                using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    MagickScriptHandler handler = new MagickScriptHandler(settings, imageData, scriptData);
                    handler.ProcessRequest(context);
                }

                Assert.AreNotEqual(0, File.ReadAllBytes(cacheFile.FullName).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Png, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }

                scriptData.OutputFormat = MagickFormat.Tiff;

                outputFile = Path.Combine(tempDir, "output.tiff");

                using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    MagickScriptHandler handler = new MagickScriptHandler(settings, imageData, scriptData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(4, tempDir.GetFiles().Count());

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Tiff, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }
            }
        }