0

我正在尝试将图像从外部源转换为 base64,下面是我的尝试

$imgsrc = file_get_contents('mydomain.com/assets/img/logo.png');

return response()->json([ 'img' => base64_encode($imgsrc) ]);

下面是输出,

数据:图像/png;base64,PCFET0NUWVBFIGh0bWw+DQo8aHRtbCBsYW5nPSJlbiI+DQogIDxoZWFkPg0KICAgICAgPG1ldGEgY2hhcnNldD0idXRmLTgiPg0KDQogICAgICA8dGl0bGU+Q29ubmVjdG5pZ2VyaWEgfCBFcnJvciA0MDQ8L3RpdGxlPg0KDQogICAgICA8bWV0YSBuYW1lPSJyb2JvdHMiIGNvbnRlbnQ9Ik5PSU5ERVgsIE5PRk9MTE9XLCBOT0FSQ0hJVkUsIE5PU05JUFBFVCIgLz4NCiAgICAgIDxsaW5rIHJlbD0ic2hvcnRjdXQgaWNvbiIgaHJlZj0iaHR0cHM6Ly9kZXZjbm1haW4uY29ubmVjdG5pZ2VyaWEuY29tL2Fzc2V0cy9pY28vZmF2aWNvbi5pY28iIHR5cGU9ImltYWdlL2ljbyI+DQoNCiAgICAgIDxtZXRhIG5hbWU9InZpZXdwb3J0IiBjb250ZW50PSJ3aWR0aD1kZXZpY2Utd2lkdGgsIGluaXRpYWwtc2NhbGU9MS4wLCBtYXhpbXVtLXNjYWxlPTEuMCwgdXNlci1zY2FsYWJsZT1ubyIgLz4NCiAgICAgIDxsaW5rIHJlbD0ibWFuaWZlc3QiIGhyZWY9Imh0dHBzOi8vd3d3LmNvbm5lY3RuaWdlcmlhLmNvbS9hc3NldHMvbWFuaWZlc3QuanNvbiIgLz4NCg0KICAgICAgPGxpbmsgaHJlZj0iaHR0cHM6Ly9kZXZjbm1haW4uY29ubmVjdG5pZ2VyaWEuY29tL2Fzc2V0cy9wbHVnaW5zL2ZvdW5kcnkvY3NzL2ZvbnQtYXdlc29tZS5taW4uY3NzIiByZWw9+DQogICAgICA8bGluayBocmVmPSJodHRwczovL2RldmNubWFpbi5jb25uZWN0bmlnZXJpYS5jb20vYXNzZXRzL2Nzcy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gICAgICAgPC9kaXY+DQogICAgICAgICAgPCEtLWVuZCBvZiByb3ctLT4NCiAgICAgICAgICA8ZGl2IGNsYXNzPSJlbWJlbGlzaC1pY29ucyI+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1oZWxwLWFsdCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktY3Jvc3MiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLXN1cHBvcnQiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLWFubm91bmNlbWVudCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktc2lnbmFsIj48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1wdWxzZSI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktbWFya2VyIj48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1wdWxzZSI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktYWxlcnQiPjwvaT4NCiAgICAgICAgICAgICAgPGkgY2xhc3M9InRpLWhlbHAtYWx0Ij48L2k+DQogICAgICAgICAgICAgIDxpIGNsYXNzPSJ0aS1hbGVydCI+PC9pPg0KICAgICAgICAgICAgICA8aSBjbGFzcz0idGktcHVsc2UiPjwvaT4NCiAgICAgICAgICA8L2Rpdj4NCiAgICAgIDwvZGl2Pg0KICAgICAgPCEtLWVuZCBvZiBjb250YWluZXItLT4NCiAgPC9zZWN0aW9uPg0KICAgICAgICAgIDxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4gDQoNCiAgICAgICAgICAgIHdpbmRvdy5jbl91cmwgPSAnaHR0cHM6Ly93d3cuY29ubmVjdG5pZ2VyaWEuY29tLyc7DQoNCiAgICAgICAgICAgIHdpbmRvdy5iYXNlX3VybCA9ICdodHRwczovL3d3dy5jb25uZWN0bmlnZXJpYS5jb20vJzsNCg0KICAgICAgICAgICAgd2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在<img>标签上,它没有呈现。

4

1 回答 1

0

如果你想显示图像,试试这个

$imageData = "/9j/4AAQSkZJRgABAQEAYABgAAD........";//base64_encode strong
echo '<img src="data:image/gif;base64,' . $imageData . '" />';

或者

$code_base64 = $row['content'];//base64_encode strong
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
于 2019-04-21T13:54:14.597 回答