PHP echo jpeg from MYSQL into an HTML page

StevenWin

I want to output an image that I've stored in MYSQL with file_get_contents("http://www.example.com/img.jpg") in an HTML page. I have the following code but I'm stuck as to how to make this work. Right now, it displays gibberish or if I use header, a broken image. I'm using codeigniter:

Model:

public function img_get() {
        $query = $this->db->query('SELECT * FROM imgtable');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

Controller:

class Site extends CI_Controller {

public function index() {
    $this->load->model('img_model');

    $data['row'] = $this->img_model->img_get();

    $this->load->view('home_view', $data);
}

View:

<html>
<head>
<title>Homepage</title>
</head>
<body>

<h3>Insert the image url</h3>
<br>

<?php 
    foreach ($row as $r) {
        echo $r->img . "<br />";
    }

?>
</body>
</html>
dev7

You are trying to display BLOB data:

displaying an image stored in a mysql blob

Show a BLOB image PHP MySQL along with other data

How to display an BLOB image stored in MySql database?

The trick is using:

<img src="data:image/jpeg;base64,' . base64_encode($r->img) . '">

Credit goes to all the other questions and answers on this site :)

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related