【问题标题】:Can I use Javascript to Rotate and Crop a gallery of images prior to upload?我可以在上传之前使用 Javascript 来旋转和裁剪图片库吗?
【发布时间】:2019-04-25 13:59:06
【问题描述】:

我一直在尝试使用文件阅读器和croppie 允许用户在上传之前在本地编辑他们的图像(缩放/旋转/裁剪),但似乎无法使其正常工作。由于模态,我怀疑这是一个 DOM 问题,但我一生都无法弄清楚如何使它工作!


FileReader () stuff
//
//
//create elements for image, edit and delete buttons, then appending to parent div
//
//

//place the new image and button into the new div
newdiv.appendChild(newimage);
newdiv.appendChild(newedit);
newdiv.appendChild(newdelete);

//place the new div into the results div
output.appendChild(newdiv);

//add event listeners to dynamically created buttons
newdelete.addEventListener('click', delImage, false);
newedit.addEventListener('click', showModal, false);

.
.
.

function showModal(){
    var edit_id = this.getAttribute("dataset-editid");
    var image_id = document.getElementById('img-'+edit_id);
    var thisImage = image_id.src;
    $('#'+edit_id).click(function(){
        $("#cropImagePop").modal();
    });
    console.log(edit_id);
    console.log('image source: ' + image_id);
    var themodal = document.getElementById('cropImagePop');
    var theimage = document.getElementById('cropimage');
    var instance = new Croppie(theimage, {
        viewport: { width: 300, height: 300 },
        boundary: { width: 900, height: 600 },
        showZoomer: true,
        enableResize: true,
        url: thisImage
    });

}

当我单击任何给定图片的动态“编辑”按钮时,模式会打开,但照片真的很大(不在模式内),并且没有可裁剪的边界框。..

这是一个小提琴:https://jsfiddle.net/noz2eb3y/

另外 - 我绝不会与 Croppie 结婚 - 如果有人有一个很棒的(工作!)javascript 库,他们知道哪些可以缩放/旋转/裁剪图像 - 我全神贯注!

【问题讨论】:

    标签: javascript php jquery bootstrap-4 croppie


    【解决方案1】:

    回答您的问题:是的,这是可能的。

    模态窗口的问题是因为您忘记添加croppie css 引用。

    我在这个fiddle 中修复了这个问题,并且还在裁剪按钮中添加了一个功能,以更新源元素。

    请看下面的例子:

    $('#cropImageBtn').click(function(){
      instance.result('base64').then(function(base64) {
        image_id.src = base64;
        cleanModal();
      });
    });
    $('#closeImageBtn').click(function(){
        cleanModal();
    });
    function cleanModal()
    {
      instance.destroy();
      $("#cropImagePop").modal("hide");
    }
    

    如果您想上传图片,您需要采取的后续步骤。

    1. 看看jquery.ajax
    2. 将 base64 编码数据发布到您的服务器
    3. 解码base64编码数据
    4. 保存图片文件

    我还建议改进您的代码(有点混乱)。

    【讨论】:

    • 感谢您的回复!我意识到我的代码不是那么好,我会继续努力的——但你的修复和解释足以让我继续前进。对此,我真的非常感激。再次感谢!
    • 没问题。很高兴我能帮忙;)
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 2020-12-24
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多