如何使用Javascript图像裁剪器“ Croppie”进行保存

Jam*_*mes 5 javascript

Croppie使用DIV容器而不是CANVAS进行裁剪。我试图找出如何将由此DIV生成的裁剪图像保存到服务器上的目录,例如通过SAVE按钮。

这是我的HTML代码...

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    <!DOCTYPE html>
    <html>
    <head>
        <title>Demo Croppie</title>
        <link rel="Stylesheet" type="text/css" href="css/croppie.css" />
        <link rel="Stylesheet" type="text/css" href="css/sweetalert.css" />

    </head>
    <body>

        <div id="testCanvas"></div>
        <div class="actions" style="margin-left: auto; margin-right: auto; width:250px;">
            <button class="vanilla-result">Result</button>
            <button class="vanilla-rotate" data-deg="-90">Rotate Left</button>
            <button class="vanilla-rotate" data-deg="90">Rotate Right</button>
        </div>

    <p><button onclick="function();">Save</button></p>


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="js/croppie.js"></script>
    <script src="js/demo.js"></script>
    <script src="js/sweetalert.min.js"></script>
        <script>
          Demo.init();
        </script>

    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

这是我的JavaScript配置...

function demoVanilla() {
    var vanilla = new Croppie(document.getElementById('testCanvas'), {
        viewport: { width: 300, height: 300, type: 'square' },
        boundary: { width: 350, height: 350 },
        enableOrientation: true
    });
    vanilla.bind({
        url: 'imgs/demo-1.jpg',
        orientation: 1
    });

    document.querySelector('.vanilla-result').addEventListener('click', function (ev) {
        vanilla.result('canvas').then(function (src) {
            popupResult({
                src: src
            });
        });
    });

    $('.vanilla-rotate').on('click', function(ev) {
        vanilla.rotate(parseInt($(this).data('deg')));
    });
}
Run Code Online (Sandbox Code Playgroud)

其余的默认情况下不会更改,而Croppie会在https://github.com/Foliotek/Croppie上进行更改,例如demo.js等。

Jam*_*mes 2

知道了!

感谢 Croppie 的开发者之一“thedustinsmith”和“TWFPSP”引导我找到正确的永恒资源及其提供的信息。

$( document ).ready(function() {
    vanillaRotate = document.querySelector('.vanilla-rotate');
    var $uploadCrop = $('#upload-demo');
        $uploadCrop.croppie({
            viewport: {
                width: 250,
                height: 250,
                type: 'square'
            },
            boundary: {
                width: 300,
                height: 300
            }
        });
        $uploadCrop.croppie('bind', 'imgs/cat.jpg');
        vanillaRotate.addEventListener('click', function() {
        vanilla.rotate(parseInt($(this).data('deg')));
        });
    $('.upload-result').on('click', function (ev) {
        $uploadCrop.croppie('result', {
            type: 'canvas',
            size: 'original'
        }).then(function (resp) {
            $('#imagebase64').val(resp);
            $('#form').submit();
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

HTML 正文...

<form action="test-image.php" id="form" method="post">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
<button class="vanilla-rotate" data-deg="-90">Rotate</button>
Run Code Online (Sandbox Code Playgroud)

测试图像.php ...

<?php
    if(isset($_POST['imagebase64'])){
        $data = $_POST['imagebase64'];

        list($type, $data) = explode(';', $data);
        list(, $data)      = explode(',', $data);
        $data = base64_decode($data);

        file_put_contents('image64.png', $data);
    }
?>
Run Code Online (Sandbox Code Playgroud)

现在旋转功能不起作用,我正在尝试修复它。似乎不知道如何在此设置中包含方向配置,这与演示文件中所示的 demoUpload 不同。但至少保存到服务器的效果很好。