cropper图片裁剪上传

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Cropper</title>
  <link rel="stylesheet" href="./cropper/cropper.css">
  <style>
    .container {
      max-width: 960px;
    }

    img {
      max-width: 100%;
    }
  </style>
</head>
<body>
<div style="width: 100%;overflow: hidden;">
  <div class="container">
    <div>
      <img src="" id="image">
    </div>
    </div>
  </div>
</div>
  <div id="dpage" class="docs-buttons">
    <a href="javascript:void(0);">
      <input type="button" name="file" class="button" value="上传">
      <input id="inputImage" type="file" accept="image/*" multiple  />
    </a>
    <a href="javascript:void(0);" class="qx"><button id="clipBtn" data-method="getCroppedCanvas">截取图片</button></a>
  </div>
  <!-- Scripts -->
  <script src="./jquery/jquery-1.12.4.min.js"></script>
  <script src="./cropper/cropper.js"></script>
  <script>
    $(function () {

      'use strict';

      var console = window.console || { log: function () {} };
      var $image = $('#image');

      var options = {
        dragMode:'move'
      };





      // Cropper
      $image.cropper(options);




      // Methods
      $('.docs-buttons').on('click', '[data-method]', function () {
        var $this = $(this);
        var data = $this.data();
        var $target;
        var result;

        if ($this.prop('disabled') || $this.hasClass('disabled')) {
          return;
        }

        if ($image.data('cropper') && data.method) {
          data = $.extend({}, data); // Clone a new one

          if (typeof data.target !== 'undefined') {
            $target = $(data.target);

            if (typeof data.option === 'undefined') {
              try {
                data.option = JSON.parse($target.val());
              } catch (e) {
                console.log(e.message);
              }
            }
          }

          if (data.method === 'rotate') {
            $image.cropper('clear');
          }

          result = $image.cropper(data.method, data.option, data.secondOption);

          if (data.method === 'rotate') {
            $image.cropper('crop');
          }

          switch (data.method) {
            case 'scaleX':
            case 'scaleY':
              $(this).data('option', -data.option);
              break;

            case 'getCroppedCanvas':
              if (result) {
                var base64url = result.toDataURL('image/jpeg');
                $.post('/api/user/uploadImage',{img:base64url},function(data){
                    if(data.status){
                      console.log(data.info);
                    }
                    console.log(data.info);
                },'json');
              }

              break;
          }

          if ($.isPlainObject(result) && $target) {
            try {
              $target.val(JSON.stringify(result));
            } catch (e) {
              console.log(e.message);
            }
          }

        }
      });


      // Keyboard
      $(document.body).on('keydown', function (e) {

        if (!$image.data('cropper') || this.scrollTop > 300) {
          return;
        }

        switch (e.which) {
          case 37:
            e.preventDefault();
            $image.cropper('move', -1, 0);
            break;

          case 38:
            e.preventDefault();
            $image.cropper('move', 0, -1);
            break;

          case 39:
            e.preventDefault();
            $image.cropper('move', 1, 0);
            break;

          case 40:
            e.preventDefault();
            $image.cropper('move', 0, 1);
            break;
        }

      });


      // Import image
      var $inputImage = $('#inputImage');
      var URL = window.URL || window.webkitURL;
      var blobURL;

      if (URL) {
        $inputImage.change(function () {
          var files = this.files;
          var file;

          if (!$image.data('cropper')) {
            return;
          }

          if (files && files.length) {
            file = files[0];

            if (/^image\/\w+$/.test(file.type)) {
              blobURL = URL.createObjectURL(file);
              $image.one('built.cropper', function () {

                // Revoke when load complete
                URL.revokeObjectURL(blobURL);
              }).cropper('reset').cropper('replace', blobURL);
              $inputImage.val('');
            } else {
              window.alert('文件类型错误');
            }
          }
        });
      } else {
        $inputImage.prop('disabled', true).parent().addClass('disabled');
      }

    });
  </script>
</body>
</html>

 

转载于:https://my.oschina.net/u/1029242/blog/753954

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值