0

我正在尝试将 base64 图像转换为 png。这是我的代码

$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';

文件创建成功。但它无法打开。它不是可读的格式。

4

1 回答 1

0

很抱歉再次提出您的问题,但请看一下,它就像魅力一样:

<?php

$img = $_POST['Base64Variable'];
define('UPLOAD_DIR', 'YourFolder/');


$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);

$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';

$success = file_put_contents($file, $data);

print $success ? $file : 'Could not save the file!';

?>
于 2014-07-06T10:41:00.873 回答