I have a base64 encoded png, how do I write the image to a file in PHP?

前端 未结 3 797
我寻月下人不归
我寻月下人不归 2020-12-01 01:40

What\'s the proper way in PHP to create an image file (PNG), when I have the base64 encoding?

I\'ve been playing around with:


file_put_contents(\'/         


        
3条回答
  •  旧时难觅i
    2020-12-01 01:51

    You need to use base64_decode(). AND. Sometimes it is not sufficient. Here is all code that you need:

    $img = $_POST['data'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $fileData = base64_decode($img);
    //saving
    $fileName = 'photo.png';
    file_put_contents($fileName, $fileData);
    

    P.S. I used this code to get PNG image from html canvas.

提交回复
热议问题