This worked for me.

function indexAction() {

// disable view and layout
Zend_Layout::getMvcInstance()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
                
// setup realpath to image
$path_to_image = $_SERVER['DOCUMENT_ROOT'].'/images/EQ2_000192.jpg';

// set headers
header("Content-Type: image/jpeg");
                
// load image
$img = imagecreatefromjpeg($path_to_image);
// adjust quality
imagejpeg($img, NULL ,75);
// free memory used
imagedestroy($img); 

}

Instead of using passed params I just tested with a hardcoded image.

Maybe add in a validate that the image exists, if using params as this other
way could lead to potential code injection with out validation.
//  $img_dir = 'some dir';
//  $image = $this->_getParam('image');
//  $path_to_image = $img_dir . $image;

Also removed the exit as i've disabled the view rendering and the layout
rendering. 

Terre

-----Original Message-----
From: unlex [mailto:dmitr...@yandex.ru] 
Sent: Wednesday, June 17, 2009 5:37 AM
To: fw-general@lists.zend.com
Subject: [fw-general] Problem with raw image output


Hi,
I have the following problem. i need to output images thumbnails without
saving them to the disk. in other words I need to output raw image directly
to the browser. It's not a difficult task and I've done this many times BUT
not using ZF. But here I have a problem which I can't solve for several
days.

For example, I have the following URL

http://myhost/mymodule/mycontroller/...0617112012.JPG

which should output the thumbnail of the image.

(To simplify the example let's output just this image without thumbnailing
it.)

So I have the following code in myactionAction():
Code:

public function myactionAction()
{
  $this->_helper->viewRenderer->setNoRender();
  $img_dir = 'some dir';
  $image = $this->_getParam('image');
  $path_to_image = $img_dir . $image;
  $img = imagecreatefromjpeg($path_to_image);
  header('Content-type: image/jpeg');
  imagejpeg($img,'',75);
  exit;
}

So, unfortunately, this doesn't works. Browser outputs nothing.
If we don't send a header ( header('Content-type: image/jpeg') browser
outputs image in binary format, so there is not a problem with GD or image
creation. Moreover - if we write like that

imagejpeg($img,'temp.jpg',75);

we successefully get our image in temp.jpg. So saving works fine while
output doesn't work.

Can smb explain why I have such a strange problem?
Thanks in advance...
--
View this message in context:
http://www.nabble.com/Problem-with-raw-image-output-tp24070121p24070121.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to