在ASP.NET中为FCKEditor添加生成缩略图功能
FCKEditor应该是目前用得比较多的免费的在线编辑器,它的功能完善,并支持多种编程语言
在图片管理中,它只有简单的图象文件上传功能,在实际的操作中,可能不得不为一张比较大的图片制作一张小图,重新上传
为了简化这一操作,考虑在FCKEditor中添加图象文件上传后自动等比生成对应的缩略图。
先找到FCKEditor FOR .net的类库文档。因为该版本比较早期,可能需要转换。
首先在FileWorkerBase.cs中添加
代码
private const string DEFAULT_THUMBNAIL_IMAGE_TYPE = ".gif.bmp.png.jpg.";//可供创建缩略图的文件类型
private const Int32 DEFAULT_THUMBNAIL_IMAGE_WIDTH = 240;//px 缩略图生成的宽度
private const Int32 DEFAULT_THUMBNAIL_IMAGE_HEIGHT = 180;//px 缩略图生成的高度
private Int32 sThumbnailImageWidth;
private Int32 sThumbnailImageHeight;
/// <summary>
/// 缩略图宽
/// </summary>
/// <value>The width of the thumbnail image.</value>
public Int32 ThumbnailImageWidth
{
get
{
object s = Application["FCKeditor:ThumbnailImageWidth"];
if (s == null)
{
s = Session["FCKeditor:ThumbnailImageWidth"];
if (s == null)
{
s = System.Configuration.ConfigurationSettings.AppSettings["FCKeditor:ThumbnailImageWidth"];
}
}
if (!Int32.TryParse(Convert.ToString(s), out sThumbnailImageWidth))
{
sThumbnailImageWidth = DEFAULT_THUMBNAIL_IMAGE_WIDTH;
}
if (sThumbnailImageWidth < 1)
{
sThumbnailImageWidth = DEFAULT_THUMBNAIL_IMAGE_WIDTH;
}
return sThumbnailImageWidth;
}
}
/// <summary>
/// 缩略图高
/// </summary>
/// <value>The Height of the thumbnail image.</value>
protected Int32 ThumbnailImageHeight
{
get
{
object s = Application["FCKeditor:ThumbnailImageHeight"];
if (s == null)
{
s = Session["FCKeditor:ThumbnailImageHeight"];
if (s == null)
{
s = System.Configuration.ConfigurationSettings.AppSettings["FCKeditor:ThumbnailImageHeight"];
}
}
if (!Int32.TryParse(Convert.ToString(s), out sThumbnailImageHeight))
{
sThumbnailImageHeight = DEFAULT_THUMBNAIL_IMAGE_HEIGHT;
}
if (sThumbnailImageHeight < 1)
{
sThumbnailImageHeight = DEFAULT_THUMBNAIL_IMAGE_HEIGHT;
}
return sThumbnailImageHeight;
}
}
/// <summary>
/// 可创建缩略图的文件类型
/// </summary>
/// <value>The type of the thumbnail image.</value>
protected string ThumbnailImageType
{
get
{
return DEFAULT_USER_FILES_TYPE;
}
}
/// <summary>
/// 创建缩略图,宽度指定,高度自适应
/// </summary>
/// <param name="filePath">文件路径</param>
public void CreateThumbnailImage(string filePath)
{
if (filePath == null || filePath.Trim().Length == 0)
{
return;//如果文件路径为空
}
string sFilePath = filePath.Replace("/", "\\");
string sFileFolder = sFilePath.Substring(0, sFilePath.LastIndexOf("\\") + 1);
string sFileName = sFilePath.Substring(0, sFilePath.LastIndexOf(".")).Substring(sFilePath.LastIndexOf("\\") + 1);
string sExt = System.IO.Path.GetExtension(sFilePath);
if (!this.ThumbnailImageType.ToLower().Contains(sExt.ToLower() + "."))//包含有效的可用于创建缩略图的文件类型
{
return;//如果文件不是图片文件
}
if (!System.IO.File.Exists(sFilePath))
{
return;//如果文件不存在
}
System.Drawing.Image img = null;
try
{
img = Bitmap.FromFile(sFilePath);
//if (img.Width <= this.ThumbnailImageWidth && img.Height <= this.ThumbnailImageHeight)
if (img.Width <= this.ThumbnailImageWidth)
{//如果文件宽小于缩略图尺寸
img.Dispose();
return;
}
double percent = (double)img.Width / (double)img.Height;
System.Drawing.Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.GetThumbnailImageAbort);
Image smallImg = img.GetThumbnailImage(this.ThumbnailImageWidth, Convert.ToInt32(this.ThumbnailImageWidth / percent), callback, IntPtr.Zero);
smallImg.Save(string.Format(@"{0}{1}_s.jpg", sFileFolder, sFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
smallImg.Dispose();
}
catch (Exception ex)
{
//TODO ANYTHING
//throw ex;
}
finally
{
if (img != null)
{
img.Dispose();
}
}
}
protected bool GetThumbnailImageAbort()
{
return false;
}
private const Int32 DEFAULT_THUMBNAIL_IMAGE_WIDTH = 240;//px 缩略图生成的宽度
private const Int32 DEFAULT_THUMBNAIL_IMAGE_HEIGHT = 180;//px 缩略图生成的高度
private Int32 sThumbnailImageWidth;
private Int32 sThumbnailImageHeight;
/// <summary>
/// 缩略图宽
/// </summary>
/// <value>The width of the thumbnail image.</value>
public Int32 ThumbnailImageWidth
{
get
{
object s = Application["FCKeditor:ThumbnailImageWidth"];
if (s == null)
{
s = Session["FCKeditor:ThumbnailImageWidth"];
if (s == null)
{
s = System.Configuration.ConfigurationSettings.AppSettings["FCKeditor:ThumbnailImageWidth"];
}
}
if (!Int32.TryParse(Convert.ToString(s), out sThumbnailImageWidth))
{
sThumbnailImageWidth = DEFAULT_THUMBNAIL_IMAGE_WIDTH;
}
if (sThumbnailImageWidth < 1)
{
sThumbnailImageWidth = DEFAULT_THUMBNAIL_IMAGE_WIDTH;
}
return sThumbnailImageWidth;
}
}
/// <summary>
/// 缩略图高
/// </summary>
/// <value>The Height of the thumbnail image.</value>
protected Int32 ThumbnailImageHeight
{
get
{
object s = Application["FCKeditor:ThumbnailImageHeight"];
if (s == null)
{
s = Session["FCKeditor:ThumbnailImageHeight"];
if (s == null)
{
s = System.Configuration.ConfigurationSettings.AppSettings["FCKeditor:ThumbnailImageHeight"];
}
}
if (!Int32.TryParse(Convert.ToString(s), out sThumbnailImageHeight))
{
sThumbnailImageHeight = DEFAULT_THUMBNAIL_IMAGE_HEIGHT;
}
if (sThumbnailImageHeight < 1)
{
sThumbnailImageHeight = DEFAULT_THUMBNAIL_IMAGE_HEIGHT;
}
return sThumbnailImageHeight;
}
}
/// <summary>
/// 可创建缩略图的文件类型
/// </summary>
/// <value>The type of the thumbnail image.</value>
protected string ThumbnailImageType
{
get
{
return DEFAULT_USER_FILES_TYPE;
}
}
/// <summary>
/// 创建缩略图,宽度指定,高度自适应
/// </summary>
/// <param name="filePath">文件路径</param>
public void CreateThumbnailImage(string filePath)
{
if (filePath == null || filePath.Trim().Length == 0)
{
return;//如果文件路径为空
}
string sFilePath = filePath.Replace("/", "\\");
string sFileFolder = sFilePath.Substring(0, sFilePath.LastIndexOf("\\") + 1);
string sFileName = sFilePath.Substring(0, sFilePath.LastIndexOf(".")).Substring(sFilePath.LastIndexOf("\\") + 1);
string sExt = System.IO.Path.GetExtension(sFilePath);
if (!this.ThumbnailImageType.ToLower().Contains(sExt.ToLower() + "."))//包含有效的可用于创建缩略图的文件类型
{
return;//如果文件不是图片文件
}
if (!System.IO.File.Exists(sFilePath))
{
return;//如果文件不存在
}
System.Drawing.Image img = null;
try
{
img = Bitmap.FromFile(sFilePath);
//if (img.Width <= this.ThumbnailImageWidth && img.Height <= this.ThumbnailImageHeight)
if (img.Width <= this.ThumbnailImageWidth)
{//如果文件宽小于缩略图尺寸
img.Dispose();
return;
}
double percent = (double)img.Width / (double)img.Height;
System.Drawing.Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.GetThumbnailImageAbort);
Image smallImg = img.GetThumbnailImage(this.ThumbnailImageWidth, Convert.ToInt32(this.ThumbnailImageWidth / percent), callback, IntPtr.Zero);
smallImg.Save(string.Format(@"{0}{1}_s.jpg", sFileFolder, sFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
smallImg.Dispose();
}
catch (Exception ex)
{
//TODO ANYTHING
//throw ex;
}
finally
{
if (img != null)
{
img.Dispose();
}
}
}
protected bool GetThumbnailImageAbort()
{
return false;
}
在FileBrowserConnector.css中修改FileUpload方法(在文件上传成功后的处理代码后面添加)
//对图片文件生成缩略图
this.CreateThumbnailImage(sFilePath);
this.CreateThumbnailImage(sFilePath);
在Uploader.cs中修改OnLoad方法,同样在文件上传成功后的处理代码后面添加上面的代码
完成后,FCKEditor自动在上传图象文件后生成缩略图,缩略图自动在原来的文件名后添加_s,并以jpg格式保存
当然它只对指定格式的图象文件,并且大小超过设置的宽度时才会生成缩略图。
希望此文件对大家有所帮助