【发布时间】:2010-07-15 19:23:51
【问题描述】:
是否可以在客户端 PC 上使用客户端选择的图像而无需将图像上传到服务器。
如果是,哪种网络编程语言可以做到这一点?
【问题讨论】:
标签: image-processing crop
是否可以在客户端 PC 上使用客户端选择的图像而无需将图像上传到服务器。
如果是,哪种网络编程语言可以做到这一点?
【问题讨论】:
标签: image-processing crop
你可以使用 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/
【讨论】:
这只能通过Flash、Silverlight 或自定义Plugin/ActiveX 来完成,具体取决于目标浏览器。
【讨论】:
这也可以使用 jQuery、MooTools、Prototype 和 script.aculo.us 等 javascript 库来完成:
http://www.bitrepository.com/image-cropping-with-jquery-mootools-prototype-scriptaculous.html
【讨论】:
如果您正在寻找 javascript 的图像裁剪器,请查看:https://github.com/supnate/icropper。它提供了裁剪的用户界面,但不是真正裁剪图像。
【讨论】: