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

mPDF and image from controller

I can't get mPDF to load some images I'm serving from a controller. It seems that mPDF uses HTTP/1.0 requests and it gets back a 302 redirect.

Apache access logs from mPDF; /img/missing-image-640x360.png loads directly from the filesystem and works; /products/showImage/ is my controller action to output images and it sends a 302.

127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /img/missing-image-640x360.png HTTP/1.0" 200 5407
127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /products/showImage/1454838253_0d52e1e46e0de6dfa2aa01490f986ff6.png HTTP/1.0" 302 -
127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /index HTTP/1.0" 200 6439
127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /products/showImage/1454838253_0d52e1e46e0de6dfa2aa01490f986ff6.png HTTP/1.0" 302 -
127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /index HTTP/1.0" 200 6439
127.0.0.1 - - [09/Feb/2016:19:56:30 +0200] "GET /products/showImage/1454838253_0d52e1e46e0de6dfa2aa01490f986ff6.png HTTP/1.1" 302 -

Loading the same content as HTML in browser works fine:

127.0.0.1 - - [09/Feb/2016:19:57:13 +0200] "GET /img/missing-image-640x360.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Feb/2016:19:57:13 +0200] "GET /products/showImage/1454838253_0d52e1e46e0de6dfa2aa01490f986ff6.png HTTP/1.1" 200 46118

The showImage action does this:

$image = new \Phalcon\Image\Adapter\GD($path);
$response = new \Phalcon\Http\Response();
$response->setContentType($image->getMime());
$response->setContent($image->render());
return $response;

Can I make phalcon reply using version 1.0? Or any other suggestion welcomed.

On a second thought, this might be due to the security redirects and ACL. mPDF probably runs like an anautheticated user. Going to check into it.



2.7k
Accepted
answer
edited Feb '16

Indeed the redirect was caused by my ACL. Solved the problem by encoding the image and using it as src for the img tag.

$image = new \Phalcon\Image\Adapter\GD($path);
$base64 = 'data:' . $image->getMime() . ';base64,' . base64_encode($image->render());