Skip to content

Commit

Permalink
Added configuration-less functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Mar 18, 2014
1 parent d730995 commit 4986877
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Data" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\NET45\Caching\CachedImage.cs" />
Expand All @@ -90,6 +91,9 @@
<Link>ImageHelpers.cs</Link>
</Compile>
<Compile Include="..\NET45\Helpers\RemoteFile.cs" />
<Compile Include="..\NET45\Helpers\ResourceHelpers.cs">
<Link>ResourceHelpers.cs</Link>
</Compile>
<Compile Include="..\NET45\Helpers\StartUp.cs">
<Link>StartUp.cs</Link>
</Compile>
Expand All @@ -105,6 +109,15 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\NET45\Config\Resources\cache.config">
<Link>Config\Resources\cache.config</Link>
</EmbeddedResource>
<EmbeddedResource Include="..\NET45\Config\Resources\processing.config">
<Link>Config\Resources\processing.config</Link>
</EmbeddedResource>
<EmbeddedResource Include="..\NET45\Config\Resources\security.config">
<Link>Config\Resources\security.config</Link>
</EmbeddedResource>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
Expand Down
25 changes: 18 additions & 7 deletions src/ImageProcessor.Web/NET45/Config/ImageCacheSection.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
// -----------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageCacheSection.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// -----------------------------------------------------------------------
// <summary>
// Represents an image cache section within a configuration file.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ImageProcessor.Web.Config
{
#region Using
using System.Configuration;
using System.IO;
using System.Xml;

using ImageProcessor.Extensions;
using ImageProcessor.Web.Helpers;

#endregion

Expand All @@ -24,7 +30,7 @@ public sealed class ImageCacheSection : ConfigurationSection
/// </summary>
/// <value>The name of the cache folder.</value>
[ConfigurationProperty("virtualPath", DefaultValue = "~/app_data/cache", IsRequired = true)]
[StringValidator(MinLength = 3, MaxLength = 200)]
[StringValidator(MinLength = 3, MaxLength = 256)]
public string VirtualPath
{
get
Expand All @@ -45,7 +51,7 @@ public string VirtualPath
/// </summary>
/// <value>The maximum number of days to store an image in the cache.</value>
/// <remarks>Defaults to 28 if not set.</remarks>
[ConfigurationProperty("maxDays", DefaultValue = "28", IsRequired = false)]
[ConfigurationProperty("maxDays", DefaultValue = "365", IsRequired = false)]
[IntegerValidator(ExcludeRange = false, MinValue = 0)]
public int MaxDays
{
Expand Down Expand Up @@ -73,7 +79,12 @@ public static ImageCacheSection GetConfiguration()
return imageCacheSection;
}

return new ImageCacheSection();
string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.cache.config");
XmlReader reader = new XmlTextReader(new StringReader(section));
imageCacheSection = new ImageCacheSection();
imageCacheSection.DeserializeSection(reader);

return imageCacheSection;
}
}
}
10 changes: 9 additions & 1 deletion src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ namespace ImageProcessor.Web.Config
{
#region Using
using System.Configuration;
using System.IO;
using System.Linq;
using System.Xml;
using ImageProcessor.Web.Helpers;
#endregion

/// <summary>
Expand Down Expand Up @@ -69,7 +72,12 @@ public static ImageProcessingSection GetConfiguration()
return imageProcessingSection;
}

return new ImageProcessingSection();
string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.processing.config");
XmlReader reader = new XmlTextReader(new StringReader(section));
imageProcessingSection = new ImageProcessingSection();
imageProcessingSection.DeserializeSection(reader);

return imageProcessingSection;
}
#endregion

Expand Down
16 changes: 13 additions & 3 deletions src/ImageProcessor.Web/NET45/Config/ImageSecuritySection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace ImageProcessor.Web.Config
#region Using
using System;
using System.Configuration;
using System.IO;
using System.Xml;

using ImageProcessor.Web.Helpers;

#endregion

/// <summary>
Expand Down Expand Up @@ -55,8 +60,8 @@ public int Timeout
/// Gets or sets the maximum allowed remote file size in bytes for the application.
/// </summary>
/// <value>The maximum number of days to store an image in the cache.</value>
/// <remarks>Defaults to 524288 (512kb) if not set.</remarks>
[ConfigurationProperty("maxBytes", DefaultValue = "524288", IsRequired = true)]
/// <remarks>Defaults to 4194304 (4Mb) if not set.</remarks>
[ConfigurationProperty("maxBytes", DefaultValue = "4194304", IsRequired = true)]
public int MaxBytes
{
get
Expand Down Expand Up @@ -111,7 +116,12 @@ public static ImageSecuritySection GetConfiguration()
return imageSecuritySection;
}

return new ImageSecuritySection();
string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.security.config");
XmlReader reader = new XmlTextReader(new StringReader(section));
imageSecuritySection = new ImageSecuritySection();
imageSecuritySection.DeserializeSection(reader);

return imageSecuritySection;
}
#endregion

