1

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?

4
  • You may need to increase the mine head of the appropriate information Commented Nov 20, 2012 at 9:07
  • 1
    Are you storing the raw binary of the image? Commented Nov 20, 2012 at 9:09
  • yaa, i am using blob function Commented Nov 20, 2012 at 9:10
  • When you post code online, please make sure it is formatted nicely. I've formatted your code, please accept my edit. Commented Nov 20, 2012 at 9:12

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't $result['image'] be html-escaped?
This is the best and the easiest solution I've found ever. Thank you:)
2

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');

1 Comment

I suppose it's worth mentioning that in this case the output should also NOT contain any HTML code.
2

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).

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.