我试图在直接浏览器中使用Zend Framework显示图像。我可以成功显示,但只能显示PNG格式。
这是我的代码在我尝试访问PNG图像并且在浏览器中显示图像时工作正常。
// disable the layout
$this->_helper->layout->disableLayout();
$file = 'http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png';
$logo = file_get_contents($file);
$type = 'image/png';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Content-Type', $type, true);
$response->setHeader('Content-Length', count($logo), true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
$response->setBody($logo);
$response->sendResponse();
exit;
再说一遍:上面的代码运行正常并且在浏览器中正确显示图像,但是当我尝试jpeg / jpg图像时,它会给我留言:
无法显示图片“http://localhost/zfproject/activity/share/image”,因为它包含错误。
以下是我用于JPEG / JPG图像类型的代码
// disable the view ... and perhaps the layout
$this->_helper->layout->disableLayout();
$file = 'http://localhost/zfproject/public/user/15/0015_19aa.jpg?c=5090';
$logo = file_get_contents($file);
$type = 'image/jpg';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Content-Type', $type, true);
$response->setHeader('Content-Length', count($logo), true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
$response->setBody($logo);
$response->sendResponse();
exit;
我不知道可能是我做错了什么,我也尝试过类型'image / jpeg',但它不起作用。
请指教。
提前谢谢你。
答案 0 :(得分:1)
试试这个:
$this->_helper->layout->disableLayout();
$file = 'http://localhost/zfproject/public/user/15/0015_19aa.jpg?c=5090';
$logo = file_get_contents($file);
$type = 'image/jpg';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Content-Type', $type, true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
$response->setBody($logo);
$response->sendResponse();
exit;