javascript – Jquery插件裁剪裁剪图像错误

前端之家收集整理的这篇文章主要介绍了javascript – Jquery插件裁剪裁剪图像错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的网站上使用 Jquery Croppie Plugin为我的用户裁剪图像,但是我有这个问题,我写的代码不会在 Croppie Site显示为例

这是我的代码

HTML代码

<input type="file" id="upload" value="Choose a file">
<button class="upload-result">Result</button>
<div class="upload-msg">
   Upload a file to start cropping
</div>
<div id="upload-demo"></div>

JS代码

$uploadCrop = $('#upload-demo').croppie({
   viewport: {
      width: 200,height: 200,type: 'circle'
   },boundary: {
      width: 300,height: 300
   }
});

注意:我已经链接我的网站与jquery,croppie.js和croppie.css

解决方法

尝试它,它适用于我.
我用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);
    }
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<Meta charset="utf-8">
<title>Test</title>
<link href="croppie.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="croppie.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
    var $uploadCrop;

    function readFile(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();          
            reader.onload = function (e) {
                $uploadCrop.croppie('bind',{
                    url: e.target.result
                });
                $('.upload-demo').addClass('ready');
            }           
            reader.readAsDataURL(input.files[0]);
        }
    }

    $uploadCrop = $('#upload-demo').croppie({
        viewport: {
            width: 200,type: 'circle'
        },boundary: {
            width: 300,height: 300
        }
    });

    $('#upload').on('change',function () { readFile(this); });
    $('.upload-result').on('click',function (ev) {
        $uploadCrop.croppie('result',{
            type: 'canvas',size: 'original'
        }).then(function (resp) {
            $('#imagebase64').val(resp);
            $('#form').submit();
        });
    });

});
</script>
</head>
<body>
<form action="test-image.PHP" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<div id="upload-demo"></div>
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result">Send</a>
</form>
</body>
</html>

猜你在找的jQuery相关文章