V
V
Vitaly the Wise2017-03-14 20:55:16
PHP
Vitaly the Wise, 2017-03-14 20:55:16

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

2 answer(s)
S
Shamsudin Serderov, 2017-03-14
@Steein

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";
}

//This code is from official PHP.NET website
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'); ?>"/>

S
Sasha, 2017-03-14
@userAlexander

Vitaliy the Wise try on JS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question