We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

showing image not working

Hi all,

i'm trying to output an image in a controller action. However i can't get it to work. If i run the same code in a regular php file it runs fine, but when i run it in a controller i can't get it to work.

    /**
     * @param $systemName
     */
    public function overviewAction($systemName)
    {

        $tournament = Tournament::findFirstBySystemName($systemName);

        if ($tournament) {

            if ($image = (string)$tournament->challonge->getOverviewImage()) {
                $this->response->resetHeaders();
                $this->response->setHeader('Content-Type', 'image/png');

                $tmpFile = $this->config->application->cacheDir . $systemName . '.png';
                file_put_contents($tmpFile, file_get_contents($image));

                $original = new \Phalcon\Image\Adapter\GD($tmpFile);
                $original->crop($original->getWidth(), $original->getHeight() - 150);
                $original->save();

                readfile($tmpFile);
            }
        }

        $this->view->setRenderLevel(View::LEVEL_NO_RENDER);
    }

I have tried it with a die at the end, setting headers in the normal fashion, different ways of outputting the file, removing the renderLevel thing etc.

Am i missing something?

Thanks!



8.1k

Check permissions of files and folders. Example : when you run script in console - script work and create/change files as "user" when you run on server - work wtih files and folders are via user (and group) "http" or "www-data" or other (see your server setup)

Nothing to do with permissions :)

If i disable the bootstrapper and run the same code it outputs the file in the browser.



8.1k

readfile($tmpFile);

Why you use this?

Check the PHP documentation for readfile()

readfile($tmpFile);

Why you use this?

Is the tmpfile being created?

What output are you actually seeing?

Open the browser dev tools and see what headers are being sent.

edited May '14

Everything works well, like i said using plain php everything works. Only doing it in a controller fails somehow.

All headers:

Request URL:https://intranet.dev/tournament/overview/starcraft2_2014
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,nl;q=0.8,en-US;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=220287936.1658737633.1399324942.xxx.1399324942.1; __utmz=220287936.1399324942.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=02jcb374o0kkuk9tmiimcpaoe7
Host:intranet.dev
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:image/png
Date:Thu, 22 May 2014 19:18:37 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.6 (Debian)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.6-1

In one of your if statements, you have "challonge" - should that be "challenge"?

Try simplifying it - just for testing. Don't use $this->response() to set the headers, just use PHP's header() function. Also, just call readfile() on the image, don't resize and try to output the thumbnail.

Btw this is still an issue. Outputting images in the controller simply does not work.



15.2k
edited May '15

It is a late reply, but I wonder why do you use readfile whereas you could just render it, something like this


$this->response->setHeader("Content-Type", $original->getMime());
$this->response->setContent($original->render());
$this->response->send();
return;

Btw this is still an issue. Outputting images in the controller simply does not work.

It was not about the php code. Render does exactly the same thing. It was not outputting the image regardless of what i used.

It is a late reply, but I wonder why do you use readfile whereas you could just render it, something like this


$this->response->setHeader("Content-Type", $original->getMime());
$this->response->setContent($original->render());
$this->response->send();
return;

Btw this is still an issue. Outputting images in the controller simply does not work.



43.9k

Hi,

I'm using


        $response = new Phalcon\Http\Response();
        $response->setContentType('image/jpeg');
        $response->setContent(file_get_contents($file));
        return $response;

This worked for me.

<?php
class IndexController extends ControllerBase
{
    public function indexAction()
    {
        header("Content-type: image/jpg");
        $img= "https://www.mundoperro.net/wp-content/uploads/Nombres-de-perro-con-la-letra-G.jpg";
        readfile($img);
        return false;
    }
}

Otherwise I dont know if there is another way to do this with Phalcon\Http\Response, i was trying with no success. Please let me know. Saludos!