我从网络上找到了下面的php文件,在我用角度4设计了一个Web应用程序之前,它工作得很好,在上传之前必须将照片裁剪掉。裁剪图像后,将裁剪后的图像保存为base64格式,现在我的问题是我如何配置当前的php脚本以将base64图像转换为真实图像
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(array('status' => false));
exit;
}
$path = '../myapp/img_folder/';
if (isset($_FILES['file'])) {
$originalName = $_FILES['file']['name'];
$ext = '.'.pathinfo($originalName, PATHINFO_EXTENSION);
$generatedName = md5($_FILES['file']['tmp_name']).$ext;
$filePath = $path.$generatedName;
if (!is_writable($path)) {
echo json_encode(array(
'status' => false,
'msg' => 'Destination directory not writable.'
));
exit;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
echo json_encode(array(
'status' => true,
'originalName' => $originalName,
'generatedName' => $generatedName
));
}
}
else {
echo json_encode(
array('status' => false, 'msg' => 'No file uploaded.')
);
exit;
}
答案 0 :(得分:4)
这是您的问题的解决方案。经过测试并可以正常工作。
$encoded = "encoded---text---here===";
$file = fopen("mypicture.png", "w"); //(you can put jpg, png or any other extension)
fwrite($file, base64_decode($encoded));
fclose($file);