我正在为Android应用开发REST API。我想将base64图像另存为从应用程序发送的jpeg图像。我在laravel干预下为此编写了代码,并与邮递员进行了测试。没问题。但是,当从Android应用发送数据时,他们说我收到了一条错误消息Image intervention - Image source not readable
,出现了500条错误。
之后,我看到他们的JSON包含反斜杠和\n
。所以我试图用stripslashes
,stripcslashes
,utf8_encode
,utf8_decode
来逃避他们,但是没有运气。
我离开了laravel干预并开始尝试使用imagecreatefromstring
(gd)库。也没有imagecreatefromstring(): Data is not in a recognized format
消息带来的好运。
这是我的尝试。
<?php
$json_string = '{"id_city":2,"image":"UklGRpQEAABXRUJQVlA4IIgEAAAQGgCdASpyAHMAPxF+tlMsKCUiqxLrCYAiCWkRQAHDhsYwTe19\nO7uR07OS4zITzPgo\/k\/\/0J5nUNHFl64IOAxaccVoK1IldwGQzh87cuQK\/Qv6ruo6EARQuRqZXsI1\nT1a85ouI4ZYwKmvLwgnuBPnn\/HRLOOjc4GJZ2WzZhF5g+XbewWBxrDfIJEcLkLPeY8ebLZwDLcjY\nSUrgaaxHaTX1+xU8bcKt59iNx\/IMFYnpdDJvKcJwFW9rBRy\/U5DjRlo5ZQU7dKgqlxXmdu5MGOh2\n43RLSSEWILgAANfz4R4KgwnZHkZWDlHwIRGB+lbtcZ4Y4mWUjleS5JFM3KmrDaJ419MI2ZJJU\/7k\n0Z63mAmpzXw31PSx+S+HPbgOz8MxBGrN09TRKMLuJ5i5LMOQQXjtsd0idgm7djBK+5MW0EnFMuCg\nZqmSJN3GJlBDoiuUSOsJsG4iwgt3mUGcqCnlcEvqKoGoXcELjZz1mr+WCRvFdllVtLtbO3la0aKi\neKrWUJGpztH59Gub9ENSLdVbif\/ZqknZP4suXBmfEj\/aBRk13G0p+PfzPFi\/UIYaKp2E8IgRyQUY\nF+ORSC+8+BY2hvmdRlfGxk25HvsYvNj9LCiCCmwf2LY80yDOCfAES8\/Zbkcf4Sk6cWdWvZ97N2Z1\nGxIG3u\/ozL3dojnfpkFySQr1mEfzZRlK0xXRQ2uEi3A0Av6pDw\/Agti0lqVNunnw94R8jLYOrQ7M\n7vSphyGicy+oyO0K7cbUQMVFW3j\/oRJK4s8WUiekb1enKbO96EDDTzQCCdn31wG2hU\/gR5RFYK7u\nxbdqWs1lLp\/JBwrua\/v2Vj3TDXFV9a\/JuzdOWSkEa7K8TqU2AlP67mM6HM132Ywl3ia631jGli+I\nIc8bKGr94ntR134G\/hLmQ1u2yq\/v1D8JwXgtQ2IRDsZvd1Y5EguLtdUSpl6z55GkWo69GAKGaNqH\njxW9hEAnx5aiF\/MPCsn34TVQ5saH2w0nOHKASzvCpVSYVbWiGJwxu7WQxTJA0rfRtULzLM+mymOK\neZtoiI0lYiireQpXTu8feigMY48QIU5Xc1G\/xqHSeyS5\/Z5VIFdkJCp\/JWbzoDCY\/tPdXrNwAzHX\nq8VX9qJ+4z1iiqtqx3MokhP8IX63qfOitGKrI8TfqC2SPr4BY1udsJjsVyFJDrxPtOia6JaDY5Ve\nTKcH+gs9oGAAjYdNDqOWy6FQbNJWVc\/VFTrzqXP2RJ32K9DepoTj8pAujbvjBzOxaKakp1934e9E\nyNjxeDmSPCWD31mo0351iPMqyyC1k02PO0nCttrpqsgUrdXS9v0kLtpx+Dt9n5y3tgGd\/dXnQhrW\nEcamg0R4XQy2kVbQVo\/eAi5G+VZOpo1c6mBAbU9rl1JPZY2HJmfZI5EZAXTH2Idi9zSzSjcP4cT+\nTWBl1kfl\/v+cAFGUJj9vx2dhvpq7hhFBzZ9NmBHCdE8qEEpz7JkHCgVIR6SSOZoEg9CMS\/NEGm9w\nUDgX+FGlCdz94\/3XNLPbe\/kEpZ\/5C+QJJVAV27kOIl9IC8If9YGAAA==\n"}';
$data = json_decode($json_string);
var_dump($data->image);
// After copy and pasted the var_dump result to the postman it works
$base64_decoded = base64_decode($data->image);
imagecreatefromstring($base64_decoded);
?>
答案 0 :(得分:4)
您使用Base64编码的数据实际上是.webp
图像,但是imagecreatefromstring()函数未检测到这种编码。
更改:
imagecreatefromstring(
收件人:
imagecreatefromwebp(