In the current project I am working with sails js as back end and angular js as front end. I have implemented a cropping module. By current problem is that, the output of the cropping module is base64 data. For doing some manipulations I need to convert this base 64 data to an image file. I received the base64 data at server side. Now need to convert this data to a file in sails js server side. I used a code for that, but not creating the image file.
My sample base 64 data is below
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xuy9B6wkeX4e9lVVd1Xn3P1yDpNz2J3N+e72juFODHfH......................."
my code for converting this base64 to image file is
var image = req.body.image.replace(/^data:image\/jpeg;base64,/, "");
var filePath = 'imagecontents/';
fileName = filePath +'abc.jpg';
mkdirp(path.join(__dirname, '../../..' + filePath), function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
require("fs").writeFile(path.join(__dirname, '../../..' + fileName), image, 'base64', function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
});
Plz help to rectify is there is any error in in this code
You can write the file using the below code:
var base64Data = data.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
console.log(err);
});
Related
Hi everybody I am facing an issue where when I create and save pdf contents to a pdf file it always opens up blank here is my code snippet
i am using express.js
fs.writeFile(
"response.pdf",
pdfData.data,
{ encoding: "utf-8" },
function (err) {
if (err) throw err;
console.log("File is created successfully.");
}
); ```
here is an example of the pdf data I'm trying to add to the pdf
```%PDF-1.5
%����
3 0 obj
<</ColorSpace/DeviceRGB/Subtype/Image/Height 224/Filter/F...(more characters)````
please advice
Hi i'm trying to download a pdf with nodejs (UNIREST) but i'm a bit stocked.
var url_pdf = "http://myurl.com/ex.pdf"
unirest.get(url_pdf)
.as.binary(function (response) {
fs.writeFile("./pdf/test.pdf", response.body, 'binary', function (err){
if (err)
console.log(err);
});
})
When wrting the file, its miss formatted (just a blank pdf file) size is ok.
node js resize and save image from remote server.
Hi,
How can I resize image without to save locally and then to save it.
when I run bellow code, I get error: "Error: Input buffer contains unsupported image format"
code:
var fs = require('fs');
var request = require('request');
var sharp = require('sharp');
function getImage()
{
request('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png', function (err, res, body) {
var binary = Buffer.from(body.toString(), 'base64');
sharp(binary).resize(198, 110).toFile('test.jpg', (err, info ) =>
{
console.log('err: ', err);
console.log('info: ', info);
});
});
}
I found resolving for my question:
var resizer = sharp().resize(198, 110).toFile('test.jpg', (err, info) => {
console.log('err: ', err);
console.log('info: ', info);
});
request('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png').pipe(resizer);
If you can make sure that https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png returns the image, you can try to pipe the response directly to sharp rather than converting response to base64 format & processing.
var resize = sharp(binary).resize(198, 110);
resize.on('error', function() {
//handle error
});
resize.on('finish', function() {
// done
});
request('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
.pipe(process);
Update:
Removed .toFile('test.jpg'); part
I am in a project,that convert svg to png in the server side----node.js server.So I use imagemagick and the the server install imagemagick 6.7.7 also install the libsvg2-bin.Everything works well but the text encoding problem.The server use UTF-8 but failed to convert.
require("fs").writeFile(target_path_svg, svg, [], function(err) {
if (err) {
res.send(err);
return;
} else {
im.convert([target_path_svg, 'png:-'], function(err, stdout) {
if (err) {
throw err
res.send("error");
}
fs.writeFileSync(target_path, stdout, 'binary');
res.send("ok");
return;
});
return;
}
});
anyone help?
As a workaround, pass target_path as the second parameter in the array in the call to im.convert() (instead of png:-). It will directly write the file.
I send from client to server a PNG as a base64 string. I decode it back and save to server. But the file is not readable as a png. Do I have to add specific headers? What am I doing wrong? Here's my code:
var base = decodedBase64;
fs.writeFile("/tmp/test.png", base, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
fs.writeFile("/tmp/test.png", base, "binary", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
The default encoding is utf-8. You don't want to save it as text, you want to safe it as binary data so pass in the binary encoding.