Base64 clipping images from database PHP

2019-08-19 23:26发布

问题:

I am making a website that displays games from a database made in MySQL workbench. I want to have images appear next to the relevant game title of said game. The problem I am having is that I am inserting these images into my database using BLOB and converting them to base64 within the PHP. When I do this and run it, the images come out clipped.

This gets the information from the table that I created in MySQL

function find_all_games(){
    global $db; //Connects to database
    $sql = 'SELECT * FROM game';
    $result = mysqli_query($db, $sql);
    return $result;
}

This is the table that pulls the information that I want from the database and converts the blob to a base64 image, but the images appear clipped.

'<?php while($game = mysqli_fetch_assoc($get_all_games)) {?>
<table>
    <tr>
      <td><?php
                echo '<img src="data:image/jpeg;base64,'.base64_encode($game['image']).' "height="301" width="220" />'; ?>
      </td>
    </tr>
    <tr>
      <td id="title"><?php echo $game['title']; ?></td>
    </tr>
    <tr>
      <td class="title">Released:</td>
      <td class="data"><?php echo $game['release_date']; ?></td>
    </tr>
    <tr>
      <td class="title">Publisher:</td>
      <td class="data"><?php echo $game['publisher']; ?></td>
    </tr>
</table>

'

The image should appear just fine but comes out with a white clip on the bottom that changes for each image even though they are all the same size in pixels.

Evidence that the images display, but are clipped

回答1:

For anyone who had the same issue as me: Courtesy of Jeff (https://stackoverflow.com/users/2417483/jeff), I was just being silly, and it turned out all I needed to do was make it a MEDIUMBLOB or LONGBLOB depending on the file sizes of your images.