尝试使用 imagecreatefromstring 保存 base64 图像,它完全是黑色的

Posted

技术标签:

【中文标题】尝试使用 imagecreatefromstring 保存 base64 图像,它完全是黑色的【英文标题】:Trying to save base64 image using imagecreatefromstring and it is completely black 【发布时间】:2011-01-27 06:35:21 【问题描述】:

我正在尝试从数据库中获取 Base64 编码图像并将其保存到文件中。无论出于何种原因,当我尝试使用 imagecreatefromstring 和 imagepng 时,图像完全是黑色的。我尝试了许多不同的方法,但最终还是得到了一个黑匣子。下面是我让它工作的最佳尝试。有任何想法吗?提前非常感谢您!

$thefile = base64_decode($strImage);  
$img = imagecreatefromstring($thefile);  

if($img !== false)  

    header('Content-Type: image/png');  
    imagepng($img);  
    imagedestroy($img);   

这应该是上面的,但格式看起来不正确.... $ strImage ='iVBORw0KGgoAAAANSUhEUgAAA4QAAAD6CAYAAAD5jokXAAAMrUlEQVR4nO3dPahkZxkH8P + CkiqlaKGojcHCr9gEBBsbu5QiSRXBTksRsUijwcI0IorBD4gGGwvxC4soKLEQImiMRgMmMYmRGDbZbLJ7d ++ 9Zy3OPdzZ2bv3zuzMnOd8 / H4wxe4O8z4zmWL + ed73fRIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABG4ukkzQmPgyQXkjxcVxoAAAC78kba8HeY5OrRY // oz4vhcL + qQAAAAHanOeXfPpe2U9gFw2d6qQgAAIBeXFvhOa9FtxAAAGByTusQLlvsFr68m3IAAADoyyodwkW6hQAAABOxTodwUdctPNxiLQAAAPRo3Q7hor0cdwtf3U45AAAA9GWTQJgkP48tpAAAAKN0q1tGl9lCCgAAMDKbdggXLW4h / d0WXxcAAIAd2GYgTI63kF5L8uaWXxsAAIAt2taW0WWHR6 + 9t6PXBwAAYEPb7hAuEgoBAAAGbJeBMBEKAQAABmtXW0YXCYUAAAADtOsOYUcoBAAAGJi + AmEiFAIAAAxKn4EwEQoBAAAGo48zhMu6UHi5YG0AAACO9N0h7HSh8N9F6wMAAMxeVSBM2kDYJPl4YQ0AAACzVRkIf5DjUAgAAEDPKgNhkryWNhAeFtcBAAAwO0Pozu2lreOguhAAAIA5qe4QdvbThsKr1YUAAADMxVACYdJ2CJskF6sLAQAAmIMhBcLEJTMAAAC9GVogvJw2EL5RXQgAAMDUDS0QJrqEAAAAO3d3hhm8Xk9b1151IQAAAFPWJLlUXcQJDjPMsAoAADBoz6e9sfPyCs9t0o58GJqnYwwFAADA2tYZ8t6k7cYNkS4hAADAmta5KGbIgfCBrBduAQAAZm + dQDj0Llw3rP5cdSEAAABjsE4g7ALXkA25iwkAADAo6wS8K2s + v0IXWr9fXQgAA MDQrXM7508z / ECY6BICAACsZN3wtM4W0ypdJ / Of1YUAAAAM2boXxYwhECa6hAAAAGda96KYsQTCMVyAAwAAUOrlrDeqYSyBMGnf1351EQAAAEO2zsUyYwqEQ5 + bOCUvpu3KfrFg7fMFawIAwGSsc95uTIHw2bTv7XJxHXPRbdP9bc / rCv0AALCBdTppYwqESfu + BIB + VHW / xvadBACAQdnPdAPhG2nf24PVhcyEQAgAACPzStrQ9LUVnjvGH99jrHmsBEIAABihVS + WGeP2S4GhP31 / P24vWBMAACZn1YtlxhiuxljzWFV81k2SbxWsCwAAk7HqxTJjDFdjrHmsqgLhcwXrAgDAZHQjA84yxnA1xprHquKzPkhypWBdAACYjO5imXNnPG + M4apJzcD0Oar4fpyPc4QAALCxVS6WGVsgvBphoU8V34 / fxH9jAADY2CoXy4wtEK56NpLNnUvNZ / 2BrNbdBgAATrFKeBpbIFz19lS2o0nyy6J1bQsGAIANXMk0A + FBdREz0iR5rWjdPxSsCwAAk9Ft + bt4ynPGFAj / nvb9uIGyP02S / YJ1D9JejAQAAGzgrI7aWM7j7cV20QpVn / nFnH0hEgAAcIYmp4e + MXQIhcE6VZ / 7UxnP / 6wAAIDBOmtA / dADoTBYq + PW1 + 8WrQsAAJPyatof1g / C5N + HHAi7S3GEwTpVgfD9MXoCAAC24rSLQYYaCA / S1uZG0***dZh3qUnyzaK1AQBgMk7rsg0tEF7N8blHYbDepdQFwr0kzxetDQAAk3Fal2cogfBijoPgYZLP1JbDkb + mLhA + mZqRFwAAMCkX0v6of + SEf6sOhHfl + Jxa1RB0bu7DqQuEny9cGwAAJuVm5wirAuGFXB8EDZsfribJjwrX / mzR2gAAMBk3O0fYdyDcz3EIdE5wHJokLxWtvZ / kz0VrAwDAZNzsHGEfgXC5G3h49HeMQ5P2gpcKLya5XLQ2AABMxitpf9jfs / T3uwyEy91AF 4SMU2Un96E4RwgAAFvRpB0jsGjbgfBKdAOn5rSxJbt2 + 9H6dxatDwAAk3FSl24bgXA5BOoGTktlIOzW / 1Xh + gAAMAkn / bDfJBBezo0h8NwGr8cwdWG / yoUkrxauDwAAk7DtQJij17uW64PhWY / Do8dBkqtph9ILksNVHQgfTW2HEgAAJuGkH / bb2DL6Ztpgd9rjIMdBcJXQeJA2CFCvuxyoyl1H699WWAMAAIzeSaMnqgbTL / tS2gtv9nNjaDxMGyp1EWtcSP1NnwdJvlFcAwAAjNqVDDcQnuS5HIfY5YD4ZmFdc / PH1AfCZ9LOJAQAAG7Rf9L + SF / stA05EC67nJO7h8ujNNiud6c + EH4lzhECAMDGlkdCVP / Qv1V35sbB94dJni2sacqaJF8vXP8tRzV8srAGAAAYveWbRsfUIbyZ7 + XGraUHSe6tLGpimiR / K67hYpLHimsAAIBRW75YZgqBcNGF3Lit1IU0m2uSvF5cw6OxPRgAADbS3Rj5w6M / Ty0QLtrLjVtK7ymtaLy6YF3pQ0d1vLW4DgAAGLXFc4RTDoSLFs8bCobrW95qXOVSkvuqiwAAgDFb / HE / l0DYWTxrKBiubiiB8E9JflldBAAAjNniOcK5BcKOYLieoQTCryZ5uboIAAAYs1fS / SD / dOYbCDvLwfAfteUMVndRT7X3pK3jtuI6AABg1Jq0l67MPRB2FoPhleJahmgogTBxjhAAADbWzeoTCK / XBcOD6kIGZnlcSSXnCAEAYEPdFkmB8EbdjaRDODM3FJcynEDoHCEAAGxocXg7N1qcYWioffJChvNdeW / abb3mEQIAwC3qtgDqEN7cazkOhb8vrqXazzKcQJgkP07yheoiAABgrLotgALh6b6T41D4RHEtld6ZYQXCdyV5uLoIAAAYM4Fwdd1n9WZ1IYWaJN + uLgIAANgOgXA93bnLvepCijRJ / lVdBAAAsB0C4frmHAqbzLtDCgAAk2LsxK2Zayg0nxEAA CZEILx1cwyFZjMCAMCEXI1AuIludMcb1YX0RCAEAIAJeTxtIDR4 / dbNaRtl1xUFAAAmQodwM3MKSXN6rwAAMAsC4WauZj4h6Urm814BAGAWBMLNNWnD0tT9LwIhAABMikC4ublctvKbCIQAADApAuHmuttGp + 6OzON9AgDAbAiEm3skbVA6X11ID5okP6ouAgAAYEjmsm20SfJSdREAAABDMpeRDHO5QAcAAGBl59OGpU9VF7Jjc + mEAgAArKVJO5dwygRCAACAE8whLM1laywAAMBa5hCW9jP99wgAALC2OcwjvJDpv0cAAIC1PZo2LJ3rcc2 + u5KP9bweAADAaFzreb1Xel7z / rSB8O09rgkAADAKfQfCijXncHkOAADA2voOZ / EM / 22qvz5a84M9rgkAADB4FR3CJu3tn32qeJ8AAACDtZ + 6QNjnFs6fHK35iR7XBAAAGKQH0wakJsmVgvUrxl104yfu73ldAACAQbg7x2MfmiQvFNXxl6P1P1q0PgAAwGycy / VB8KC2nCTDqQMAAGBy7svx1swhBcFO3wPqAQAAJu1Sru8Edo ++ B / RcxVNpa / tIdSEAAABj8r4ke2k7ficFwMMkr5ZVt7omydXqIgAAgGn5Vdqw9LHqQjZ0IW1376TQtxwA95PcUVPmLet7 / AQAADATQwsb9yY5n3bMw36Ou3tnhb3F0NcFv9 / 3XPuuOEcIAADsxMW0Q9dXCVvVj8OFx0Ha0Pfk9j + SwamYRwgAAMzE + bRn1Cofe0me2PUbHalfpA2E76guBAAAgP4N9RZUAAAAdmxoZz0BAADoiXOEAAAAM / VQ2kD4tupCAAAA6N + 16gIAAACoIRACAADMlEAIAAAwUwIhAADATAmEAAAAMyUQAgAAzJQ5hAAAADP037SB8MvVhQAAANC / g7Sh8PHqQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAbfo / R04U3Pjl9j8AAAAASUVORK5CYII ='; P>

【问题讨论】:

我会说和alpha通道有关,base64的内容很好,我用其他工具写出来的。 【参考方案1】:

在你的 imagepng() 之前调用 imagesavealpha()

imagesavealpha($img, true);
imagepng($img);

【讨论】:

谢谢!那行得通!昨晚我花了好几个小时把头撞在墙上试图弄清楚这一点。非常感谢您抽出宝贵时间回答 这是救命稻草 :)

以上是关于尝试使用 imagecreatefromstring 保存 base64 图像,它完全是黑色的的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用堆叠小部件构建布局

尝试使用 Curl 登录网站

不安全的 JavaScript 尝试使用框架访问

当我尝试使用 ansible ping 我的 VM 时,它会尝试 ping 服务器 ID 而不是 IP

尝试使用 ConfigurationBuilder 但得到 FileLoadException

尝试使用 Jquery 更改 HTML [重复]