修复DTcms所有版本中一处图片处理BUG

Hailin 发表于 2016-11-01 09:47:11 来源于CMS插件网 评论 0 点击 2054
摘要:这个BUG是DTcms历史遗留问题,从小编接触到的DTcms2.0的版本,一直到DTcms4.0、以及最新的DTcms4.0.3版本,这个问题一直存在,未得到修复。为避免延伸到DTcms5.0,所以公布这个BUG。

这个BUG是DTcms历史遗留问题,从小编接触到的DTcms2.0的版本,一直到DTcms4.0、以及最新的DTcms4.0.3版本,这个问题一直存在,未得到修复。为避免延伸到DTcms5.0,所以公布这个BUG。

相信很多人都有发现,DTcms系统,图片上传后,不管是否生成缩略图、还是裁剪,图片上传后,图片的大小会发生变化,且图片质量会有一定的损失,图片色彩稍微丰富一点的图片都会失真,仔细看会有噪点。

好了,问题说完了,我们来说说为什么会出现这个问题,首页我们打开DTcms.Web.UI.UpLoad.cs

//保存文件
postedFile.SaveAs(fullUpLoadPath + newFileName);
//如果是图片,检查图片是否超出最大尺寸,是则裁剪
if (IsImage(fileExt) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0))
{
    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight);
}

这一段代码严格来说没问题,不过小编有强迫症,硬改成了

if (IsImage(fileExt) && this.siteConfig.imgmaxheight > 0 && this.siteConfig.imgmaxwidth > 0)

这个可以不用改哈。

主要的问题出现,DTcms.Common.Thumbnail.cs  308

public static void MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight)
{
    //2012-02-05修改过,支持替换
    byte[] imageBytes = File.ReadAllBytes(fileName);
    Image img = Image.FromStream(new System.IO.MemoryStream(imageBytes));
    MakeThumbnailImage(img, newFileName, maxWidth, maxHeight);
    //原文
    //MakeThumbnailImage(Image.FromFile(fileName), newFileName, maxWidth, maxHeight);
}

修改成

public static void MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight)
{
    //2012-02-05修改过,支持替换
    byte[] imageBytes = File.ReadAllBytes(fileName);
    Image img = Image.FromStream(new System.IO.MemoryStream(imageBytes));
    if (img.Width > maxWidth || img.Height > maxHeight)
    {
        MakeThumbnailImage(img, newFileName, maxWidth, maxHeight);
    }
    //原文
    //MakeThumbnailImage(Image.FromFile(fileName), newFileName, maxWidth, maxHeight);
}

解决方法是这样,置于造成问题的原因,自己看看就明白了啥。

附件列表

  • 点击下载附件:DTcms 4.0 开源版修复补丁.rar 大小4KB 所需积分:0分 下载次数:236次

热门资讯