在客户端裁剪和调整图像大小

时间:2010-07-15 19:23:51

标签: image-processing crop

是否可以在客户端PC上使用客户端选择的图像而无需将图像上传到服务器。

如果是,那么哪种网络编程语言可以做到这一点?

4 个答案:

答案 0 :(得分:7)

您可以使用HTML5 Canvas,无需使用插件等。

加载图像,更改画布大小和绘制图像。也可以将结果作为dataUrl提取。

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body { margin: 0px; padding: 0px; }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      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 = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
    </script>
  </body>
</html>

所有功劳归于:

http://www.html5canvastutorials.com/tutorials/html5-canvas-image-crop/

答案 1 :(得分:1)

这只能通过FlashSilverlight或自定义Plugin/ActiveX来完成,具体取决于目标浏览器。

答案 2 :(得分:1)

这也可以使用jQuery库,如jQuery,MooTools,Prototype和script.aculo.us:

http://www.bitrepository.com/image-cropping-with-jquery-mootools-prototype-scriptaculous.html

答案 3 :(得分:0)

如果您正在通过javascript查找图片裁剪器,请查看:https://github.com/supnate/icropper。它提供了用于裁剪的用户界面,但没有真正裁剪图像。

相关问题