【问题标题】:Php - how can i echo this variable as jpegPhp - 我如何将此变量回显为 jpeg
【发布时间】:2014-01-31 07:13:49
【问题描述】:

在 Php(zend 框架 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

【问题讨论】:

  • 在 src 中获取图片并在 zend 中获取预览操作?你确定你发布的方法被调用了吗?
  • 在调用getpreviewAction() 之前是否打印了任何内容?任何响应都会强制发送标头,因此在发送标头后更改标头将无济于事(如您在示例中所见)。
  • 粘贴错误。它应该是 getpreview 并且仍然是同样的问题。
  • @styfle: 从 _helper 和 _response 没有打印这些是开始
  • 这样的东西怎么样:php.net/manual/de/function.imagejpeg.php

标签: php jquery zend-framework


【解决方案1】:

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

另外,如果你只是exit,那么setHeader 可能不会工作。所以在echo之前先做header('Content-Type: image/jpeg')

编辑:

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

【讨论】:

  • 像您提到的那样尝试过,但仍然无法正常工作。例如: echo "data:image/jpeg;base64," 。 base64_decode($raw);或回显 base64_decode($raw);或标题(.. 仍然没有得到 JPEG
  • 检查编辑。假设 $result[0]['preview'] 是 base64 编码的字符串,它应该可以正常工作。
  • 是的 - 它是一个 base64 编码的字符串,但就像你展示的一样,我已经应用了,但它仍然无法正常工作。我的字符串是“data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAA.. long..”我也试过把它分成两部分,比如 data:image/jpeg;base64,另一部分全部随机加密值,然后也与 base64_decode 这两个部分一起使用。但仍然没有运气。
  • 删除data:image/jpeg;base64,然后试试看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多