How to display Base64 images in HTML
My suspect is of course the actual Base64 data. Otherwise it looks good to me. See this fiddle where a similar scheme is working. You may try specifying the character set.
<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>
Attach image and send it via json in base64 encoding
Updating the answer based on comment. you can refer to the example on sandbox
Please add create a promise to avoid callback hell. Here I have promised the base46 Function. Promisification
const getBase64Promise = function (file) {
return new Promise((resolve, reject) => {
getBase64(file, (success,err) => {
if(err) reject(err)
else resolve(success);
});
});
};
Like this
async function construct_json(form, callback) {
let data = await getBase64Promise(form.get("picture"));
let json = {
picture: data
};
return json;
}
Base64 Encoding Image
As far as I remember there is an xml element for the image data. You can use this website to encode a file (use the upload field). Then just copy and paste the data to the XML element.
You could also use PHP to do this like so:
<?php
$im = file_get_contents('filename.gif');
$imdata = base64_encode($im);
?>
Use Mozilla's guide for help on creating OpenSearch plugins. For example, the icon element is used like this:
<img width="16" height="16">data:image/x-icon;base64,imageData</>
Where imageData
is your base64 data.
encoding an image to base64 string
You've only copied the first 1024 characters of the encoded string into the question. Maybe the print
command truncated it. Also, the online tool seems to prefer (require?) the URL safe version, so as Günter says, use base64UrlEncode
.
To confirm that the string you are using is roughly the right length, add 33%. That is, a 2k image would convert to a string roughly 2667 characters long.
Django rest framework returning base64 image URL encoded
I managed to solve this issue by creating a dict for the base64 images outside the serializer of the model, which was the one doing that URL encoding. I also had to remove new line characters as the response was drawing them as characters.
with open(f"image_file.jpg" , "rb") as image_file:
images_dict["image_1"] = base64.encodebytes(image_file.read()).decode('utf-8').replace("\n", "")
Decoding a base64 image
As there can not be ,
in base64 string and also it is the separator, so the image part is right after the comma:
byte[] imgBytes = Convert.FromBase64String(base64string.Split(',')[1]);
converting image to base64 - image becomes invisible
I am assuming u need the key baseImageCorrect
and encoding
key at the same level.
Use this instead:
const EncodedImage = JSON.stringify({
fileBase64: {baseImageCorrect, encoding: 'base64' },
});
Related Topics
PHP _Get and _Set Magic Methods
Truncate Text Containing Html, Ignoring Tags
How to Properly Use a Pdo Object For a Parameterized Select Query
How to Return Integer and Numeric Columns from MySQL as Integers and Numerics in PHP
Best Way to Defend Against MySQL Injection and Cross Site Scripting
Downloading Large Files Reliably in PHP
How to Validate Array in Laravel
PHP Replacing Multiple Spaces With a Single Space
How to Remove a Comma Off the End of a String
PHP Connection Failed: Sqlstate[Hy000] [2002] Connection Refused
Need to Write At Beginning of File With PHP
JavaScript Equivalent of PHP'S In_Array()
How to Create and Download a CSV File from PHP Script
PHP: Access Array Value on the Fly
Insert Multiple Rows With One Query MySQL