0

I'm trying to display an image from a base64 encoded format. I have wrote the following php code for that. But it only shows a blank image of size 20px x 20px. How to correct this snippet?

<?php
$username = "root";
$password = "";
$host = "localhost";

$connector = mysql_connect($host, $username, $password) or die("Unable to connect");

$selected = mysql_select_db("postman", $connector) or die("Unable to connect");

//execute the SQL query and return records
$result = mysql_query("SELECT image FROM mail ");
?>

this is the connectivity code and below is the table created to show images within that table.......

<table border="2" id="tblData"  bgcolor="#ACAAFC"    align="center">
<thead>
    <tr >
        <th><h4>IMAGE</h4></th>
    </tr>
</thead>

<tbody>
</tbody>

here is the basic running in the looping body where the image is being fetched and displayed but the image is not being shown on the execution of the code, using the function base64_decode. so as to decode the image string array.

<?php
while ($row = mysql_fetch_assoc($result)) {

    echo "<tr>"
    $row['image'];
    $str = implode(';', $row);
    $imgstr = "image/jpeg;base64,$str";
    $new_data = explode(";", $imgstr);
    $type = $new_data[0];
    $data = explode(",", $new_data[1]);
    header("Content-type:" . $type);

    echo "<td>" . base64_decode($data[1]) . "</td>";

    echo "</tr>";
    "</table>";
}
?>
    </tbody>
  </table>
</form>
<?php mysql_close($connector); ?>
Raja Gopal
  • 1,845
  • 1
  • 17
  • 34
  • 1
    Please refactor your code to use the [MySQLi](http://php.net/mysqli) or [PDO](http://php.net/manual/en/ref.pdo-mysql.php) extension instead. The MySQL extension has been [deprecated since 2013](http://php.net/manual/en/intro.mysql.php) and has been dropped in PHP7 that was released earlier this month. So this code will not work on PHP7. Also see [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Oldskool Dec 30 '15 at 09:25
  • You cant send any header after any output. Why bother exploding the `$imgstr`? Just put it in tag – frz3993 Dec 30 '15 at 09:25
  • Please format your code properly as it contains many (unnecessary) spaces and newlines. – Hecke29 Dec 30 '15 at 09:36

2 Answers2

0

Assuming you're using the PDO extension, the cose should be like this (show_image.php can be the name of the file):

<?php
$where = dirname(__FILE__);   
include($where . "/config/db.php"); // here you'll have the param to connect to db

$query = "SELECT image FROM mail "; 
$stmt = $dbh->query($query);
if($stmt->rowCount() > 0)
{ 
    $data = $stmt->fetch(PDO::FETCH_ASSOC); //FETCH ASSOC IS NOT NEEDED, but it should works anyway
    $image = $data['image'];
    $type = $data['type'];
    header("Content-type: ".$type);
    echo $image;
}
else 
{
    // show another (default) image maybe 
}
?>

And when you need to display the image you can do that:

<img src="show_image.php" />

If you need to pass param to the show_image.php file, for example to get an image from a specific id, you should do that:

<img src="show_image.php?id=1 /> // Assuming the id you need is "1"

And the db.php file should be like this:

<?php
$user = "your username";
$password = "password";
$host = "localhost or your host anyway";
$dbname = "postman";
$dbh = null;
try {
    $dbh = new PDO ("mysql:host=" . $host . ";dbname=" . $dbname, $user, $password);
    } catch (PDOException $ex) {
    echo $ex->getMessage();
    }
?>

PDO documentation

  • what for pdo extension stands and i cant provide again and again src of an image because i m using an array of image string encoded in base 64 from database – karishma chhangani Dec 30 '15 at 11:29
  • maybe you can use a for cycle and pass the id of the image you want? You can get the last id, then made a for that work to the last id value and pass the id in the src (sorry if i haven't understood, i'm not good with english) – Giuseppe De Paola Dec 30 '15 at 11:43
  • not dats fyn i just want to know how to let the code run!!.. dont be sorry dats perfectly fyn, dat can happen with me aswell..... – karishma chhangani Dec 31 '15 at 04:43
0

The problem you have is that you're mixing up the code serving the HTML page and the code serving the image.

Traditionally, you would have needed two scripts:

  • one that serves the HTML: it gets the list of images, and builds the HTML with the table listing the images, but not the images themselves, only including a link to the image (probably with the id of the image)
  • another that serves the image itself, fetching its data from the database based on the id

The alternative is to use data: scheme URLs. This means including the images in the HTML itself, with tags of the form:

<img src="data:image/png;base64,<base 64 encoded image data here>" alt="whatever">

As your images are already Base 64-encoded, you wouldn't need to decode/re-encode them, of course.

jcaron
  • 17,302
  • 6
  • 32
  • 46