I have an image that is base64 encoded. Decoding this image through C# works like a charm with the following code:
byte[] imageBytes = Convert.FromBase64String(decodeImageForm.Data);
var ms = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
pbImage.Image = Image.FromStream(ms, true);
However, when trying to decode the same text in PHP using the following code
$payload = "..."; // same text as used above
$data = base64_decode($payload);
$im = imagecreatefromstring($data);
if ($im !== false)
{
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else
{
die("Failed to create image from string. ".strlen($payload));
}
it fails with the following warning:
Warning: imagecreatefromstring(): Data is not in a recognized format
Here is the GD section of phpinfo()
- GD Support enabled
- GD Version bundled (2.0.34 compatible)
- FreeType Support enabled
- FreeType Linkage with freetype
- FreeType Version 2.4.6
- T1Lib Support enabled
- GIF Read Support enabled
- GIF Create Support enabled
- JPEG Support enabled
- libJPEG Version 6b
- PNG Support enabled libPNG Version 1.2.46 WBMP Support enabled XBM Support enabled
Directive Local Value Master Value gd.jpeg_ignore_warning 0 0
What am I missing here? There are images that do work sometimes