图片转 base64 JavaScript
Image to base64 JavaScript
所以我尝试使用 HTML canvas 元素拍摄外部图像 URL 并将其转换为 Base64,以便我可以从中提取颜色。我得到了一个 base64 字符串,但是当我检查输出时它是空白的。这是我的代码:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = new Image();
var imgData
img.onload = function() {
ctx.drawImage(img, 0, 0);
};
img.src = 'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png';
imgData = c.toDataURL();
console.log(imgData)
有没有更好的方法来解决这个问题?
另一种方法是,您可以执行 HTTP 请求,然后将响应转换为 blob。 有错字,已修正
我在我的服务器上使用本地图像对此进行了测试,并且成功了。
convert = (image, callback) => {
let request = new XMLHttpRequest();
request.onload = () => {
let reader = new FileReader();
reader.onloadend = () => {
callback(reader.result);
};
reader.readAsDataURL(request.response);
};
request.open('GET', image);
request.responseType = 'blob';
request.send();
};
convert('https://via.placeholder.com/350x150', (base64) => {
console.log('Base64:', base64)
})
输出:
Base64:, data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpaqqqq3t7fFxcW+vr6xsbGjo6OcnJyLKnDGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABAElEQVRoge3SMW+DMBiE4YsxJqMJtHOTITPeOsLQnaodGImEUMZEkZhRUqn92f0MaTubtfeMh/QGHANEREREREREREREtIJJ0xbH299kp8l8FaGtLdTQ19HjofxZlJ0m1+eBKZcikd9PWtXC5DoDotRO04B9YOvFIXmXLy2jEbiqE6Df7DTleA5socLqvEFVxtJyrpZFWz/pHM2CVte0lS8g2eDe6prOyqPglhzROL+Xye4tmT4WvRcQ2/m81p+/rdguOi8Hc5L/8Qk4vhZzy08DduGt9eVQyP2qoTM1zi0/uf4hvBWf5c77e69Gf798y08L7j0RERERERERERH9P99ZpSVRivB/rgAAAABJRU5ErkJggg==
确保您已 CORS 启用。因为如果未启用 CORS,您将收到这样的错误:
Access to XMLHttpRequest at 'https://via.placeholder.com/350x150' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
所以我尝试使用 HTML canvas 元素拍摄外部图像 URL 并将其转换为 Base64,以便我可以从中提取颜色。我得到了一个 base64 字符串,但是当我检查输出时它是空白的。这是我的代码:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = new Image();
var imgData
img.onload = function() {
ctx.drawImage(img, 0, 0);
};
img.src = 'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png';
imgData = c.toDataURL();
console.log(imgData)
有没有更好的方法来解决这个问题?
另一种方法是,您可以执行 HTTP 请求,然后将响应转换为 blob。 有错字,已修正
我在我的服务器上使用本地图像对此进行了测试,并且成功了。
convert = (image, callback) => {
let request = new XMLHttpRequest();
request.onload = () => {
let reader = new FileReader();
reader.onloadend = () => {
callback(reader.result);
};
reader.readAsDataURL(request.response);
};
request.open('GET', image);
request.responseType = 'blob';
request.send();
};
convert('https://via.placeholder.com/350x150', (base64) => {
console.log('Base64:', base64)
})
输出:
Base64:, data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpaqqqq3t7fFxcW+vr6xsbGjo6OcnJyLKnDGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABAElEQVRoge3SMW+DMBiE4YsxJqMJtHOTITPeOsLQnaodGImEUMZEkZhRUqn92f0MaTubtfeMh/QGHANEREREREREREREtIJJ0xbH299kp8l8FaGtLdTQ19HjofxZlJ0m1+eBKZcikd9PWtXC5DoDotRO04B9YOvFIXmXLy2jEbiqE6Df7DTleA5socLqvEFVxtJyrpZFWz/pHM2CVte0lS8g2eDe6prOyqPglhzROL+Xye4tmT4WvRcQ2/m81p+/rdguOi8Hc5L/8Qk4vhZzy08DduGt9eVQyP2qoTM1zi0/uf4hvBWf5c77e69Gf798y08L7j0RERERERERERH9P99ZpSVRivB/rgAAAABJRU5ErkJggg==
确保您已 CORS 启用。因为如果未启用 CORS,您将收到这样的错误:
Access to XMLHttpRequest at 'https://via.placeholder.com/350x150' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.