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.
Related
I am using multer to upload images to the server . And i am using sharp to resize the images and then send to the client. But what happens is once a user uploads the image, multer uploads it to the server and sharp resizes it even but if the same user uploads again multer uploads the new file but sharp doesnt resize the new uploaded image rather it sends the original resized image.
app.post('/upload',(req,res)=>{
upload(req,res,(err)=>{
if(err){
res.render('picupload',{
msg: err
});
}else{
if(req.file == undefined){
res.render('picupload',{
msg: 'Error: No File selected!'
});
} else{
console.log(req.file);
sharp(`./public/uploads/${req.file.filename}`)
.resize(40,40)
.toFile(`./public/uploads/resize/${req.file.filename}`,(err,info)=>{
if(err) throw err;
console.log(info);
});
res.render(pathjoin13,{
file: `uploads/${req.file.filename}` ,
file1: `uploads/resize/${req.file.filename}`
});
}
}
});
});
The res.render() call depends on the output of the asynchronous toFile() call so will need to move to its callback, something like:
.toFile(`./public/uploads/resize/${req.file.filename}`,(err,info)=>{
if (err) throw err;
console.log(info);
res.render(pathjoin13,{
file: `uploads/${req.file.filename}` ,
file1: `uploads/resize/${req.file.filename}`
});
});
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.
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);
});
I'm trying to insert a binary attachment to CouchDB with nano. I have a JPG in data returned by http.request.
I save it with nano as follows
db.attachment.insert( id, 'content', self._data, contentType, {rev: rev}, function(err, body) {
callback();
});
but when I try view it though a web browser the image is broken.
The file is full of UTF-8 escape characters which is visible when I pull it with CURL:
$ curl http://127.0.0.1:5984/web-crawler/doc-test.jpg/content
"ÿØÿà\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000ÿâ\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007Î\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000öÖ\u0000\u0001\u0000\u0000\u0000\u0000Ó-HP \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\
Content is not corrupted because if I save it to a file I can see the image.
var fs = require('fs');
fs.writeFile('logo.jpg', data, 'binary', function(err){
if (err) throw err
console.log('File saved.')
});
What is the right way to do it?
Ok, this question can be close. The answer to my problem is Buffer:
db.attachment.insert( id, 'content', new Buffer(self._data, "binary"), contentType, {rev: rev}, function(err, body) {
callback();
});
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.