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

后端 未结 15 2326
情话喂你
情话喂你 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:38

    This code works for me check below code:

    <?php
    define('UPLOAD_DIR', 'images/');
    $image_parts = explode(";base64,", $_POST['image']);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);
    $file = UPLOAD_DIR . uniqid() . '.png';
    file_put_contents($file, $image_base64);
    ?>
    
    0 讨论(0)
  • 2020-11-22 03:41

    This function should work. this has the photo parameter that holds the base64 string and also path to an existing image directory should you already have an existing image you want to unlink while you save the new one.

     public function convertBase64ToImage($photo = null, $path = null) {
        if (!empty($photo)) {
            $photo = str_replace('data:image/png;base64,', '', $photo);
            $photo = str_replace(' ', '+', $photo);
            $photo = str_replace('data:image/jpeg;base64,', '', $photo);
            $photo = str_replace('data:image/gif;base64,', '', $photo);
            $entry = base64_decode($photo);
            $image = imagecreatefromstring($entry);
    
            $fileName = time() . ".jpeg";
            $directory = "uploads/customer/" . $fileName;
    
            header('Content-type:image/jpeg');
    
            if (!empty($path)) {
                if (file_exists($path)) {
                    unlink($path);
                }
            }
    
            $saveImage = imagejpeg($image, $directory);
    
            imagedestroy($image);
    
            if ($saveImage) {
                return $fileName;
            } else {
                return false; // image not saved
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:42

    PHP has already a fair treatment base64 -> file transform

    I use to get it done using this way:

    $blob=$_POST['blob']; // base64 coming from an url, for example
    $blob=file_get_contents($blob);
    $fh=fopen("myfile.png",'w'); // be aware, it'll overwrite!
    fwrite($fh,$blob);
    fclose($fh);
    echo '<img src=myfile.png>'; // just for the check
    
    0 讨论(0)
  • 2020-11-22 03:43

    Well your solution above depends on the image being a jpeg file. For a general solution i used

    $img = $_POST['image'];
    $img = substr(explode(";",$img)[1], 7);
    file_put_contents('img.png', base64_decode($img));
    
    0 讨论(0)
  • 2020-11-22 03:43

    Total concerns:

    $data = 'data:image/png;base64,AAAFBfj42Pj4';
    
    // Extract base64 file for standard data
    $fileBin = file_get_contents($data);
    $mimeType = mime_content_type($data);
    
    // Check allowed mime type
    if ('image/png'==$mimeType) {
        file_put_contents('name.png', $fileBin);
    }
    

    http://php.net/manual/en/wrappers.data.php

    http://php.net/manual/en/function.mime-content-type.php

    0 讨论(0)
  • 2020-11-22 03:45

    Taken the @dre010 idea, I have extended it to another function that works with any image type: PNG, JPG, JPEG or GIF and gives a unique name to the filename

    The function separate image data and image type

    function base64ToImage($imageData){
        $data = 'data:image/png;base64,AAAFBfj42Pj4';
        list($type, $imageData) = explode(';', $imageData);
        list(,$extension) = explode('/',$type);
        list(,$imageData)      = explode(',', $imageData);
        $fileName = uniqid().'.'.$extension;
        $imageData = base64_decode($imageData);
        file_put_contents($fileName, $imageData);
    }
    
    0 讨论(0)
提交回复
热议问题