Created
August 17, 2017 09:30
-
-
Save restlessmedia/9401155f40bd12d40d6affb53c4231b5 to your computer and use it in GitHub Desktop.
Fixes ResponseStream writing issue identified here https://stackoverflow.com/questions/45729149/when-disabling-the-diskcache-plugin-images-fail-to-display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ImageResizer; | |
using ImageResizer.Configuration; | |
using ImageResizer.Encoding; | |
using ImageResizer.Plugins; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
namespace ImageResizer.CustomPlugins | |
{ | |
public class DefaultEncoder : IEncoder, IQuerystringPlugin, IPlugin, IFileSignatureProvider | |
{ | |
public DefaultEncoder() | |
{ | |
_encoder = new ImageResizer.Plugins.Basic.DefaultEncoder(); | |
} | |
public DefaultEncoder(ImageFormat outputFormat) | |
{ | |
_encoder = new ImageResizer.Plugins.Basic.DefaultEncoder(outputFormat); | |
} | |
public DefaultEncoder(ImageFormat outputFormat, int jpegQuality) | |
{ | |
_encoder = new ImageResizer.Plugins.Basic.DefaultEncoder(outputFormat, jpegQuality); | |
} | |
public DefaultEncoder(ResizeSettings settings, object original) | |
{ | |
_encoder = new ImageResizer.Plugins.Basic.DefaultEncoder(settings, original); | |
} | |
public bool SupportsTransparency | |
{ | |
get | |
{ | |
return _encoder.SupportsTransparency; | |
} | |
} | |
public string MimeType | |
{ | |
get | |
{ | |
return _encoder.MimeType; | |
} | |
} | |
public string Extension | |
{ | |
get | |
{ | |
return _encoder.Extension; | |
} | |
} | |
public IEncoder CreateIfSuitable(ResizeSettings settings, object original) | |
{ | |
ImageFormat requestedFormat = ImageResizer.Plugins.Basic.DefaultEncoder.GetRequestedFormat(settings.Format, ImageFormat.Jpeg); | |
if (requestedFormat == null || !_encoder.IsValidOutputFormat(requestedFormat)) | |
return null; //An unsupported format was explicitly specified. | |
return new DefaultEncoder(settings, original); | |
} | |
public void Write(Image i, Stream s) | |
{ | |
if (ImageFormat.Jpeg.Equals(_encoder.OutputFormat)) | |
SaveJpeg(i, s, _encoder.Quality); | |
else if (ImageFormat.Png.Equals(_encoder.OutputFormat)) | |
Save(i, s, ImageFormat.Png); | |
else if (ImageFormat.Gif.Equals(_encoder.OutputFormat)) | |
Save(i, s, ImageFormat.Gif); | |
} | |
public IEnumerable<string> GetSupportedQuerystringKeys() | |
{ | |
return _encoder.GetSupportedQuerystringKeys(); | |
} | |
public IPlugin Install(Config c) | |
{ | |
c.Plugins.add_plugin(this); | |
return this; | |
} | |
public bool Uninstall(Config c) | |
{ | |
c.Plugins.remove_plugin(this); | |
return true; | |
} | |
public IEnumerable<FileSignature> GetSignatures() | |
{ | |
return _encoder.GetSignatures(); | |
} | |
private void Save(Image img, Stream target, ImageFormat format) | |
{ | |
if (!target.CanSeek) | |
{ | |
using (MemoryStream ms = new MemoryStream(_streamCopyBuffer)) | |
{ | |
img.Save(ms, format); | |
ms.WriteTo(target); | |
} | |
} | |
else | |
{ | |
img.Save(target, format); | |
} | |
} | |
private static void SaveJpeg(Image image, Stream target, int quality) | |
{ | |
if (quality < 0) | |
quality = 90; //90 is a very good default to stick with. | |
if (quality > 100) | |
quality = 100; | |
using (EncoderParameters p = new EncoderParameters(1)) | |
{ | |
using (EncoderParameter ep = new EncoderParameter(Encoder.Quality, quality)) | |
{ | |
p.Param[0] = ep; | |
ImageCodecInfo info = ImageResizer.Plugins.Basic.DefaultEncoder.GetImageCodeInfo("image/jpeg"); | |
if (!target.CanSeek) | |
{ | |
using (MemoryStream ms = new MemoryStream(_streamCopyBuffer)) | |
{ | |
image.Save(ms, info, p); | |
ms.WriteTo(target); | |
} | |
} | |
else | |
{ | |
image.Save(target, info, p); | |
} | |
} | |
} | |
} | |
private readonly ImageResizer.Plugins.Basic.DefaultEncoder _encoder; | |
private const int _streamCopyBuffer = 4096; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<configSections> | |
<section name="resizer" type="ImageResizer.ResizerSection,ImageResizer" requirePermission="false" /> | |
</configSections> | |
<resizer> | |
<plugins> | |
<remove name="DefaultEncoder" /> | |
<add name="ImageResizer.CustomPlugins.DefaultEncoder, ImageResizer.CustomPlugins" /> | |
<add name="DiskCache" /> | |
</plugins> | |
<diskcache dir="~/test" enabled="false" /> | |
</resizer> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment