How to save a PNG image server-side, from a base64 data string

后端 未结 15 2332
情话喂你
情话喂你 2020-11-22 03:18

I\'m using Nihilogic\'s \"Canvas2Image\" JavaScript tool to convert canvas drawings to PNG images. What I need now is to turn those base64 strings that this tool generates,

15条回答
  •  抹茶落季
    2020-11-22 03:48

    I had to replace spaces with plus symbols str_replace(' ', '+', $img); to get this working.

    Here is the full code

    $img = $_POST['img']; // Your data 'data:image/png;base64,AAAFBfj42Pj4';
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    file_put_contents('/tmp/image.png', $data);
    

    Hope that helps.

提交回复
热议问题