[英]base64 image to png not working php
我正在嘗試將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.';
文件創建成功。 但是它不能打開,它不是可讀格式。
很抱歉,您的問題已恢復,但請看一下,它的工作原理很吸引人:
<?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.