How to convert png base64 to a jpg base64 nodejs - node.js

So, I am currently using an API using curl that expects a base64 string as a body. This base64 has to be in the JPG format, though. The images that I am uploading are online so the most ideal solution would look something like this:
var url = "www.someimg.nl/pic.png";
var base64png = Base64.Encode(url);
var base64jpg = Base64.Convert(base64png, "JPG");
//Do my curl here
All the solutions that I see usually save the file while they are converting it. I do not want to do this as I do not want a local copy of every image handled by my program. Is there any way to do what I am describing?

Related

I want to convert an image into base64 encoding using buffer. As buffer is deprecated and it doesnt work in buffer.from ,bufffer.alloc()

var imgStr= new Buffer.from(img).toString('base64');
I have been using this, but it is not working. This is the code to convert the screenshot i have taken into base64 encoding.
You don't need to use new
var imgStr = Buffer.from(imgBuffer).toString('base64');

Binary data from mongodb gets corrupted

When I upload a photo it converts to base64 and then when I send to mongodb using Mongoose it saves as Binary. But when I call the same picture back from the database it returns as Buffer array. After converting to base64 it returns as a base64 string but completely different from the original base64. The new base64 is unable to be rendered in browser because it has been corrupted.
Below are pictures of the different strings
This is the initial base64
This is the Buffer array
This is the corrupted base64 after converting from the buffer array using Buffer.from(avatar).toString('base64').
Please note that I appended to it "data:image/png;base64," before rendering in the browser and it still did not render.
Please can someone tell me what I am doing wrong?
the best solution is convert to png or jpg file and upload only path and save image to folder.
Here is how I solved it.
I converted from binary to utf8 instead of to base64.
There is a huge difference bewteen
Buffer.from(binary_data, 'binary').toString('utf8')
and
Buffer.from(binary_data, 'binary').toString('base64')

Sanitize Html with base64 image (and convert it to an image)

I have a WebApp with a TinyMCE Html Editor that allows users to input some html from a web page. Images can be pasted and are encoded as base64.
Before saving the user input to DB I use OWASP java-html-sanitizer to discard potential dangerous code (javascript,...).
Some characters in the base64 string of the image are escaped and when I try to get the image back (using apache commons Base64) I'm not able to get a valid image.
Here my code for decoding the image:
byte[] b;
String s = html;
b = s.getBytes(Utility.UTF8);
b = org.apache.commons.codec.binary.Base64.decodeBase64(b);
For the HtmlSanitizer I have done nothing special, just followed the Ebay Policy Example allowing base64 images as suggested here.
Ah, as suggested here I need "to HTML decode before base64 decoding".
I have tried with apache common StringEscapeUtils:
org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(html);
and it's working. Great.
allowedSchemes: [ 'data'] or allowedSchemesByTag: { img: [ 'data' ]} can be used to allow img tag to accept/allow base64.

Nodejs fs.readfile vs new buffer binary

I have a situation where I receive a base64 encoded image, decode it, then want to use it in some analysis activity.
I can use Buffer to go from base64 to binary but i seem to be unable to use that output as expected (as an image).
The solution now is to convert to binary, write it to a file, then read that file again. The FS output can be used as an image but this approach seems a bit inefficient and additional steps as i would expect the buffer output to also be a usable image as it has the same data?
my question, is how does the fs.readfile output differ from the buffer output? And is there a way i can use the buffer output as i would the fs output?
Buffer from a base64 string:
var bin = new Buffer(base64String, 'base64').toString('binary');
Read a file
var bin = fs.readFileSync('image.jpg');
Many thanks

Creating an image from a nsIBinaryInputStream

I create a binary input stream using some js trickery which contains compressed image data like jpeg or gif. I want to decode and display this data either using imgITools::decodeImageData or some other way but couldn't find a way yet. Where should I start?
The easiest way is to read the image data into a string, base64 encode the string, then turn that into a data: URL and set that as the src of your image. Unfortunately stackoverflow won't let me create a live data: link, but it would look like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAMklEQVRIx2NgGAWjYBQMFXAFDRMrR5GF/6H4CglyoxaOWjhq4aiFg7hoGwWjYBTQDgAAy8VWOfRR6fkAAAAASUVORK5CYII="

Resources