简体   繁体   English

base64解码不起作用

[英]base64 decoding doesn't work

I am trying to display an image from a mysql database in a HTML table with PHP. 我正在尝试使用PHP在HTML表中显示来自mysql数据库的图像。 As I stored the images as BLOBs in the DB, I used the following base64 functions to convert them from binary and display them correctly: 当我将图像作为BLOB存储在数据库中时,我使用以下base64函数将其从二进制转换并正确显示:

$enc=base64_encode($image);
$dec=base64_decode($enc);
echo $dec;

I don't know why images continue displaying like this: 我不知道为什么图像继续像这样显示: 在此处输入图片说明

Here is the entire PHP code: 这是完整的PHP代码:

<?php
    // Konexio lokala sortu
    $sql = mysql_connect('localhost', 'root', '') or die(mysql_error());
    // Konexioa lokala egiaztatu
    mysql_select_db("quiz") or die(mysql_error());
    $sql="SELECT * FROM `Erabiltzaile`";
    $records = mysql_query($sql);
    if (! $records)
    {
         die('Errorea: ' . mysql_error());
    }
    mysql_close();
?>

<html>
<head>
    <title>Erabiltzaileak</title>
</head>
<body>
    <table width="800" border="1" cellpadding="1" cellspacing="1">
            <tr>
                <th>Izena</th>
                <th>E-posta</th>
                <th>Pasahitza</th>
                <th>Telefonoa</th>
                <th>Espezialitatea</th>
                <th>Interesak</th>
                <th>Argazkia</th>
            <tr>
        <?php
            while($erabiltzaile=mysql_fetch_assoc($records)) {
                echo "<td>".$erabiltzaile['Izena']."</td>";
                echo "<td>".$erabiltzaile['Eposta']."</td>";
                echo "<td>".$erabiltzaile['Pasahitza']."</td>";
                echo "<td>".$erabiltzaile['Telefonoa']."</td>";
                echo "<td>".$erabiltzaile['Espezialitatea']."</td>";
                echo "<td>".$erabiltzaile['Interesak']."</td>";
                $image = $erabiltzaile['Argazkia'];
                echo "<td>";
                $enc=base64_encode($image);
                $dec=base64_decode($enc);
                echo $dec;
                echo "</td>";
                echo "</tr>";
            }
        ?>

    </table>
</body>

It's my first question here, hope the format of the question is good. 这是我的第一个问题,希望问题的格式正确。 Sorry about the words in Basque(Argazkia means Picture). 对不起,巴斯克语中的单词(Argazkia表示图片)。

Put the base64 code in data: URI in an <img> tag: 将base64代码放入<img>标记中的data: URI中:

$type = getimagesizefromstring($image);
$enc = base64_encode($image);
echo "<td>";
echo "<img src='data:" . $type['mime'] . "';base64," . $enc . "'>";
echo "</td>";

getimagesizefromstring is available in PHP 5.4+. getimagesizefromstring在PHP 5.4+中可用。 If you have an older version, you can find a polyfill here . 如果您使用的是旧版本,则可以在此处找到一个polyfill。

I finally made it work with this: 我终于做到了这一点:

$enc = base64_encode($image);
echo "<td>";
echo '<img src="data:image/;base64,' . $enc . '"/>';
echo "</td>";

Without $type = getimagesize($image); 没有$type = getimagesize($image);

Thank you for all the comments and for the answer Barmar! 感谢您的所有评论和Barmar的答案! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM