Javascript: Convert image to base64 not complete? - javascript

I want to convert image to base64 but the value is not complete?
<img crossorigin="anonymous" id="imageid" src="{{helper.photo_1}}" />
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL
}
var base64 = getBase64Image(document.getElementById("imageid"));
console.log(base64); // the value is not complete.
When I use the base64 to show image, this is what it looks like:

Related

How to get dataURL of Salesforce ContentVersion in LWC?

I am trying to get dataURL of Salesforce ContentVerison using canvas.toDataURL in LWC method but I am getting the following CORS policy error:
"error image"
LWC html code:
<template>
<div id="full-screen-image" class="full-screen-image">
<img data-id="prev-image" src="/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Png&versionId=06821000001Gsm8AAC&operationContext=CHATTER&contentId=05T21000004x5ga" class="testImage">
<canvas id="myCanvas" class="slds-hide" data-id="prev-canvas" style="border:1px solid #d3d3d3;"></canvas>
</div>
</template>
JS code
handleRotateRight() {
var image = this.template.querySelector('[data-id="prev-image"]');
var canvas = this.template.querySelector('[data-id="prev-canvas"]');
var context = canvas.getContext('2d');
let img = new Image();
img.crossOrigin = "anonymous";
img.src = image.src;
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
context.translate(canvas.width / 2,canvas.height / 2);
context.rotate(Math.PI/2);
context.drawImage(img, -img.width / 2, -img.height / 2);
//convert to png image as dataURL
var dataURL = canvas.toDataURL("image/png");
console.log('dataURL-->', dataURL);
//convert that as base64 encoding
var convertedDataURI = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
console.log('convertedDataURI-->', convertedDataURI);
}
}

Javascript Canvas get data from image and display it

I've no idea why this doesn't work. The src attribute of the image is changed with the new data, but the image is not actually visible. I tried with several different images. It's just javascript in a browser and Jquery is required.
<body>
<img src="" id="test">
</body>
<script>
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
let img = new Image();
img.onload = getIt
img.src = 'download.png';
function getIt() {
canvas.width = this.width;
canvas.height = this.height;
const imageData = ctx.getImageData(0, 0, this.width, this.height);
ctx.drawImage(this, canvas.width , canvas.height);
ctx.putImageData(imageData, canvas.width, canvas.height);
$('#test').get(0).src = canvas.toDataURL("image/png");
}
</script>
I tried with several different images, all png. At the moment this is the entire code for the project so I don't foresee anything that could be interfering with it.
The result is this but the image cannot be seen:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAeCAYAAAAhDE4sAAAAMklEQVRIS+3SsQ0A....etc" id="test">
EDIT: It seems like it might be getting the data from the image incorrectly but I've no idea why...I mean why would it get the data but be incomplete or incorrect?
Please try this:
(You don't need to get or put the image data)
Also you were doing this:ctx.putImageData(imageData, canvas.width, canvas.height);. This means that you are drawing the image totally outside the canvas. Do this instead: ctx.putImageData(imageData, 0,0);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
let img = new Image();
img.onload = getIt
img.src = "download.png";
function getIt() {
canvas.width = this.width;
canvas.height = this.height;
//draw the image onto the canvas
ctx.drawImage(this, 0,0);
//use the data uri
test.src = canvas.toDataURL("image/png");
}
<img src="" id="test">

Convert image on my page to base64

I have an image loaded on my page that I need to convert to base64. I'm currently using the following:
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
var img = document.getElementById("additem_img");
ctx.drawImage(img, 10, 10);
console.log(c.toDataURL());
Which I found in another StackOverflow question. It seems to create a base64 string
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAEYklEQVR4Xu3UAQkAAAwCwdm/9HI83BLIOdw5AgQIRAQWySkmAQIEzmB5AgIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBg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QIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlACBB1YxAJfjJb2jAAAAAElFTkSuQmCC
But when I try to decode this using every online decoder I can, I get nothing. The "additem_img" element is an image who's source is an image from a remote webpage. I want to create a local copy using canvas and base64.
try this:
<img id="additem_img" src="image.png">
<script>
var c = document.createElement("canvas");
var ctx = c.getContext("2d");
var img = document.getElementById("additem_img");
var w = img.width;
var h = img.height;
c.width = w;
c.height = h;
ctx.drawImage(img, 0, 0);
console.log(c.toDataURL());
</script>

Base64 data from absolute image path

I am trying to get base64 data from image url but it always return "data:"
Here's the code :
function getBase64Image(imgurl) {
var canvas = document.createElement("canvas");
var img = new Image();
img.src = imgurl;
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
console.log(dataURL);
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
Use img.onload to execute the code after your image has loaded. Use a callback function to report back the dataURL.
function getBase64Image(imgurl, callback) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
if (typeof callback === 'function') {
callback(dataUrl);
}
};
img.src = imgurl;
}

HTML5 canvas toDataURL sometimes returns "data:," instead of the image

I have created a canvas with document.createElement("canvas"):
var canvas = document.createElement("canvas");
canvas.width = width; // width = 4000 or more
canvas.height = height; // height = 5000 or more
Then I use canvas.toDataURL() to get its base64 string:
var str = canvas.toDataURL();
but the 'str' sometimes returns as "data:,", with no image data in it. Just those six chars.
Sometimes it returns the correct string like "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAEGgAABPsCAYAAABg/aH3AAAgAElEQVR4XuzcQREAAAjDM..."
I the canvas.width is too large ....
You should try to create a 2D context explicitly and then draw in it an empty shape, something like:
var canvas = document.createElement("canvas");
canvas.width = 4000;
canvas.height = 5000;
var ctx=canvas.getContext('2d');
ctx.fillRect(0,0,0,0);
canvas.toDataURL();
Edit: It seems that your canvas contains too much data to be exported as a dataUri:
var canvas = document.createElement("canvas");
canvas.width = 400000000;
canvas.height = 500000000;
var ctx=canvas.getContext('2d');
ctx.fillRect(0,0,0,0);
canvas.toDataURL();
// outputs : "data:,"

Categories

Resources