What's wrong with imagecreatefromstring function?

ptCoder picture ptCoder · May 22, 2013 · Viewed 12.5k times · Source

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/zkeGgEC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QuAoI9iuvcwIECBAgQIAAAQJNQLA3P2sCBAgQIECAAAECVwHBfuV1ToAAAQIECBAgQKAJCPbmZ02AAAECBAgQIEDgKiDYr7zOCRAgQIAAAQIECDQBwd78rAkQIECAAAECBAhcBQT7ldc5AQIECBAgQIAAgSYg2JufNQECBAgQIECAAIGrgGC/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.

Answer

Mr. Llama picture Mr. Llama · May 22, 2013

There's a ... in the middle of your base64 encoded image.

AAAECBAg...gQIECAAA