Expand Down
1 change: 1 addition & 0 deletions src/ImageProcessor.Web/NET45/Config/Resources/cache.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<cache virtualPath="~/app_data/cache" maxDays="356"/>
39 changes: 39 additions & 0 deletions src/ImageProcessor.Web/NET45/Config/Resources/processing.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<processing>
<presets>
</presets>
<plugins autoLoadPlugins="true">
<plugin name="Alpha" type="ImageProcessor.Processors.Alpha, ImageProcessor"/>
<plugin name="Brightness" type="ImageProcessor.Processors.Brightness, ImageProcessor"/>
<plugin name="Contrast" type="ImageProcessor.Processors.Contrast, ImageProcessor"/>
<plugin name="Crop" type="ImageProcessor.Processors.Crop, ImageProcessor"/>
<plugin name="Filter" type="ImageProcessor.Processors.Filter, ImageProcessor"/>
<plugin name="Flip" type="ImageProcessor.Processors.Flip, ImageProcessor"/>
<plugin name="Format" type="ImageProcessor.Processors.Format, ImageProcessor"/>
<plugin name="GaussianBlur" type="ImageProcessor.Processors.GaussianBlur, ImageProcessor">
<settings>
<setting key="MaxSize" value="22"/>
<setting key="MaxSigma" value="5.1"/>
<setting key="MaxThreshold" value="100"/>
</settings>
</plugin>
<plugin name="GaussianSharpen" type="ImageProcessor.Processors.GaussianSharpen, ImageProcessor">
<settings>
<setting key="MaxSize" value="22"/>
<setting key="MaxSigma" value="5.1"/>
<setting key="MaxThreshold" value="100"/>
</settings>
</plugin>
<plugin name="Quality" type="ImageProcessor.Processors.Quality, ImageProcessor"/>
<plugin name="Resize" type="ImageProcessor.Processors.Resize, ImageProcessor">
<settings>
<setting key="MaxWidth" value="5000"/>
<setting key="MaxHeight" value="5000"/>
</settings>
</plugin>
<plugin name="Rotate" type="ImageProcessor.Processors.Rotate, ImageProcessor"/>
<plugin name="RoundedCorners" type="ImageProcessor.Processors.RoundedCorners, ImageProcessor"/>
<plugin name="Saturation" type="ImageProcessor.Processors.Saturation, ImageProcessor"/>
<plugin name="Vignette" type="ImageProcessor.Processors.Vignette, ImageProcessor"/>
<plugin name="Watermark" type="ImageProcessor.Processors.Watermark, ImageProcessor"/>
</plugins>
</processing>
4 changes: 4 additions & 0 deletions src/ImageProcessor.Web/NET45/Config/Resources/security.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<security allowRemoteDownloads="true" timeout="300000" maxBytes="4194304" remotePrefix="/remote.axd">
<whiteList>
</whiteList>
</security>
56 changes: 56 additions & 0 deletions src/ImageProcessor.Web/NET45/Helpers/ResourceHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ResourceHelpers.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Provides helper methods for working with resources.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ImageProcessor.Web.Helpers
{
using System.IO;
using System.Reflection;
using System.Text;

/// <summary>
/// Provides helper methods for working with resources.
/// </summary>
public class ResourceHelpers
{
/// <summary>
/// Converts an assembly resource into a string.
/// </summary>
/// <param name="resource">
/// The resource.
/// </param>
/// <param name="assembly">
/// The assembly.
/// </param>
/// <param name="encoding">
/// The character encoding to return the resource in.
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
public static string ResourceAsString(string resource, Assembly assembly = null, Encoding encoding = null)
{
assembly = assembly ?? Assembly.GetExecutingAssembly();
encoding = encoding ?? Encoding.UTF8;

using (MemoryStream ms = new MemoryStream())
{
using (Stream manifestResourceStream = assembly.GetManifestResourceStream(resource))
{
if (manifestResourceStream != null)
{
manifestResourceStream.CopyTo(ms);
}
}

return encoding.GetString(ms.GetBuffer()).Replace('\0', ' ').Trim();
}
}
}
}
8 changes: 8 additions & 0 deletions src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Config\ImageProcessingSection.cs" />
<Compile Include="Config\ImageProcessorConfig.cs" />
<Compile Include="Config\ImageSecuritySection.cs" />
<Compile Include="Helpers\ResourceHelpers.cs" />
<Compile Include="Helpers\StartUp.cs" />
<Compile Include="Helpers\ImageHelpers.cs" />
<Compile Include="Helpers\RemoteFile.cs" />
Expand All @@ -72,6 +73,13 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Config\Resources\cache.config">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Config\Resources\processing.config">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Config\Resources\security.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
15 changes: 11 additions & 4 deletions src/ImageProcessor/Processors/Resize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,19 @@ private Image ResizeImage(

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;

graphics.Clear(backgroundColor);
Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight);
graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel);
// An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image
// as the algorithm appears to be pulling averaging detail from surrounding pixels beyond the edge
// of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror
// images of the pixels within solves this problem.
using (ImageAttributes wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.Clear(backgroundColor);
Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight);
graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel, wrapMode);
}

// Reassign the image.
image.Dispose();
Expand Down
Binary file added src/Nuget/ImageProcessor.Web.Config.1.0.0.0.nupkg
Binary file not shown.

0 comments on commit 4986877

Please sign in to comment.