问题
I'm using an online service which need an image in base 64. I've done a simple form where I can upload an image, and then my php encode the image in base 64, like this :
$base64Image = base64_encode(file_get_contents($_FILES['image']['tmp_name']));
I then pass it in a json array, and the other page tries to display the image with my data, but it fails (it has the missing image icon) and in Chrome console I have the error :
page.php:1 GET data:image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAADXkl…kBnhcAeF6A5wV4XoDnBXheQIfnBXhewAjPCxTxr8wL/AFFnWzmtWVLygAAAABJRU5ErkJggg== net::ERR_INVALID_URL
The HTML code for the img element :
<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==">
What's wrong ?
回答1:
Your html code contains invalid base64 code. I think that slashes are skipped while you are encoding the image to json.
来源:https://stackoverflow.com/questions/41462036/encode-image-file-to-base-64