drxp993551 2013-03-24 23:02
浏览 49
已采纳

在Silverstripe Framework中生成并返回图像

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条回答 默认 最新

  • douwei1174 2013-03-24 23:33
    关注

    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"/>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog