0
votes

I am trying to generate and return an image to the template using the Silverstripe 3.0 Framework and am getting some issues.

I return this to the browser in a variable like so:

public function make_image(){

    $string = 'string';
    $im     = imagecreate(300,300);
    $orange = imagecolorallocate($im, 220, 210, 60);
    $px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
    imagestring($im, 3, $px, 9, $string, $orange);
    imagepng($im);
    imagedestroy($im);

}

The browser renders this as: �PNG IHDR,,C�6PLTE��<��*�"IDATh���1 �Om ?�x-��{�IEND�B`�

The headers that are being returned are text/html and there is only one request that makes me think there is something strange going on there. Can anyone help me out with this.

Possibly a different way of achieving this is Silverstripe using the Image() class?

1
Try header("Content-type: image/png"); - user1467267
I have, the headers in Silverstripe are set before the template is rendered. - Daniel Tate
<img src="my_image.png" type="image/png" /> There's not much info on this, but can't hurt to try to force the image to show correct on output. Are you writing the file to the server first or instantly putting it on the page? If I do this in my Terminal; file -b my_png_file.png it outputs; PNG image data, 1920 x 1080, 8-bit/color RGB, non-interlaced. As you are using a CMS, it might be that your headers are already sent. Try enabling error_reporting('E_ALL'); and see if that's the case. Very common issue. You can only use image headers when you are fetching a PHP as an image tho. - user1467267
With that I mean that the header will make the whole PHP page rendered as such. So if you do header("Content-type: image/png"); you can only give back the image, and not extra content. Something like this would be the result; <img src="my_image.php" />. - user1467267
@Allendar I am trying to output it straight to the page but I am unsure if that is the correct way. I am going to test what you suggested and the answer below. - Daniel Tate

1 Answers

2
votes

I'm not too sure why you need to play with your http headers. I understand you're going through intermediary steps to generate your image, so you need to see it being generated, but if your initial goal is to generate an image to include it in a template, this might help:

public function StringImage($string) {
    $filePath = ASSETS_PATH.'/'.$string.'.png';
    if (!file_exists(ASSETS_PATH.'/'.$string.'.png')){
        $stringFontSize = 11;
        $dimensions = imagettfbbox($stringFontSize, 90, 'Arial', $string);
        $gd = new GD();
        $width = $dimensions[2] - $dimensions[4];
        $height = $dimensions[7] - $dimensions[5];
        $image = imagecreatetruecolor($width, $height);
        imagefilledrectangle($image, 0, 0, $width, $height, 0xFFFFFF);
        imagettftext($image, $stringFontSize, 90, $width, $height, 0x000000, 'Arial', $string);
        $gd->setGD($image);
        $gd->writeTo($filePath);
    }
    return '<img src="'.ASSETS_DIR.'/'.$string.'.png'.'" alt="string"/>';
}