Answer the question
In order to leave comments, you need to log in
What are base64 equivalents for images?
What's the problem: now I'm making an application that takes pictures and sends them to the backend (php). After processing, they are base64 encoded and sent back to the frontend. The problem is that some pictures are not displayed. Please advise what can be done
Answer the question
In order to leave comments, you need to log in
If correctly converted to Base64, they will all be displayed!
$files = array_slice($argv, 1);
foreach ($files as $file) {
$picture = file_get_contents($file);
$size = getimagesize($file);
// base64 encode бинарные данные, а затем разбить его на фрагменты в соответствии с семантикой RFC 2045
$base64 = chunk_split(base64_encode($picture));
echo '<img src="data:' . $size['mime'] . ';base64,' . "\n" . $base64 . '" ' . $size[3] . ' />', "\n";
}
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
.logo {
background: url("<?php echo base64_encode_image ('img/logo.png','png'); ?>") no-repeat right 5px;
}
<img src="<?php echo base64_encode_image ('img/logo.png','png'); ?>"/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question