[英]base64 image to png not working php
Im trying to convert base64 image to png.This is my code 我正在尝试将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.';
The file is created successfully. 文件创建成功。 But it could not be open.Its not in a readable format.
但是它不能打开,它不是可读格式。
Sorry for reviving your question, but take a look, it worked like charm: 很抱歉,您的问题已恢复,但请看一下,它的工作原理很吸引人:
<?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!';
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.