使用PHP,我正在尝试从datauri生成图像。如果我将字符串硬编码为这样解码,它可以正常工作:
$sourceString = "iVBORw0KGgoAAAANSUhEUgAAAJ8AAAArCAYAAABmQFqDA..."; // I truncated the actual string for this example...
$destination = $_SERVER['DOCUMENT_ROOT'].'/test.jpeg';
$image = imagecreatefromstring(base64_decode($sourceString));
imagejpeg($image, $destination, 100);
这很好用,并在我的根文件夹中生成一个名为“test.jpeg”的图像。
我的问题是,如果我从POST变量中获取datauri,它就不起作用。这是我的代码:
$sourceString = $_POST['data'];
$destination = $_SERVER['DOCUMENT_ROOT'].'/test.jpeg';
$image = imagecreatefromstring(base64_decode($sourceString));
imagejpeg($image, $destination, 100);
在上面的例子中,我确认$ sourceString包含与我在第一个例子中硬编码时相同的确切内容。在第二个示例中,我收到以下错误:
[09-Sep-2015 16:54:00 UTC] PHP Warning: imagecreatefromstring(): gd-png: fatal libpng error: Read Error: truncated data in request.image_paste.php on line 10
[09-Sep-2015 16:54:00 UTC] PHP Warning: imagecreatefromstring(): gd-png error: setjmp returns error condition in request.image_paste.php on line 10
[09-Sep-2015 16:54:00 UTC] PHP Warning: imagecreatefromstring(): Passed data is not in 'PNG' format in request.image_paste.php on line 10
[09-Sep-2015 16:54:00 UTC] PHP Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in request.image_paste.php on line 10
[09-Sep-2015 16:54:00 UTC] PHP Warning: imagejpeg() expects parameter 1 to be resource, boolean given in request.image_paste.php on line 11
我不明白什么是错的。我尝试将变量转换为字符串:
$sourceString = (string)$_POST['data'];
但这没有帮助。
有谁知道可能出现什么问题?