在同一页面上的php Handle AJAX请求中上传后如何显示图像

时间:2019-01-30 14:16:07

标签: javascript php jquery html ajax

我正在尝试创建图像裁剪工具,它应该上传图片,裁剪并在页面上将其作为image.png而不是作为基础64(data:image)打印(据我所知)通过HTML和javascript。所以我试图通过PHP做到这一点。我正在上传和裁剪图像,但是无法显示上载的图像,当我裁剪图像时会创建两个.png文件。其中之一是正常图像,第二个是0字节错误图像(每当页面刷新时,它都会创建此0字节错误图像)。

我该如何解决?

<html lang="en">
<head>
  <title>PHP and jQuery - Crop Image and Upload using Croppie plugin</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.js"></script>

  <meta name="csrf-token" content="{{ csrf_token() }}">
</head>

<body>
<div class="container">
  <div class="card" style="max-height: 500px;">
    <div class="card-header bg-info">PHP and jQuery - Crop Image and Upload using Croppie plugin</div>
    <div class="card-body">

      <div class="row">
        <div class="col-md-4 text-center">
        <div id="upload-demo"></div>
        </div>
        <div class="col-md-4" style="padding:5%;">
        <strong>Select image to crop:</strong>
        <input type="file" id="image">

        <button class="btn btn-success btn-block btn-upload-image" style="margin-top:2%">Upload Image</button>
        </div>

        <div class="col-md-4">
        <div id="preview-crop-image" style="background:#9d9d9d;width:300px;padding:50px 50px;height:300px;"></div>
        </div>
      </div>

    </div>
  </div>
</div>


<script type="text/javascript">


var resize = $('#upload-demo').croppie({
    enableExif: true,
    enableOrientation: true,    
    viewport: { // Default { width: 100, height: 100, type: 'square' } 
        width: 200,
        height: 200,
        type: 'circle' //square
    },
    boundary: {
        width: 300,
        height: 300
    }
});


$('#image').on('change', function () { 
  var reader = new FileReader();
    reader.onload = function (e) {
      resize.croppie('bind',{
        url: e.target.result
      }).then(function(){
        console.log('jQuery bind complete');
      });
    }
    reader.readAsDataURL(this.files[0]);
});


$('.btn-upload-image').on('click', function () {
  resize.croppie('result', {
    type: 'canvas',
    size: 'viewport'
  }).then(function (img) {
    $.ajax({
      type: "POST",
      data: {"image":img},
      success: function (data) {
        html = '<img src="' + img + '" />';
        $("#preview-crop-image").html(html);
      }
    });
  });
});


</script>
<?php
{
if(isset($_POST['image'])){

$image = $_POST['image'];
list($type, $image) = explode(';',$image);
list(, $image) = explode(',',$image);

$image = base64_decode($image);
$image_name = time().'.png';
file_put_contents('uploads/'.$image_name, $image);

echo 'successfully uploaded';
            display_image();
}
 function display_image(){
  ?>
  
    <img src="<?php echo "uploads/". $image_name; ?>" width="70" height="70" >

  <?php
  }
  }
?>

</body>
</html>

0 个答案:

没有答案