renderPartial doesn't render 'Content-type: image/png'?!

Hi guys,

I’m playing around with the gd library, and I have this code:


header ('Content-type: image/png');

$im = @imagecreatetruecolor(120, 20)

      or die('Cannot Initialize new GD image stream');

$text_color = imagecolorallocate($im, 233, 14, 91);

imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);




now, if I run this independently, just like a simple script, it outputs the right image with the proper string, which is exactly what I want. However, I’m interested in rendering this output alongside other html content, as part of my view. So, I have something like this:



	<?php $this->renderPartial('_layoutImage'); ?>



…and I put the ‘image’ code inside the _layoutImage file. When I load the page, I get this error:

The image “http://path/to/webapp/index.php?r=layout/add” cannot be displayed, because it contains errors.

I suspect that it probably has something to do with this line of code:

header ('Content-type: image/png');

but I thought that renderPartial renders a view file without any layouts, ie there shouldn’t be any problems with header declarations, etc.

Any help would be appreciated…


You must have a <img> tag. You can’t just output the binary image data to the browser when you want to display it in a html page.

Instead of putting the image-generation into a layout, create a controller action for it.

Then you can acess the image directly and you can include it in html pages with:

<img src="http://path/to/webapp/index.php?r=someController/actionThatGeneratesImage">

Worked like a charm

Thanks :)