簡要說明jquery+jcrop實現的圖片裁剪儲存

龐順龍發表於2019-05-11

程式碼更多的是思路,請各位看官勿噴~

#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人立場,如涉及侵權,請及時告知。

相關文章