问题
What is wrong with this code?
<?php
$data = "iVBORw0KGgoAAAANSUhEUgAAAuwAAAUeCAYAAAAl3WRgAAAgAElEQVR4Xuzdz6/vCV3f8TPADIozowg1ETZ2U2Chm6oLuqkdE1clSlzVdEhXLuAPKPwB1X110ZWBpqyMGLoiAe3KRa2bshDclE0xqSDlp4woej5nPJczd+bOmTmv+2Q+d+ZxEkPC3Pfrfs/je0ye8+V7v/exn/rNL/zDhS8CBAgQIECAAAECBE4p8JhgP+Xz4kERIECAAAECBAgQuBIQ7H4QCBAgQIAAAQIECJxYQLCf+Mnx0AgQIECAAAECBAgIdj8DBAgQIECAAAECBE4sINhP/OR4aAQIECBAgAABAgQEu58BAgQIECBAgAABAicWEOwnfnI8NAIECBAgQIAAAQKC3c8AAQIECBAgQIAAgRMLCPYTPzkeGgECBAgQIECAAAHB7meAAAECBAgQIECAwIkFBPuJnxwPjQABAgQIECBAgIBg9zNAgAABAgQIECBA4MQCgv3ET46HRoAAAQIECBAgQECw+xkgQIAAAQIECBAgcGIBwX7iJ8dDI0CAAAECBAgQICDY/QwQIECAAAECBAgQOLGAYD/xk+OhESBAgAABAgQIEBDsfgYIECBAgAABAgQInFhAsJ/4yfHQCBAgQIAAAQIECAh2PwMECBAgQIAAAQIETiwg2E/85HhoBAgQIECAAAECBAS7nwECBAgQIECAAAECJxYQ7Cd+cjw0AgQIECBAgAABAoLdzwABAgQIECBAgACBEwsI9hM/OR4aAQIECBAgQIAAAcHuZ4AAAQIECBAgQIDAiQUE+4mfHA+NAAECBAgQIECAgGD3M0CAAAECBAgQIEDgxAKC/cRPjodGgAABAgQIECBAQLD7GSBAgAABAgQIECBwYgHBfuInx0MjQIAAAQIECBAgINj9DBAgQIAAAQIECBA4sYBgP/GT46ERIECAAAECBAgQEOx+BggQIECAAAECBAicWECwn/jJ8dAIECBAgAABAgQICHY/AwQIECBAgAABAgROLCDYT/zkeGgECBAgQIAAAQIEBLufAQIECBAgQIAAAQInFhDsJ35yPDQCBAgQIECAAAECg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ToAAAQIECBAgQKAJCPbmZ02AAAECBAgQIEDgKiDYr7zOCRAgQIAAAQIECDQBwd78rAkQIECAAAECBAhcBQT7ldc5AQIECBAgQIAAgSYg2JufNQECBAgQIECAAIGrgGC/8jonQIAAAQIECBAg0AQEe/OzJkCAAAECBAgQIHAVEOxXXucECBAgQIAAAQIEmoBgb37WBAgQIECAAAECBK4Cgv3K65wAAQIECBAgQIBAExDszc+aAAECBAgQIECAwFVAsF95nRMgQIAAAQIECBBoAoK9+VkTIECAAAECBAgQuAoI9iuvcwIECBAgQIAAAQJNQLA3P2sCBAgQIECAAAECVwHBfuV1ToAAAQIECBAgQKAJCPbmZ02AAAECBAgQIEDgKiDYr7zOCRAgQIAAAQIECDQBwd78rAkQIECAAAECBAhcBQT7ldc5AQIECBAgQIAAgSYg2JufNQECBAgQIECAAIGrgGC/8jonQIAAAQIECBAg0AQEe/OzJkCAAAECBAgQIHAVEOxXXucECBAgQIAAAQIEmoBgb37WBAgQIECAAAECBK4Cgv3K65wAAQIECBAgQIBAExDszc+aAAECBAgQIECAwFVAsF95nRMgQIAAAQIECBBoAoK9+VkTIECAAAECBAgQuAoI9iuvcwIECBAgQIAAAQJNQLA3P2sCBAgQIECAAAECVwHBfuV1ToAAAQIECBAgQKAJCPbmZ02AAAECBAgQIEDgKiDYr7zOCRAgQIAAAQIECDSBAaWRPdih0+LkAAAAAElFTkSuQmCC";
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
I get these errors:
Warning: imagecreatefromstring(): gd-png: fatal libpng error: Read Error: truncated data in D:\wamp\www\img\index.php on line 5
Warning: imagecreatefromstring(): gd-png error: setjmp returns error condition in D:\wamp\www\img\index.php on line 5
Warning: imagecreatefromstring(): Passed data is not in 'PNG' format in D:\wamp\www\img\index.php on line 5
Warning: imagecreatefromstring(): Couldn't create GD Image Stream out of Data in D:\wamp\www\img\index.php on line 5
An error occurred.
Can anyone please help me?
Thank You.
回答1:
There's a ...
in the middle of your base64 encoded image.
AAAECBAg...gQIECAAA
回答2:
The base64
data is not valid
, complete
or contains invalid characters
... Here is a quick verification
var_dump($data === base64_encode(base64_decode($data)));
Returns
bool(false)
回答3:
$image = imagecreatefromstring(file_get_contents('your_image_path_here'));
Example: $image = imagecreatefromstring(file_get_contents('sample.jpg'));
来源:https://stackoverflow.com/questions/16701238/whats-wrong-with-imagecreatefromstring-function