/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using InteSoft.Web.ExternalResourceLoader;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
namespace Everest.CmsServices.Controllers{
public class WebResourceController : ExternalResourceController
{
enum AnchorPosition
{
Top,
Center,
Bottom,
Left,
Right
}
/// <summary>
/// Resizes the image.
/// <remarks>
/// thanks Hendrik: http://forum.kooboo.com/yaf_postst121_An-update-to-one-CmsServicesController-by-member-Hendrik.aspx
/// </remarks>
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="cache">The cache.</param>
/// <returns></returns>
public ActionResult ResizeImage(string url, int width, int height, bool? cache)
{
var imageFullPath = Server.MapPath(HttpUtility.UrlDecode(url));
// create an image and calculate the width/height.
using (System.Drawing.Image img = System.Drawing.Image.FromFile(imageFullPath))
{
this.ControllerContext.HttpContext.Response.ContentType = "image/png";
//Bitmap with final width and height
Bitmap Target = new Bitmap(width, height);
//Resizing
using (Graphics graphics = Graphics.FromImage(Target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.InterpolationMode = InterpolationMode.High;
/** FISHBLUB **/
AnchorPosition anchor = AnchorPosition.Center;
int sourceWidth = img.Width;
int sourceHeight = img.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)width / (float)sourceWidth);
nPercentH = ((float)height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentW;
switch (anchor)
{
case AnchorPosition.Top:
destY = 0;
break;
case AnchorPosition.Bottom:
destY = (int)
(height - (sourceHeight * nPercent));
break;
default:
destY = (int)
((height - (sourceHeight * nPercent)) / 2);
break;
}
}
else
{
nPercent = nPercentH;
switch (anchor)
{
case AnchorPosition.Left:
destX = 0;
break;
case AnchorPosition.Right:
destX = (int)
(width - (sourceWidth * nPercent));
break;
default:
destX = (int)
((width - (sourceWidth * nPercent)) / 2);
break;
}
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(width,
height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(img.HorizontalResolution,
img.VerticalResolution);
//Graphics grPhoto = Graphics.FromImage(bmPhoto);
graphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;
graphics.DrawImage(img,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
/** END FISHBLUB **/
//graphics.DrawImage(img, 0, 0, width, height);
// Target = this.ConvertToGrayscale(Target);
using (MemoryStream memoryStream = new MemoryStream())
{
//if (quality == "low")
//{
// Target.Save(memoryStream, ImageFormat.Gif);
//}
//else
//{
Target.Save(memoryStream, ImageFormat.Jpeg);
//}
memoryStream.WriteTo(this.ControllerContext.HttpContext.Response.OutputStream);
}
}
}
if (cache != null && cache.Value == true)
{
DateTime timestamp = HttpContext.Timestamp;
HttpCachePolicyBase cachePolicy = this.ControllerContext.HttpContext.Response.Cache;
int duration = 2592000;
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.SetNoServerCaching();
cachePolicy.SetExpires(timestamp.AddSeconds(duration));
cachePolicy.SetMaxAge(new TimeSpan(0, 0, duration));
cachePolicy.SetValidUntilExpires(true);
cachePolicy.SetLastModified(timestamp);
cachePolicy.VaryByHeaders["Accept-Encoding"] = true;
cachePolicy.VaryByParams["name"] = true;
cachePolicy.VaryByParams["version"] = true;
cachePolicy.VaryByParams["display"] = true;
cachePolicy.VaryByParams["condition"] = true;
cachePolicy.SetOmitVaryStar(true);
}
return null;
}
//public ActionResult ResizeImage(string url, int width, int height, bool? cache)
//{
// var imageFullPath = Server.MapPath(HttpUtility.UrlDecode(url));
// // create an image and calculate the width/height.
// System.Drawing.Image img = System.Drawing.Image.FromFile(imageFullPath);
// this.ControllerContext.HttpContext.Response.ContentType = "image/png";
// Bitmap Target = new Bitmap(width, height);
// using (Graphics graphics = Graphics.FromImage(Target))
// {
// graphics.CompositingQuality = CompositingQuality.HighSpeed;
// graphics.CompositingMode = CompositingMode.SourceCopy;
// graphics.InterpolationMode = InterpolationMode.High;
// graphics.DrawImage(img, 0, 0, width, height);
// // Target = this.ConvertToGrayscale(Target);
// using (MemoryStream memoryStream = new MemoryStream())
// {
// //if (quality == "low")
// //{
// // Target.Save(memoryStream, ImageFormat.Gif);
// //}
// //else
// //{
// Target.Save(memoryStream, ImageFormat.Jpeg);
// //}
// memoryStream.WriteTo(this.ControllerContext.HttpContext.Response.OutputStream);
// }
// }
// if (cache != null && cache.Value == true)
// {
// DateTime timestamp = HttpContext.Timestamp;
// HttpCachePolicyBase cachePolicy = this.ControllerContext.HttpContext.Response.Cache;
// int duration = 2592000;
// cachePolicy.SetCacheability(HttpCacheability.Public);
// cachePolicy.SetNoServerCaching();
// cachePolicy.SetExpires(timestamp.AddSeconds(duration));
// cachePolicy.SetMaxAge(new TimeSpan(0, 0, duration));
// cachePolicy.SetValidUntilExpires(true);
// cachePolicy.SetLastModified(timestamp);
// cachePolicy.VaryByHeaders["Accept-Encoding"] = true;
// cachePolicy.VaryByParams["name"] = true;
// cachePolicy.VaryByParams["version"] = true;
// cachePolicy.VaryByParams["display"] = true;
// cachePolicy.VaryByParams["condition"] = true;
// cachePolicy.SetOmitVaryStar(true);
// }
// return null;
//}
}
}
|