0

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?

4

1 に答える 1

2

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"/>';
}
于 2013-03-24T23:33:24.620 に答える