1

I have 1 column containing binary format data. I want to display that Image on browser and I am useing codeignater

My Model Code

function GetLogoById($Id)
    {
        $this->load->database();
        $query =  $this->db->query( "EXEC GetLogoById '$Id'" );
        $result = array();
        foreach ($query->result() as $row)
        {
            $result[] = array("Logo"=> $row->Logo);
        }
           return $result;
    }

My Controller Code:

public function GetLogoById()
    {
    $this->load->model('MyModel');
    $result = $this->MyModel->GetLogoById($Id);
    $result = base64_decode($result);

    echo $result;
}

It return Blank in browse. What I am missing....

1
  • How did you store image in database? By encoding in base64 or storing original text string of uploaded image file? If you don't use correct encoding and decoding method, image will be displayed as text in browser. Commented Oct 4, 2013 at 10:21

3 Answers 3

1

try with this.

$this->load->model('GetLogoById');

   $result = $this->mymodel->GetLogoById($CarrierId);

   header('Content-type: image/png');
   echo  $result[0]['Logo'];
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

public function GetLogoById()
{
$this->load->model('MyModel');
$result = $this->MyModel->GetLogoById($Id);
//$result = base64_decode($result);
header('Content-Type: image/jpeg');  #set a header
echo $result;
}

5 Comments

becouse I am Getting Binarydata from database And that is not well formated so
No dont use base64_decode. Use it only when you have encoded it via base64_encode. See updated answer.
Hi..i used ur latest code...but by this i am getting error....image can not be display bcoz it's contain error....so, do u want my binary data?
May be there is a problem with your binary data. How you saved it in database. But this is the way to do it. Try saving the data once more.
0
<?php

public function GetLogoById() {

   $this->load->model('MyModel');

   // Get your result
   $result = $this->MyModel->GetLogoById($Id);

   // Do base64 decode if you encoded it.
   $result = base64_decode($result);

   // Set header according to the image type. 
   // if the image is jpg use 'image/jpeg'
   // if the image is png use 'image/png' 
   // and so on ...
   header('Content-Type: image/jpeg');

   // Only echo the exact value. Keep in mind that no other 
   // space or any other values are echoed
   echo $result;

   // Also make sure that there must not be any spaces or other values before or after the php tags
}

?>

// This is a sample.

<?php

   $image_data = file_get_contents( __DIR__ .  '/dino_babu.jpg');

   $image_token = base64_encode($image_data); // Save this token to database

   $image_data_decode = base64_decode($image_token); // retrieve the token & decode it

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

   echo $image_data_decode; // Print the data

?>

2 Comments

are you sure that your encoded value in database is correct and properly retrieved ?
it's storing byte of array..and i am just fetch record nothing else...so, i dont think so i am making any mistake on that.

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.