Updates the image helper and imagescontroller to never scale images larger than what they are.

This commit is contained in:
Shannon
2013-12-10 18:24:20 +11:00
parent 200a400fc5
commit 6a6d9ec7b0
2 changed files with 53 additions and 16 deletions

View File

@@ -25,6 +25,15 @@ namespace Umbraco.Core.Media
return codec.MimeType;
}
/// <summary>
/// Creates the thumbnails if the image is larger than all of the specified ones.
/// </summary>
/// <param name="fs"></param>
/// <param name="fileName"></param>
/// <param name="extension"></param>
/// <param name="originalImage"></param>
/// <param name="additionalThumbSizes"></param>
/// <returns></returns>
internal static IEnumerable<ResizedImage> GenerateMediaThumbnails(
IFileSystem fs,
string fileName,
@@ -32,22 +41,33 @@ namespace Umbraco.Core.Media
Image originalImage,
IEnumerable<int> additionalThumbSizes)
{
// Make default thumbnails
var thumbs = new List<ResizedImage>
{
Resize(fs, fileName, extension, 100, "thumb", originalImage),
Resize(fs, fileName, extension, 500, "big-thumb", originalImage)
};
//make custom ones
foreach (var thumb in additionalThumbSizes.Where(x => x > 0).Distinct())
var result = new List<ResizedImage>();
var allSizes = new List<int> {100, 500};
allSizes.AddRange(additionalThumbSizes.Where(x => x > 0).Distinct());
foreach (var s in allSizes)
{
thumbs.Add(Resize(fs, fileName, extension, thumb, string.Format("thumb_{0}", thumb), originalImage));
if (originalImage.Width >= s && originalImage.Height >= s)
{
result.Add(Resize(fs, fileName, extension, s, "thumb", originalImage));
}
}
return thumbs;
return result;
}
/// <summary>
/// Performs an image resize
/// </summary>
/// <param name="fileSystem"></param>
/// <param name="path"></param>
/// <param name="extension"></param>
/// <param name="maxWidthHeight"></param>
/// <param name="fileNameAddition"></param>
/// <param name="originalImage"></param>
/// <returns></returns>
private static ResizedImage Resize(IFileSystem fileSystem, string path, string extension, int maxWidthHeight, string fileNameAddition, Image originalImage)
{
var fileNameThumb = String.IsNullOrEmpty(fileNameAddition)

View File

@@ -100,6 +100,13 @@ namespace Umbraco.Web.Editors
return GetResized(imagePath, width, Convert.ToString(width));
}
/// <summary>
/// Gets a resized image - if the requested max width is greater than the original image, only the original image will be returned.
/// </summary>
/// <param name="imagePath"></param>
/// <param name="width"></param>
/// <param name="suffix"></param>
/// <returns></returns>
private HttpResponseMessage GetResized(string imagePath, int width, string suffix)
{
var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
@@ -121,12 +128,22 @@ namespace Umbraco.Web.Editors
if (fileStream.CanSeek) fileStream.Seek(0, 0);
using (var originalImage = Image.FromStream(fileStream))
{
ImageHelper.GenerateThumbnail(
originalImage,
width,
fullNewPath,
"jpg",
mediaFileSystem);
//If it is bigger, then do the resize
if (originalImage.Width >= width && originalImage.Height >= width)
{
ImageHelper.GenerateThumbnail(
originalImage,
width,
fullNewPath,
"jpg",
mediaFileSystem);
}
else
{
//just return the original image
fullNewPath = fullOrgPath;
}
}
}
}