我知道这可以用纯 PHP 完成,但找不到解决方案。
这是我到目前为止所拥有的:
<?php
// posted canvas image data (string), such as:
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArcAAAHbCAYAAADRQ7"
$data = $_POST['data'];
// remove the "data:image/png;base64," part
$uri = substr($data,strpos($data,",")+1);
$imgData = base64_decode($uri);
// NOT WORKING: transparent to white pixels
$im = imagecreatefromstring($imgData);
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $white);
// END NOT WORKING
// put the data to a file
file_put_contents('yourimage.png', $imgData);
// force user to download the image
if(file_exists('yourimage.png')){
// save dialog box
header('Content-Disposition: attachment; filename="yourimage.png"');
// output png format
header('Content-type: image/png');
// read from server and write to buffer
readfile('yourimage.png');
}
?>
首先,我试图找出 PHP 是否可以直接更改 image/png-String 并修改透明部分,但找不到任何东西。现在我尝试从发布的字符串创建图像,但也失败了......
任何帮助表示赞赏。