Results 1 to 7 of 7

Thread: Return image from action controller

  1. #1
    joatack is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default Return image from action controller

    Hi all,

    I tried from long time to return an image content from an action :

    HTML :
    Code:
    <img src="user/loadavatar" />
    Action :
    Code:
    class ImageController extends Zend_Controller_Action 
    {
     
        function imageAction()
        {
    	$this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
    		
        	$image = file_get_contents("test.png");
      	
    	$this->getResponse()->clearBody ();
    	$this->getResponse()->setHeader('Content-Type', 'image/png');
    	$this->getResponse()->setBody($image); 
        }
    }
    When I called my action : http://localhost/skybook/image/image
    -> Firefox display : "Image cannot be displayed because it contains errors"

    If I try to put in a simple file test.php :
    Code:
    <?php
    header("Content-Type: image/png");
    echo file_get_contents('test.png');
    ?>
    It works !!

    I think something is added to my content when I want to pass via an action.....

    Help !!!

    Jo

  2. #2
    rbroen is offline Junior Member
    Join Date
    Jun 2010
    Location
    Roermond, The Netherlands
    Posts
    29

    Default

    Your ImageController action worked on my machine, the only thing I changed was the image; I provided the absolute path to a png on my server. The image was provided when I requested
    Code:
    http://thedomain.com/controllername/image
    Last edited by rbroen; 06-28-2010 at 09:24 AM.

  3. #3
    joatack is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default

    :s

    What can I do now, I am sure that the image I readed correctly because I can see the content but impossible to view the image....Do you think my bootstraap could be the cause ? (it could add some stupid things)......

    Thx

  4. #4
    rbroen is offline Junior Member
    Join Date
    Jun 2010
    Location
    Roermond, The Netherlands
    Posts
    29

    Default

    try a fresh zf project (like I did)
    try a different png file
    take a look at your apache error.log (an post it back here)

  5. #5
    joatack is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default

    On clean project & different image & no error in apache log :
    Code:
    class ImageController extends Zend_Controller_Action 
    {
     
        function imageAction()
        {
    		$this->_helper->layout->disableLayout();
    	    $this->_helper->viewRenderer->setNoRender();
    	    
    		$image = file_get_contents("test.jpg");
    	  	
    		$this->getResponse()->clearBody ();
    		$this->getResponse()->clearAllHeaders();
    		$this->getResponse()->setHeader('Content-Type', 'image/jpg',true);
    		$this->getResponse()->setHeader('Cache-Control', 'public');
    		$this->getResponse()->setBody($image); 
        }
    }
    Firefox put : http://localhost/test/image/image
    IE put the content of the image, if I tried to save the content in jpg image file I cannot open it.....

    Why it works if I call a simple php file but via an action it doesn't ?
    Last edited by joatack; 06-29-2010 at 09:36 AM.

  6. #6
    joatack is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default

    I compared the original image and the generated image and there is 2 differences :

    -> a small hexa code is added at the begining of content (0D 0A.......)
    -> a smal hexa code is removed at the end of content (........00 3B
    )

    Why when I use an action, the content of the image is modified ?

  7. #7
    joatack is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default

    Resolved, it was the empty line in my bootstraap, index and helpers.....

    Don't use the end tag (?>) and remove all empty line at the end of files !

    thx for help !

Similar Threads

  1. Not able to create a Controller or Action
    By maskme in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 04-08-2010, 05:22 PM
  2. zend controller front dnt return baseurl in bootstrap ??
    By lion21 in forum General Q&A on Zend Framework
    Replies: 2
    Last Post: 03-12-2010, 06:10 PM
  3. Something between bootstrap and action controller?
    By guyinva in forum Model-View-Controller (MVC)
    Replies: 6
    Last Post: 10-23-2009, 06:27 PM
  4. Use controller/action output as a string in another action
    By Snuyt in forum Model-View-Controller (MVC)
    Replies: 2
    Last Post: 04-26-2008, 11:41 PM
  5. Subclassing the Action Controller
    By ehalber in forum General Q&A on Zend Framework
    Replies: 8
    Last Post: 03-18-2008, 05:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •