我们可以使用jquery调整图像大小吗

Can we resize image using jquery?

本文关键字:图像 调整 可以使 jquery 我们      更新时间:2023-09-26

是否可以在不使用任何服务器端脚本的情况下从url下载图像并使用jquery物理调整图像大小?我做了很多搜索,但没有找到任何解决方案。

如果需要下载,可以使用类似的画布

html

<canvas id="c" width="200" height="150"></canvas>
<a id="download">Download as image</a>

javascript

// Grab the Canvas and Drawing Context
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
// Create an image element
var img = document.createElement('IMG');
// Specify the src to load the image
img.setAttribute('crossOrigin', 'anonymous');
img.src = "http://i.imgur.com/adB2oag.png";
// When the image is loaded, draw it
img.onload = function () {
    ctx.drawImage(img, 0, 0, img.width,    img.height, 0, 0, canvas.width, canvas.height );
}
function downloadCanvas(link, canvasId, filename) {
    link.href = document.getElementById(canvasId).toDataURL();
    link.download = filename;
}
document.getElementById('download').addEventListener('click', function() {
    downloadCanvas(this, 'c', 'test.png');
}, false);

将图像放在画布上,根据您的喜好调整图像大小,并将画布下载为图像

您可以尝试从这个jsFiddle