|
|
Hello all,
I seem to be unable to output an image to the browser via a zf controller action. relevent code follows.
public function viewAction()
{ $this->_helper->layout->disableLayout();
$this->_helper->ViewRenderer->setNoRender();
if (!$id = $this->_getParam('id', false)) {
return $this->_helper->redirector('index');
}
$model = $this->getModel();
$image = $model->get($id);
$this->_response->setHeader('Content-Type', 'image/jpeg', true);
echo $image['data'];
}
As you can see, when the action is called (with a valid id parameter), i just want the browser to spit out the image. Instead, i'm getting no output at all.
I have using readfile() and pointing to a absolute filesystem path as well, also with no luck.
The data appears to be downloading - firebug shows the actual image sizes being downloaded
Does anyone have any thoughts?
Michael Crumm
|
|
Did you try with $this->getResponse()->setBody($image['data']); ?
I don't know if it will work but maybe will do the trick :)
Giuliano
Michael Crumm wrote
Hello all,
I seem to be unable to output an image to the browser via a zf controller
action. relevent code follows.
public function viewAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->ViewRenderer->setNoRender();
if (!$id = $this->_getParam('id', false)) {
return $this->_helper->redirector('index');
}
$model = $this->getModel();
$image = $model->get($id);
$this->_response->setHeader('Content-Type', 'image/jpeg', true);
echo $image['data'];
}
As you can see, when the action is called (with a valid id parameter), i
just want the browser to spit out the image. Instead, i'm getting no output
at all.
I have using readfile() and pointing to a absolute filesystem path as well,
also with no luck.
The data appears to be downloading - firebug shows the actual image sizes
being downloaded
Does anyone have any thoughts?
Michael Crumm
|
|
Giuliano, Thanks for the thought! Unfortunately, I tried that as well and still can't seem to get an image out. Here's my controller action: public function viewAction() { $this->_helper->layout->
disableLayout();
$logo = $this->_logo; $modifiedDateGM = gmdate('D, d M Y H:i:s', strtotime($logo['modified'])) . ' GMT'; $response = $this->getFrontController()->getResponse();
$response->setHeader('Last-Modified', $modifiedDateGM, true);
$response->setHeader('Content-Type', $logo['type'], true); $response->setHeader('Content-Length', $logo['size'], true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600, must-revalidate', true);
$response->setBody($logo['data']);
$response->sendResponse(); exit; } Seems like someone over in the zf-db list was having a similar issue - I'm having a similar discussion over there. Any more thoughts?
-Mike On Fri, Oct 24, 2008 at 8:29 AM, Giuliano Riccio <[hidden email]> wrote:
Did you try with $this->getResponse()->setBody($image['data']); ?
I don't know if it will work but maybe will do the trick :)
Giuliano
Michael Crumm wrote:
>
> Hello all,
> I seem to be unable to output an image to the browser via a zf controller
> action. relevent code follows.
>
> public function viewAction()
> {
> $this->_helper->layout->disableLayout();
> $this->_helper->ViewRenderer->setNoRender();
> if (!$id = $this->_getParam('id', false)) {
> return $this->_helper->redirector('index');
> }
>
> $model = $this->getModel();
> $image = $model->get($id);
>
> $this->_response->setHeader('Content-Type', 'image/jpeg', true);
> echo $image['data'];
> }
>
> As you can see, when the action is called (with a valid id parameter), i
> just want the browser to spit out the image. Instead, i'm getting no
> output
> at all.
>
> I have using readfile() and pointing to a absolute filesystem path as
> well,
> also with no luck.
>
> The data appears to be downloading - firebug shows the actual image sizes
> being downloaded
>
>
> Does anyone have any thoughts?
>
> Michael Crumm
>
>
--
View this message in context: http://www.nabble.com/Outputting-an-image-tp20132978p20150164.html
Sent from the Zend Framework mailing list archive at Nabble.com.
|
|
The following self-contained example works fine for me in my environment. Try it on yours, it may help to narrow the problem down further:
public function imgtestAction()
{
$this->_helper->layout->disableLayout();
$logo = file_get_contents(" http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png");
$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;
}
Michael Crumm wrote
Giuliano,
Thanks for the thought! Unfortunately, I tried that as well and still can't
seem to get an image out.
Here's my controller action:
public function viewAction()
{
$this->_helper->layout->disableLayout();
$logo = $this->_logo;
$modifiedDateGM = gmdate('D, d M Y H:i:s',
strtotime($logo['modified'])) . ' GMT';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Last-Modified', $modifiedDateGM, true);
$response->setHeader('Content-Type', $logo['type'], true);
$response->setHeader('Content-Length', $logo['size'], true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600,
must-revalidate', true);
$response->setBody($logo['data']);
$response->sendResponse();
exit;
}
Seems like someone over in the zf-db list was having a similar issue - I'm
having a similar discussion over there.
Any more thoughts?
-Mike
|
|
You'll see a significant performance improvement if you don't load the
files contents into PHP. The best way to do this is with an apache mod
called X-SendFile. ( http://tn123.ath.cx/mod_xsendfile/) but if you cant
install the module, then you'll want to use fpassthru(). Fopen/fread
and file_get_contents approaches have significant overhead and really
shouldn't be used.
Chris Martin wrote:
The following self-contained example works fine for me in my environment. Try
it on yours, it may help to narrow the problem down further:
public function imgtestAction()
{
$this->_helper->layout->disableLayout();
$logo =
file_get_contents("http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png");
$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;
}
Michael Crumm wrote:
Giuliano,
Thanks for the thought! Unfortunately, I tried that as well and still
can't
seem to get an image out.
Here's my controller action:
public function viewAction()
{
$this->_helper->layout->disableLayout();
$logo = $this->_logo;
$modifiedDateGM = gmdate('D, d M Y H:i:s',
strtotime($logo['modified'])) . ' GMT';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Last-Modified', $modifiedDateGM, true);
$response->setHeader('Content-Type', $logo['type'], true);
$response->setHeader('Content-Length', $logo['size'], true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600,
must-revalidate', true);
$response->setBody($logo['data']);
$response->sendResponse();
exit;
}
Seems like someone over in the zf-db list was having a similar issue - I'm
having a similar discussion over there.
Any more thoughts?
-Mike
--
Kevin McArthur
StormTide Digital Studios Inc.
Author of the recently published book, "Pro PHP"
http://www.stormtide.ca
|
|
@Chris-
Thanks for the help. After setting up a very minimal test environment, I was able to get that code snippet working as well. I'm now convinced that something I'm doing either in my bootstrap or my initialization plugin is causing the problem, but I'm still just not sure what yet.
@Kevin Thanks for the info. X-SendFile looks worth checking out. As far as fpassthru is concerned, I was attempting to use readfile() at first, but this also did not succeed. However, like I mentioned above, I'm sure the problem is not with propagating a response object, so readfile would probably work just as well if i can identify where the actual problem is.
Thanks everyone for the help. If I end up with a solution as to why this was not working, I will post it.
-Mike
|
|
You may want to use readfile() instead of fpassthru(), which saves you the fopen() call.On Sat, Oct 25, 2008 at 2:57 AM, Kevin McArthur <[hidden email]> wrote:
You'll see a significant performance improvement if you don't load the
files contents into PHP. The best way to do this is with an apache mod
called X-SendFile. ( http://tn123.ath.cx/mod_xsendfile/) but if you cant
install the module, then you'll want to use fpassthru(). Fopen/fread
and file_get_contents approaches have significant overhead and really
shouldn't be used.
Chris Martin wrote:
The following self-contained example works fine for me in my environment. Try
it on yours, it may help to narrow the problem down further:
public function imgtestAction()
{
$this->_helper->layout->disableLayout();
$logo =
file_get_contents("http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png");
$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;
}
Michael Crumm wrote:
Giuliano,
Thanks for the thought! Unfortunately, I tried that as well and still
can't
seem to get an image out.
Here's my controller action:
public function viewAction()
{
$this->_helper->layout->disableLayout();
$logo = $this->_logo;
$modifiedDateGM = gmdate('D, d M Y H:i:s',
strtotime($logo['modified'])) . ' GMT';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Last-Modified', $modifiedDateGM, true);
$response->setHeader('Content-Type', $logo['type'], true);
$response->setHeader('Content-Length', $logo['size'], true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600,
must-revalidate', true);
$response->setBody($logo['data']);
$response->sendResponse();
exit;
}
Seems like someone over in the zf-db list was having a similar issue - I'm
having a similar discussion over there.
Any more thoughts?
-Mike
--
Kevin McArthur
StormTide Digital Studios Inc.
Author of the recently published book, "Pro PHP"
http://www.stormtide.ca
-- Kind regards, -behzad
|
|
Hello again,
I just realized that the reply I thought i left here never made it to the list.
Quite some time ago, I realized my problem was whitespace following a closing tag in my Controller file.
Stupid mistake, but one that had me digging for solutions for quite a few days. Posting this here in the hopes that I can save someone else the same futile search for answers.
-Mike On Sat, Oct 25, 2008 at 4:18 PM, Behzad <[hidden email]> wrote:
You may want to use readfile() instead of fpassthru(), which saves you the fopen() call.On Sat, Oct 25, 2008 at 2:57 AM, Kevin McArthur <[hidden email]> wrote:
You'll see a significant performance improvement if you don't load the
files contents into PHP. The best way to do this is with an apache mod
called X-SendFile. ( http://tn123.ath.cx/mod_xsendfile/) but if you cant
install the module, then you'll want to use fpassthru(). Fopen/fread
and file_get_contents approaches have significant overhead and really
shouldn't be used.
Chris Martin wrote:
The following self-contained example works fine for me in my environment. Try
it on yours, it may help to narrow the problem down further:
public function imgtestAction()
{
$this->_helper->layout->disableLayout();
$logo =
file_get_contents("http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png");
$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;
}
Michael Crumm wrote:
Giuliano,
Thanks for the thought! Unfortunately, I tried that as well and still
can't
seem to get an image out.
Here's my controller action:
public function viewAction()
{
$this->_helper->layout->disableLayout();
$logo = $this->_logo;
$modifiedDateGM = gmdate('D, d M Y H:i:s',
strtotime($logo['modified'])) . ' GMT';
$response = $this->getFrontController()->getResponse();
$response->setHeader('Last-Modified', $modifiedDateGM, true);
$response->setHeader('Content-Type', $logo['type'], true);
$response->setHeader('Content-Length', $logo['size'], true);
$response->setHeader('Content-Transfer-Encoding', 'binary', true);
$response->setHeader('Cache-Control', 'max-age=3600,
must-revalidate', true);
$response->setBody($logo['data']);
$response->sendResponse();
exit;
}
Seems like someone over in the zf-db list was having a similar issue - I'm
having a similar discussion over there.
Any more thoughts?
-Mike
--
Kevin McArthur
StormTide Digital Studios Inc.
Author of the recently published book, "Pro PHP"
http://www.stormtide.ca
-- Kind regards, -behzad
|
|
CONTENTS DELETED
The author has deleted this message.
|
|
This post has NOT been accepted by the mailing list yet.
It only works for png images. not for jpegs
|
|