是否可以从 url 下载图像并使用 jquery 物理调整图像大小,而不使用任何服务器端脚本? 我进行了大量搜索但没有找到任何解决方案。
最佳答案
如果您需要下载,可以使用这样的 Canvas
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);
将图像放入 Canvas 并根据需要调整图像大小,然后将 Canvas 下载为图像
你可以尝试从这个jsFiddle
关于javascript - 我们可以使用 jquery 调整图像大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38519171/