最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - save base64 image from javascript - Stack Overflow

programmeradmin4浏览0评论

I have a variable in JavaScript name as myImage which holds the base64 encoded string of an image like data:image/png;base64,iVBORw0KGgoAAAANSUhE...... Now I want to save this image on folder at server side. Please Help

Thanks in Advance

I have a variable in JavaScript name as myImage which holds the base64 encoded string of an image like data:image/png;base64,iVBORw0KGgoAAAANSUhE...... Now I want to save this image on folder at server side. Please Help

Thanks in Advance

Share Improve this question asked May 17, 2013 at 4:22 Ghazanfar IdreesGhazanfar Idrees 311 silver badge6 bronze badges 1
  • 2 Possible duplicate from stackoverflow./questions/11511511/… – user1193035 Commented May 17, 2013 at 4:32
Add a ment  | 

2 Answers 2

Reset to default 4

You need to work on asking questions a little better, but I will try to give you a basic explanation.

Step one: Pass the base64 string to the server.

Step two: Strip the first parts and last parts.

Step three: base64_decode the string.

Step four: file_put_contents the result of that.

Of course this is very basic, but there are already answers out there for this, and I was able to find even more with a google search.

The below code does the trick:

Here you are just writing the data to a file and saving it in the write format

var data = image.replace(/^data:image\/\w+;base64,/, "");
    var buf = new Buffer(data, 'base64');
    fs.writeFile('image.png', buf,function(err, result) {
      if(err){console.log('error', err);}
    });

The resulting image will be stored as image.png at the current directory

发布评论

评论列表(0)

  1. 暂无评论