我正在使用Google Charts API生成一些图表,并且正在使用ajax请求将数据发送到端点,该端点会将图像保存到磁盘以供PDF使用。但是,我不确定如何将图像字符串转换为PHP图像资源。
看起来像这样(我将剪一堆代码以节省一些空间):
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAEsCAYAAABwoFP+AAAgAElEQVR4XuzdCZcDuXXe/XGcfU/sLI41Glv5/l9JGmskZ99XJ7H9nl/bf71QqUgWyeKOOqdPd5NVKODiAvfBXX/rz//8z//8m3lNCkwKTArsTAFby//7f//v6+c//If/8M3f+Bt/45v//b//9zd/+qd/+s3f//t//5vf+q3f+nrjn/3Zn33d8z/+x//45vd+7/e+Pv+bf/Nv7tyb2dykwKTApMCkwC0o8FsTSN6CrLPNSYHPpsD//b//9TJwxYw7z0NGWIDRtXeSQyxAOi1trkg3pCSmx2MUn75CgHbRTcKMlTnqf+PAhEDRO8Xuk/SW/5KVcOxwmV4Q0Wmzt+1+bVH3Ytk1Fkly339GSMm9fVRMZLGgOma2jnYdtSkR61hBx//Ocffr0aVR0Ieuus1loE9HDP9H85OVcKX2WHDSfrl163ZJ71LmFQA8CRSR7UKpzCoEdEYjGgmZizicv3YjWKaUQk0YIcUxi86GGIxG5zHGtWdc9EdhEgLf5Ky2UCMmS4J8doTpF0zSE8bFEFGkG/S+pgwwC3ggbOTp3aXR1gEgg1VD7OFfGcez7JGgvWf9/ZgPPE1zifpD0TfBHtj13NJLD5O9JOZUCAzYRyStK3m0FI20hmv4v8jwR+ql37hl3LeKYOtyIp375DuG9UsWqU7wELtbJIpIXE3gN93gIJC1NqtwkqhjBm/Lds/Agg9E6IRT+F5PpLUf8mC57nPrdXz+W1g4+HsqP6xE5IGptDsmkXCKfluAjA+S+xCc2I0E8yHIYOTzMAzk2cpsN8s880V9ktPzu/h8tJB++yJ3E4K37QXI8+m7sOaL998x4LpFN58SHNfXS82wjm9qDvXsgjDZ1Dj9978j3fqc51jfWi3vcIh73VNSdXhmBIpKvL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';
$decoded = json_decode($data, true);
$resource = imagecreatefromstring($decoded['image_data']);
var_dump($resource);
这会在imagecreatefromstring
步骤Warning: imagecreatefromstring(): Data is not in a recognized format
上发出警告。
该图像格式的正确处理程序是什么?
答案 0 :(得分:0)
这是因为您需要摆脱领先的data:image/png;base64,
而且您的base64字符串看起来已损坏。我做了以下
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAQhUlEQVR4Xu2dTWxcVxWAz3kzEwKqiCtoG0eCWiCx6IK6Euwq6u5aqB0HCVGJnzoggWji1lmAxApbLGCBVAc7QnQBjlS1YlXHToVAIBwJEAsEk+5QF7ggOa666ARQ0mRm3kH3zZvxeDJjz3jsmTn2N1IWyby577zvnHy697777lPhAwEIQMAJAXUSJ2FCAAIQEIRFEUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykikAhAAGERQ1AAAJuCCAsN6kiUAhAAGFRAxCAgBsCCMtNqggUAhBAWNQABCDghgDCcpMqAoUABBAWNQABCLghgLDcpIpAIQABhEUNQAACbgggLDepIlAIQABhUQMQgIAbAgjLTaoIFAIQQFjUAAQg4IYAwnKTKgKFAAQQFjUAAQi4IYCw3KSKQCEAAYRFDUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykikAhAAGERQ1AAAJuCCAsN6kiUAhAAGFRAxCAgBsCCMtNqggUAhBAWNQABCDghgDCcpMqAoUABBBWhzUw9PpLQ8dzudNqNqmiI2YypCqF2OyOir6nqn+J1daTZuNovZyxm8W7d9cLZy4UOjwVh0MAAg0EEFabJfHAG5dGc2V7UVSmGn9iZqK6C0qzgqnmxSSvImsbE+evtHlqDoMABFICCKuNUhheWXhVRZ4W1aFmh7clrHstVxDRZRFZRl5tJIFDICAiCGuHMgjDvw9msv9Q1Qebikrk7dBjErV1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qAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAv8H5S4HFJE4mRUAAAAASUVORK5CYII="}';
$decoded = json_decode($data, true);
$final = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $decoded['image_data']));
$im = imagecreatefromstring($final);
if ($im !== false) {
ob_clean();
header('Content-type: image/png');
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
}
?>