mubashermubi
mubashermubi

Reputation: 9256

Displaying image directly in browser using zend framework

I am trying to display image using Zend Framework in directly browser. I can display successfully but only PNG format.

Here is my code which works fine when I try to access the PNG image and it displays the image in browser.

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

Again: the above code works fine and displays the image in browser correctly but when I try the jpeg/jpg image it give me message:

The image “http://localhost/zfproject/activity/share/image” cannot be displayed because it contains errors.

Here is the code I used for JPEG/JPG image type

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

I do not know may be I am doing something wrong, i have tried the type 'image/jpeg' also but it doesn't work.

Please advise.

Thank you in advance.

Upvotes: 1

Views: 481

Answers (1)

MWT
MWT

Reputation: 172

Try this:

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

Upvotes: 1

Related Questions