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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址