I'm trying to get jspdf lib to show a jpeg image.
I do this:
src2 ="domain.com/images/pxPrescription/active_tick.jpg";
tickedImage = new Image();
tickedImage.src = src2;
src="domain.com/images/pxPrescription/no_tick.jpg";
no_tick = new Image();
no_tick.src = src;
doc.setFont('helvetica');
doc.setFontSize(10);
var basestarty = 190;
var basestartx = 15;
var offsetextra = 6;
var extraadd = 0.5;
var extratextoffsetx = basestartx + 10;
var extratextoffsety = basestarty + 10.5;
doc.text(extratextoffsetx, extratextoffsety, "Distance");
if(jQuery('#extraDist').is(":checked")){
doc.addImage(tickedImage, 'JPEG', basestartx, (basestarty+(offsetextra)+extraadd), 5, 5);
}else{
doc.addImage(no_tick, 'JPEG', basestartx, (basestarty+(offsetextra)+extraadd), 5, 5);
}
I also load the images into a hidden diff at page load to make sure they are available on the page.
I get Error: Supplied data is not a JPEG
Can someone help me solve this?
I'm not 100% sure since there isn't an environment to tinker with, but you should call
await no_tick.decode()
or
await tickedImage.decode()
as applicable in either of your if/else statements prior to adding the image to your pdf to ensure that you have valid image data available and then make your handler code an async function.
You're also welcome to use .then() as shown here:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decode
Related
I have 16 bit raw data as base64 string. Want to convert that into png image, based on depth 8,16,24.
// code to process 16bit buffer array
function process16bitBitmap(buffer, options = {}) {
const view = new Uint8ClampedArray(buffer);
const out = new Uint8ClampedArray(buffer.byteLength);
// set alpha channel
view.forEach((a, i) => out[(i * 4) + 3] = a);
const canvas = document.createElement('canvas');
canvas.width = options.width;
canvas.height = options.height;
const imgData = new ImageData(out, options.width, options.height);
canvas.getContext('2d').putImageData(imgData, 0, 0);
// if you want to save a png version
document.body.appendChild(canvas);}
fiddle to reproduce the issue jsfiddle
Facing hard time to correctly create a png file from 16 bit raw data.
16 bit image should looks like this.
With javaScript Vanilla:
I found this but coundn't find a way to reproduce.
With nodejs:
I found this and it works fine. Also found this but didn't work for me.
const fs = require('fs');
const path = require('path');
const base64ToPNG = (data) => {
data = data.replace(/^data:image\/png;base64,/, '');
fs.writeFile(path.resolve(__dirname, 'image.png'), data, 'base64', function (err) {
if (err) throw err;
});
}
var img = "YourImageHere...iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAYAAAAbWs+...";
base64ToPNG(img)
If your objective is doing it with vanilla I don't know if I could help you, with node that way works, so probably you'll just need to figure how to adjust the depth.
I have a web application that allows user to upload tons of images, My goal is to reduce the hosting fees AWS are going to charge me by shrink the image size users uploaded,
Currently i set the limit to 1MB using simple Js, I am not really a frontend developer, So How hard it is to create a javascript solution that automatically shrinks the image size? Is this can be done easily?
If not, then my other two options are Django and AWS UI, the backend is Django, But Django can only process the image once it reaches the backend, So if this happens, Would my aws cost increase?
Finally it's the AWS solution, I don't know how to do it yet, Maybe using something called lambda? Is it completely free? If it keeps my cost down and easy to set up then this is it?
Or would it be wise just to use simple javascript? is this can be easily implemented? like copy and paste code less than 50lines of code then it's automatically solved?
HTML5 Canvas Resize (Downscale) Image High Quality?
ImageMagick on js
JavaScript library similar to Imagemagick
Sorry if this bothers you, I can't use comment yet
Here's a working example based on my comment:
/**
* Resize a base 64 Image
* #param {String} base64 - The base64 string (must include MIME type)
* #param {Number} newWidth - The width of the image in pixels
* #param {Number} newHeight - The height of the image in pixels
*/
const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
async function Main() {
const file = document.querySelector('.file').files[0];
const newFile = await toBase64(file);
document.body.append("Before: ");
let before = document.createElement("img");
before.src = newFile;
document.body.appendChild(before);
resizeBase64Img(newFile, 200, 200).then(resized => {
let img = document.createElement("img");
img.src = resized;
document.body.appendChild(img);
});
}
function resizeBase64Img(base64, newWidth, newHeight) {
return new Promise((resolve, reject) => {
var canvas = document.createElement("canvas");
canvas.style.width = newWidth.toString() + "px";
canvas.style.height = newHeight.toString() + "px";
let context = canvas.getContext("2d");
let img = document.createElement("img");
img.src = base64;
img.onload = function() {
context.scale(newWidth / img.width, newHeight / img.height);
context.drawImage(img, 0, 0);
resolve(canvas.toDataURL());
}
});
}
document.querySelector(".file").addEventListener("input", Main);
<input class="file" type="file">
I've got a new problem within my project. I want to create a pdf file via pdfmake. I was following this tutorial and everything works fine, except the "transformation" of the '&' sign. In my 'var docDefinition' I am getting content from mysql database. There are some enterprise names with '&'-sign in it. If I call the enterprise name within php, the '&'-sign is shown correctly. But within the javascript and through convert to base64, only 'ampersand' is shown in the generated pdf.
Other signs like 'ö', 'ä' etc. are shown correctly.
Any ideas?
### Update
Here is a snippet of my code 'generatePDF.blade.php':
#section('scripts')
<script type="text/javascript">
...
// Create the PDF
var docDefinition = {
pageSize: 'A4',
pageOrientation: 'portrait',
...
content: [
{ text: 'Ausführende Firma: '},
'{{ $report->user->enterprise->enterprise_name }}',
...
],
};
And then the stuff from the tutorial:
//save the pdf into base64 strings
var pdfstr;
try {
pdfMake.createPdf(docDefinition).getDataUrl(function (result) {
pdfstr = result;
console.log('Result: ' + pdfstr);
var pdfAsArray = convertDataURIToBinary(pdfstr);
console.log('Array: ' + pdfAsArray);
PDFJS.getDocument(pdfAsArray).then(function getPdf(pdf) {
console.log('pdf var: ' + pdf.getPage(1));
//
// Fetch the first page
//
pdf.getPage(1).then(function getPdfPage(page) {
var scale = 1;
var viewport = page.getViewport(scale);
//
// Prepare canvas using PDF page dimensions
//
var canvas = $("#pdfviewer").get(0);
var context = canvas.getContext('2d');
// Check device pixel ratio and then if the browser is doubleing them
console.log('Pixelratio is', window.devicePixelRatio);
console.log('The browser is doubleing the pixels:', context.webkitBackingStorePixelRatio);
var factor = 1;
if (window.devicePixelRatio >= 2 && context.webkitBackingStorePixelRatio < 2 || context.webkitBackingStorePixelRatio == undefined) {
factor = 2;
}
//canvas.height = viewport.height*factor;
//canvas.width = viewport.width*factor;
canvas.setAttribute('width', viewport.width*factor);
canvas.setAttribute('height', viewport.height*factor);
// context.scale(factor, factor);
context.transform(devicePixelRatio,0,0,devicePixelRatio,0,0);
//
// Render PDF page into canvas context
//
var renderContext = {
canvasContext: context,
viewport: viewport,
};
page.render(renderContext);
// $('#containerPDFViewer').modal({ backdrop: 'static' }); //disable backdrop so user need to make choice
});
});
});
}
catch (e) {
throw e;
}
var BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (var i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
// array[i] = raw.codePointAt(i);
}
return array;
}
### Update 2
I figured out, that my problem has noting to do with the javascript part but with the laravel framework I am working with. I am sorry for not telling you, I just forgot to.
So when I am getting my data from mySQL in my blade view (both UTF-8), everything is fine. But if I do the same not within the pure blade but into javascript my error occurs. So I checked via dd($myvar->value); and so I can see, that the '$'-sign is not converted but the "ampersand ($amp;)". How can I solve that blade prob?
Okay, now I found an answer for my problem:
some tables in DB were in collation of utf8_general_ci, but in laravel /app/config/database.php there is 'collation' => 'utf8mb4_unicode_ci'. So the solution is to change the tables into that collation and then call the values from DB in blade via {!! $myvar->field !!} and woooosh, everything works as it should!
I have an image encoded in base64 and I need to draw it on a canvas in a synchronous fashion. Take a look at this code I had:
//convert base64 to actual Image()
var input = new Image();
input.src = dataurl;
//draw on canvas
var w = input.width, h = input.height;
canvas.width = w; canvas.height = h;
ctx.drawImage(input, 0, 0)
This used to work on Chrome. I know it is not a good practice to assuming the browser will be able to finish loading before the next instruction, but it worked. After several updates later, it finally fails.
I cannot listen to the load event because that would make it async, which is not good because that's a generator function and I need to return the canvas synchronously. The program is built around it and making it async would mean complete rewrite. That's not good.
Attempt
input.src = dataurl;
while(!input.width){ /* busy wait */ }
var w = input.width, h = input.height;
Does not work since the browser won't update it before my code finishes executing.
Right now I really can't think of any way to solve this dilemma, so any input is appreciated. (Do note that the datauri is generated from another canvas, which I do have access to.)
It's not possible to draw an image to a canvas before it is loaded, which means waiting to a load event. This means you cannot load a data URI into an DOM image synchronously, so you will need to convert your API to an asynchronous one.
The only other way would be to have a JavaScript-based image decoder that converts the data URI into a ImageData object, which would not be trivial to do and require users to load a lot more code.
There used to be a different hack involving innerHTML that would work, but it too no longer works.
Now-Broken Example:
var base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJ4AAACeCAYAAADDhbN7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyhpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDE0IDc5LjE1Njc5NywgMjAxNC8wOC8yMC0wOTo1MzowMiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RkE1MTZBNzk1MDE1MTFFNUEzODE5OTE2QTE2M0ExOTYiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RkE1MTZBNzg1MDE1MTFFNUEzODE5OTE2QTE2M0ExOTYiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIEND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ufc1+uXLFiujPpCzgjUjQ/+Xn061xq8O4V2Z4HnAYR+fY7Lp+/bwBrzWMdsrn7W51GA/IQDjF48WdO2xiLeChUBjtu2bNrs/EiwdnYLRyfBK2MwXwUMcg2rTIBrcrHcadvSeckklgDmy49On6YTyAh/KC0Y4Pu8+tPkSPzvSIBuQz/fwZAV7vYLSDHu9y63vxBSuIHq9baTgr1Wy4esFav4JHj4cADwEeQoCHAA8hwEOAhxDgoeKrUhPI/TwhS4+HEOAhwEMI8BDgIcBDCPBQpVWqebyQF5wRPR5CgIcADyHAQ4CHAA8hwEOAh1A+Kt1RtIgeDyHAQ4CHEOAhwEMI8BDgIcBDCPAQ4CEEeAjwEAI8BHgIAR4CPIQADwEeAjyEAA8BHkKAhwAPIcBDgIcQ4CHAQwjwEOAhwEMI8FAl9T8BBgB+2d0kUnLhGgAAAABJRU5ErkJggg==';
var el = document.createElement('p');
el.innerHTML = '<img src="'+base64+'" />';
var input = el.firstChild;
//draw on canvas
var canvas = document.getElementById('canvas');
var w = input.width, h = input.height;
canvas.width = w; canvas.height = h;
var ctx = canvas.getContext('2d');
ctx.drawImage(input, 0, 0);
<canvas id="canvas"></canvas>
Just use Promise to make it async.
return new Promise(resolve => {
image.onload = function () {
ctx.drawImage(input, 0, 0)
resolve('resolved');
}
});
I have a web application that generates images on the fly and then renders (segments of) them on the client using canvas - the general code looks something like this (simplified)...
var width = ...
var height = ...
var img = new Image();
img.onload = function(){ // Set onload before setting image source ;)
var canvas = document.createElement("canvas");
canvas.setAttribute('width',width);
canvas.setAttribute('height',height);
var context = canvas.getContext("2d");
context.drawImage(this,0,0,width,height,0,0,width,height);
}
img.src = ...
In firefox and chrome, this works fine. In IE9 (and sometimes IE10), this sometimes results in a blank image. As a workaround, I put in...
var width = ...
var height = ...
var img = new Image();
img.onload = function(){
var canvas = document.createElement("canvas");
canvas.setAttribute('width',width);
canvas.setAttribute('height',height);
var context = canvas.getContext("2d");
window.setTimeout(function(){
context.drawImage(img,0,0,width,height,0,0,width,height);
}, 10); // 10 millisecond delay here seems to give IE time to make sure the image is actually ready
}
img.src = ...
Obviously, putting in random delays is making me nervous - I would much rather figure out why this happens. Is there another event type / method / property on images that may be used? Could there perhaps be something in the http headers sent down with the images / the encoding / transfer method that is causing this?
Since drawing images on the canvas seems to be something that trips up new users a lot, searches mostly yield instances where the developer set the source before the onload method. The closest other question / reply I was able to find was this : https://stackoverflow.com/a/15186350/470062
Edit:
I have updated the examples to include width and height definitions at the start
Edit 29/Apr/2013:
The 10 millisecond delay is not always sufficient - I have changed the code to check the canvas pixels to determine if anything has been drawn - This feels like such an ugly hack though - I would love to know if there is a better way :
... snip - this is at the end of the image load callback..
if((navigator.appVersion.indexOf("MSIE")) >= 0){
function checkAndRedraw(){
context.clearRect(0,0,width,height);
context.drawImage(img, 0, 0, width, height);
var imageData = context.getImageData(0, 0, width, height),
data = imageData.data,
len = data.length;
for (var i = 0; i < len; i += 4){
if(data[i] || data[i+1] || data[i+2]){
scheduleRedraw = null; // prevent memory leak
return; // A pixel contained some non empty value so something was drawn - done.
}
}
scheduleRedraw();
}
var redrawHackTimeout;
function scheduleRedraw(){
window.clearTimeout(redrawHackTimeout);
redrawHackTimeout = window.setTimeout(checkAndRedraw, 500);
}
scheduleRedraw();
}
//END IE9 Hack
I have the same issue in IE9, what I had to do is this too:
theImage = new Image();
theImage.onload = function() {
that = this;
setTimeout(function() {
that.onload2();
}, 100);
};
theImage.onload2 = function(){
#your old load function code
}
theImage.src = 'http://....';
I don't understand why ie9 triggers load image function when it's not actually loaded or not "usable" yet in a canvas. it makes me pull my hair out.
var img = new Image();
img.src = ...
img.onload = function(){ // Set onload before after setting image source ;)
var canvas = document.createElement("canvas");
canvas.setAttribute('width',width);
canvas.setAttribute('height',height);
var context = canvas.getContext("2d");
context.drawImage(this,0,0,width,height,0,0,width,height);
}
You must set the src, then wait for the image to load.