准备好以下代码:
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$image = ...
$this->getResponse()->setHeader('Content-type', 'image/png');
imagepng($image);
imagedestroy($image);
这会在服务器上生成损坏的图像,但在localhost上正常工作。
即使该代码在行动中也会产生损坏的图像:
header("Content-Type: image/png");
echo file_get_contents('.../test.png');
我也尝试过这两行:
答案 0 :(得分:2)
public function imgAction()
{
$this->_helper->layout->disableLayout();
$img = file_get_contents("Your Image");
$type = 'image/png';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Content-Type', $type, true);
$response->setHeader('Content-Length', count($img), true);
$response->setBody($img);
$response->sendResponse();
exit;
}