C5-8.3: dynamic image error

Permalink
I have a simple class which creates an image and a function which displays it with 'imagepng()'. If I simply open that file with the class, it shows the image.

But if I instantiate the class in view.php and then try to show it, it gives me an error: "the image such and such cannot be displayed because it contains errors".

What can be wrong with the view?

controller:
use Img;
...
    public function view() 
    {
        $img = new Img();
        $this->set('img', $img);
    }

view:
$img->show('Hello');

Img class:
class Img
{
    public function show($img_str = '')
    {
        header('Content-type: image/png');
        $im = imagecreate(100, 30);
        $bg = imagecolorallocate($im, 255, 255, 255);
        $textcolor = imagecolorallocate($im, 0, 0, 255);
        imagestring($im, 5, 0, 0, $img_str, $textcolor);
        imagepng($im);
        imagedestroy($im);
    }
}

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
If I output a string instead of an image in the class:
public function show_str($img_str = '')
    {
        echo $img_str;
    }

and this in the view:
$img->show_str('Hello');

The string is displayed. That is the class/view/controller work fine with a string. But not with an image. Can't figure out why.