回到顶部

简要说明jquery+jcrop实现的图片裁剪保存

时间:4年前   作者:庞顺龙   浏览:632   [站内原创,转载请注明出处]

标签: JavaScript   jQuery   AJAX  

代码更多的是思路,请各位看官勿喷~

#region 编辑头像:保存上传头像
Post["/common/SaveHeadImg"] = parameters =>
{
    if (Page.UserHasLogin)
    {
        try
        {
            //获取页面截图参数,生成新的用户头像图片  
            UserImgUpload userImgUpload = this.Bind<UserImgUpload>();

            if (userImgUpload == null)
            {
                return JSONHelper.JsonResult(0, "");
            }

            string xstr = userImgUpload.x1;
            string ystr = userImgUpload.y1;
            string wstr = userImgUpload.w;
            string hstr = userImgUpload.h;
            string FileUrl = userImgUpload.FileUrl;
            string updateType = userImgUpload.updateType;
            string fileLocationOnServer = HttpContext.Current.Server.MapPath(FileUrl);

            //根据原始图片高宽和 870px 进行比例计算得到  Ratio 
            string Ratio = "1";
            System.Drawing.Image image = System.Drawing.Image.FromFile(fileLocationOnServer);

            if (image.Width > 870 && image.Width >= image.Height)
            {
                Ratio = (TranDataType.StrToFloat(870) / TranDataType.StrToFloat(image.Width)).ToString();
            }
            else if (image.Height > 600 && image.Height >= image.Width)
            {
                Ratio = (TranDataType.StrToFloat(600) / TranDataType.StrToFloat(image.Height)).ToString();
            }

            string savePath = "/HeadImage/" + sysWebUser.UserLoginName + "/" + sysWebUser.UserLoginName + "_original.jpg";
            int x = TranDataType.ObjToInt(Math.Round(TranDataType.ObjToInt(xstr) / TranDataType.StrToDouble(Ratio)));
            int y = TranDataType.ObjToInt(Math.Round(TranDataType.ObjToInt(ystr) / TranDataType.StrToDouble(Ratio)));
            int w = TranDataType.ObjToInt(Math.Round(TranDataType.ObjToInt(wstr) / TranDataType.StrToDouble(Ratio)));
            int h = TranDataType.ObjToInt(Math.Round(TranDataType.ObjToInt(hstr) / TranDataType.StrToDouble(Ratio)));

            ImageCut ic = new ImageCut(x, y, w, h);
            System.Drawing.Bitmap bm = new Bitmap(fileLocationOnServer);
            System.Drawing.Bitmap cuted = ic.KiCut(bm);
            string cutPath = System.Web.HttpContext.Current.Server.MapPath(savePath);
            string wjjPath = System.Web.HttpContext.Current.Server.MapPath("/HeadImage/" + sysWebUser.UserLoginName + "/");

            if (!Directory.Exists(wjjPath))
            {
                Directory.CreateDirectory(wjjPath);
            }

            //删除现有的头像文件
            
            //保存原始截图文件 Original Picture
            cuted.Save(cutPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            //生成 150*150 头像
            string userimg150 = "/HeadImage/" + sysWebUser.UserLoginName + "/" + sysWebUser.UserLoginName + "_150_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(cutPath);
            var thumb = originalImage.GetThumbnailImage(150, 150, () => false, IntPtr.Zero);
            thumb.Save(System.Web.HttpContext.Current.Server.MapPath(userimg150));

            //生成 70*70 头像
            string userimg70 = "/HeadImage/" + sysWebUser.UserLoginName + "/" + sysWebUser.UserLoginName + "_70_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            System.Drawing.Image originalImage2 = System.Drawing.Image.FromFile(cutPath);
            var thumb2 = originalImage2.GetThumbnailImage(70, 70, () => false, IntPtr.Zero);
            thumb2.Save(System.Web.HttpContext.Current.Server.MapPath(userimg70));

            //必须释放所有进程资源,否则无法删除原始图片
            originalImage.Dispose();
            originalImage2.Dispose();

            cuted.Dispose();
            bm.Dispose();

            image.Dispose();

            //删除原始上传图片
            File.Delete(fileLocationOnServer);
            File.Delete(cutPath);

            //删除临时上传文件夹 
            //FileHelper.DeleteDir(HttpContext.Current.Server.MapPath("../HeadImageTemp/" + sysWebUser.UserLoginName));

            //修改个人信息
            
            return Response.AsRedirect("/PersonalHeadImage");

        }
        catch (Exception ex)
        {
            MessageHelper.WriteOperateExceptionLog("/common/SaveHeadImg", "SaveHeadImg", ex.Message + " | " + ex.StackTrace, "", "");
            return JSONHelper.JsonResult(0, "");
        }
    }
    else
    {
        return Response.AsRedirect("/");
    }
    return null;
};
#endregion


庞顺龙最后编辑于:4年前

内容均为作者独立观点,不代表八零IT人立场,如涉及侵权,请及时告知。

评论努力加载中...
暂无评论
暂无评论

手机扫码阅读

热门相关

加载中...
关于我们   联系我们   申请友链   赞助记录   站点地图
© 2014 - 2017 www.80iter.com All Rights Reserved. 京ICP备14042174号-1
本站遵循 CC BY 4.0 协议,转载请注明出处 。