繁体   English   中英

如何使用croppie js裁剪图像并在asp.net mvc中上传

[英]How to crop image using croppie js and upload in asp.net mvc

嗨,我想裁剪图像并将其上传到服务器上。

我正在使用croppie js插件并使用get()方法通过使用WebImage类在服务器上获取点来裁剪它。

Asp.net MVC 代码

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ImageCrop(FormCollection fc)
    {
                WebImage data = WebImage.GetImageFromRequest();
                if (data != null)
                {
                    int x1, y1, x2, y2;
                    x1 = int.Parse(fc["x1"].ToString());
                    y1 = int.Parse(fc["y1"].ToString());
                    x2 = int.Parse(fc["x2"].ToString());
                    y2 = int.Parse(fc["y2"].ToString());
                    var fileName = Path.GetFileName(data.FileName);
                    fileName = Lawyer_id2 + ".jpeg";
                    var big = Server.MapPath("~/contents/ProfilePictures/big/" + fileName);
                    data.Crop(y1, x1, x2, y2);
                    data.Save(big);

                }

      }

js代码

$uploadCrop = $('#upload-demo').croppie({
    viewport: {
        width: 200,
        height: 200,
        type: 'square'
    },
    boundary: {
        width: 300,
        height: 300
    },
    showZoomer: false,
    mouseWheelZoom: false
});
readFile(fl);
$(".closeModal").on("click", function () {
    $uploadCrop.croppie('result', {
        type: 'canvas',
        size: 'viewport'
    }).then(function (resp) {
        $('.upload-msg').css('display', '');
        popupResult({
            src: resp
        });

    });
    var arr = $uploadCrop.croppie('get').points;
    $("#x1").val(arr[0]);
    $("#y1").val(arr[1]);
    $("#x2").val(arr[2]);
    $("#y2").val(arr[3]);
});  

我得到隐藏输入字段中的所有点,然后将这些点传递给webimge对象进行裁剪,但问题是裁剪后的图像没有保持纵横比并且裁剪错误,浏览器端裁剪是完美的,但是当我将该点传递给服务器端进行裁剪时它不像浏览器端那样工作,我不知道解决这个问题。

裁剪已经在客户端进行,您应该只将结果发送到服务器端。 无需将裁剪点发送到服务器。

在 html 上定义SelectUpload按钮,以及一个id="main-cropper"的 div

        <div>
            <div>
                <div id="main-cropper"></div>
                <a class="button actionSelect">
                    <input type="file" id="select" value="Choose Image" accept="image/*">
                </a>
                <button class="actionUpload">Upload</button>
            </div>
        </div>

在 JS 代码上,将croppie 对象附加到潜水,将视点定义为边界,最后将请求发送到服务器以将结果存储为 blob。 在服务器端,假设会有一个带有GetImage操作的Create控制器等待请求。 testFileName.png被指定为文件名,您可以根据您的场景修改它。

        var basic = $('#main-cropper').croppie({
            viewport: { width: 200, height: 300 },
            boundary: { width: 300, height: 400 },
            showZoomer: false
        });

        function readFile(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#main-cropper').croppie('bind', {
                        url: e.target.result
                    });
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

        $('.actionSelect input').on('change', function () { readFile(this); });
        $('.actionUpload').on('click', function() {
            basic.croppie('result','blob').then(function(blob) {
                var formData = new FormData();
                formData.append('filename', 'testFileName.png');
                formData.append('blob', blob);
                var MyAppUrlSettings = {
                    MyUsefulUrl: '@Url.Action("GetImage","Create")'
                }

                var request = new XMLHttpRequest();
                request.open('POST', MyAppUrlSettings.MyUsefulUrl);
                request.send(formData);
            });
        });

在服务器上,在Create控制器中:

    [HttpPost]
    public ActionResult GetImage(string filename, HttpPostedFileBase blob)
    {
        var fullPath = "~/Images/" + filename;
        blob.SaveAs(Server.MapPath(fullPath));
        return Json("ok");
    }

生成 uuid 字符串,并将其设置为文件名并存储在数据库中也是有意义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM