如何在不使用插件的情况下在html中裁剪上传的图像?

时间:2017-02-09 08:51:39

标签: javascript jquery html5

需要裁剪未使用任何裁剪插件上传的图像

1 个答案:

答案 0 :(得分:0)

你可以使用canvas:

in html

<canvas id="corpImg" width="578" height="300"></canvas>

在js

<script>
      var canvas = document.getElementById('corpImg');
      var context = canvas.getContext('2d');
      var imageObj = new Image();

      imageObj.onload = function() {
        // draw cropped image
        var sourceX = 150;
        var sourceY = 0;
        var sourceWidth = 150;
        var sourceHeight = 150;
        var destWidth = sourceWidth;
        var destHeight = sourceHeight;
        var destX = canvas.width / 2 - destWidth / 2;
        var destY = canvas.height / 2 - destHeight / 2;

        context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
      };
      imageObj.src = 'image url';
    </script>