mans
mans

Reputation: 1087

php mysql image not display properly

I have following code for get image from my database table field.

public function getStudentses() {
    $students_data = array();       

    $query = $this->db->query(
        "SELECT * FROM " . DB_PREFIX . "students 
        WHERE customer_id = '" . (int)$this->customer->getId() . "'");   

    foreach ($query->rows as $result) {             
        $students_data[$result['students_id']] = array(
            'students_id'        => $result['students_id'],
            'firstname'          => $result['firstname'],
            'filename'           => $result['filename'],
            'image'              => $result['image'],
        );
    }       
    return $students_data;
}

This is my HTML code for display image:

<img src="<?php echo $result['image']; ?>" />

but when I render my page the image is not display properly its like some symbols like

s4�.���N����萗�p�A@4pbr��]�����F�G�>�v��W

Why its like this? How can I fix my error?

Upvotes: 1

Views: 3091

Answers (5)

echo_Me
echo_Me

Reputation: 37253

if the image is stored by this code

  s4�.���N����萗�p�A@4pbr��]�����F�G�>�v��W

its because you are just copying and pasting this code from database. be sure where it stored the the name/id of the image and the path of the image and get the image by its path and id/name or follow what Prasanth said to you. this my help you How to retrieve images from MySQL database and display in an html tag

Upvotes: 0

Vyktor
Vyktor

Reputation: 21007

Most common practice for this (what I've seen) is to build html:

<img src="image.png.php?student_id=<?php echo $result['students_id']; ?>" />

And then image.png.php:

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "students "
            "WHERE students_id = '" .(int)$request->getRequestParam('sudent_id') . "'"); 

// And now display raw content as image
$student = reset( $query->rows);
if( !$student){
   header('HTTP/1.0 404 Not Found');
   die( 'Blah blah... Blah blah blah');
}

header( 'Content-Type: image/jpeg');
echo $student['image'];

This way you'll be able to create links to images usable from whatever place you'll want (without any database access required on remote site).

Upvotes: 2

Prasanth
Prasanth

Reputation: 5258

Modern browsers have this support for using raw image data as source.

You can try to use something like:

<img src="data:image/png;<?php echo $result['image']; ?>" />

You should encode the image using base64 and then output something like:

<img src="data:image/png;base64,<?php echo base64_encode($result['image']); ?>" />

I have use image/png for example. If your image is something else, you should use corresponding encType.

Just make sure to read this and this.

Upvotes: 4

sberry
sberry

Reputation: 132088

Assuming you are storing the raw binary of the image, you can do this:

'image'              => base64_encode($result['image']),

Then in the html:

<img src="data:image/png;base64,<?php echo $result['image']; ?>" />

This assumes the image is a png. If not, change the mime type accordingly.

Upvotes: 1

Sandeep Bansal
Sandeep Bansal

Reputation: 6394

You will most likely need to change the header to an image

http://php.net/manual/en/function.header.php

header('Content-Type: image/jpeg');

Upvotes: 2

Related Questions