将图像文件编码为base 64

时间:2017-01-04 10:51:37

标签: php html image base64

我使用的是需要基本64位图像的在线服务。我已经完成了一个简单的表单,我可以上传图像,然后我的php在base 64中对图像进行编码,如下所示:

$ base64Image = base64_encode(file_get_contents($ _ FILES [' image'] [' tmp_name']));

然后我在json数组中传递它,另一个页面尝试用我的数据显示图像,但它失败了(它有丢失的图像图标),在Chrome控制台中我有错误:

page.php:1 GET data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAADXkl…kBnhcAeF6A5wV4XoDnBXheQIfnBXhewAjPCxTxr8wL/AFFnWzmtWVLygAAAABJRU5ErkJggg== net::ERR_INVALID_URL

img元素的HTML代码:

<img src="data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAADXklEQVR4nO2bP0xTURTGv5YXgk1kaFOt0WqCktaoMVrqgJEQ66CJimEwMS5AogPxz6gmTi4Km5KQirs7hciiYfIPCuLWmlYTivwTaVK0pUjLc8D3wiv0Qe 55BlzfmPv 9qPc3Pb 51wbKqqqiDQdWsEPyYXkIzNl6Vzex042uBGaipH0l 9d6gsXTF2khrAePwn7j vx43Hx H1bd/wefeebWh9cAQP xqQmsqR9VQU6hvMjmfxtn8CDc1eBEIejLycRiScWLOjbq8DF67tR/3F3ahQ7BiLpjE6OANVBUlPhVwAAOjvSeDkX2OBMx4cO70To4Mz6O2OI5fJ4/z1A/q6RiScgHb4qHoKUgow 20BbyITONXsBQDY7TYEQh4EQh4U8ssG4wCQjM3j46sZaXoK5O8Ajb5nX1DIL695vdg8APR2x6XrRZFWgNnxLF5HJjZ8rtTZpepFkVYAYOUsr7eLqzE7u1S9CFILsJgtoJA3d5dJL22ZXgSpBTjXVoPKqgrTZ5raa7dML4K0AlS7KtF4ee Gzx084YI/6JSuF0VaAc621KDKsblf1fV2kaoXRUoBqp2VCF3ZZ3htLJpG1 0RPGp5h j7OcOaP iCr84pTU9BykVo9dlNxubR2x3Xr7kA0Nk2BH/Qiab2WviDLgAru9jZNiRFT4FcAO3sjkXTiIQTJW9osQ8pxFqH4Ktb UO0szz59RdJT8VGjcM9dz9hMVsw7Nhm8Aed8AVd J7MkPSXiN8Htic3h1Ur8zgVaj/CbnUep0L1r1DzvNVQ/SuAtXlcBhT/CiA3j7cefmFqtmOgETu8jpLrInqKf33FqjwuC1H/ qpVeVwWov4N5bEij8tExL hAFbkcZmI DcUwIo8LhMR/3oBrMrjshD1rxfAqjwuC1H/ClA6j0fCCWTSS3r60tDy Ofh1Jo37xhoNP1w164q03URPcW/AsjN42aXnM0goqf4V6h53mqo/m1P74yqVuZxKtR hH0xW8CPyYWyLzdz0zlk0r/LE20BVP/cD B ALgfwP0AgPsB3A8AuB/A/QDuB3A/gPsBOtwP4H6AEe4HFPG/9API/yDB8wI8L8DzAjwvwPMC4HkBnhcAeF6A5wV4XoDnBXheQIfnBXhewAjPCxTxr8wL/AFFnWzmtWVLygAAAABJRU5ErkJggg==">

出了什么问题?

1 个答案:

答案 0 :(得分:1)

您的html代码包含无效的base64代码。我认为在将图像编码为json时会跳过斜杠。