我使用Nihilogic的“Canvas2Image”JavaScript工具将画布图纸转换为PNG图像。 我现在需要的是使用PHP将该工具生成的base64字符串转换为服务器上的实际PNG文件。

简而言之,我目前所做的是在客户端使用Canvas2Image生成一个文件,然后检索base64编码的数据并使用AJAX将其发送到服务器:

// Generate the image file
var image = Canvas2Image.saveAsPNG(canvas, true);   

image.id = "canvasimage";
canvas.parentNode.replaceChild(image, canvas);

var url = 'hidden.php',
data = $('#canvasimage').attr('src');

$.ajax({ 
    type: "POST", 
    url: url,
    dataType: 'text',
    data: {
        base64data : data
    }
});

在这一点上,"hidden.php"收到一个数据块,看起来像data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABE…

从现在起,我几乎被难住了。从我所读到的,我相信我应该使用PHP的imagecreatefromstring函数,但我不确定如何从base64编码的字符串实际创建一个PNG图像并将其存储在我的服务器上。 请帮助!


当前回答

这段代码为我检查以下代码:

<?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);
?>

其他回答

好吧,上面的解决方案取决于图像是一个jpeg文件。我用的是通解

$img = $_POST['image'];
$img = substr(explode(";",$img)[1], 7);
file_put_contents('img.png', base64_decode($img));

我必须用加号替换空格str_replace(' ', '+', $img);让这个工作起来。

这里是完整的代码

$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);

希望这能有所帮助。

总担心:

$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

我希望这对你有所帮助。 我用core php方法解决了这个问题。 我解决的代码如下。

$base64string = 'BASE64 STRING GOES HERE';
$uploadpath   = 'YOUR UPLOAD DIR PATH';
$parts        = explode(";base64,", $base64string); //THIS WILL GET THE ORIGINAL FILE ENCODE STRING
$imagebase64  = base64_decode($parts[1]); //THIS WILL GET THE DECODED IMAGE STRING
$file         = $uploadpath . uniqid() . '.png'; // THIS WILL GIVE THE FILE NAME AND SET THE FILE PATH
file_put_contents($file, $imagebase64); // THIS FUNCTION WILL STORE THE IMAGE TO GIVEN PATH WITH FILE_NAME

这个函数应该可以工作。它有一个photo参数,保存base64字符串,还有一个现有图像目录的路径,如果你已经有一个现有的图像,你想要在保存新图像时解除链接。

 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
        }
    }
}