Php - 如何将此变量作为jpeg回显

时间:2014-01-31 07:13:50

标签: php jquery zend-framework

在Php(zend framework 1.x)中,我打印RAW值data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAA....。并尝试在

中预览它

但是当加载JPEG作为链接失败时,内容类型:image / html而不是image / jpeg。

在PHP中,我将内容类型设置为image / jpeg但不适用。怎么解决?

  public function getpreviewAction() {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    $this->_response->setHeader('Content-Type', 'image/jpeg');

    $post = $this->getRequest()->getQuery();
    $data = (object) $post;
    $this->db = Application_Model_Db::db_load();

    $sql = "select *from sh_av_profile where
              username='{$data->username}'
        limit 1";

    $result = $this->db->fetchAll($sql);
    if (count($result) > 0) {
      //echo "<img src={$result[0]['preview']} />";
      echo $result[0]['preview'];         
    }    

    exit;
  }  

JS:

  function previewRender() {
    var root = $('img');
    //root.attr('src',lasturl +"?"+ Date.parse(new Date().toString()) );      

    root.attr('src', 'http://pbx/ajax/getpreview?username=T1');      
  }

回复标题:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:18844
Content-Type:text/html
Date:Fri, 31 Jan 2014 07:06:14 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=70
Pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)
Vary:Accept-Encoding
X-Powered-By:PHP/5.4.6-1ubuntu1.2

1 个答案:

答案 0 :(得分:1)

我认为您需要先base64_decode然后echo变量。

另外,如果你只是exit,那么setHeader可能无法正常工作。因此,请header('Content-Type: image/jpeg')之前echo

编辑:

header('Content-Type: image/jpeg');
echo base64_decode( $result[0]['preview'] );
exit();