如何在php中从二进制数据中获取浏览器上的图像

时间:2013-10-04 09:35:47

标签: php codeigniter codeigniter-2

我有1列包含二进制格式数据。 我想在浏览器上显示该图像,我正在使用codeignater

我的型号代码

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;
    }

我的控制器代码:

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

    echo $result;
}

在浏览中返回Blank。我错过了什么....

3 个答案:

答案 0 :(得分:1)

试试这个。

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

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

   header('Content-type: image/png');
   echo  $result[0]['Logo'];

答案 1 :(得分:0)

试试这个:

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;
}

答案 2 :(得分: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
}

?>

//这是一个示例。

<?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

?>