0

我正在使用 GD2 和图像函数来接收一个字符串,然后使用不同大小的不同字体将其转换为图像。我使用的功能如下。

目前,它相当快,但还不够快。每个用户调用该函数大约 20 次,并且生成的图像总是新的(不同的),因此缓存无济于事!

我希望获得一些关于如何使这个功能更快的想法。也许为正在运行的脚本提供更多 RAM?还有什么特定于这个 PHP 函数的吗?

我还能做些什么来调整此功能的性能?

  function generate_image($save_path, $text, $font_path, $font_size){

    $font = $font_path;

    /*
    * I have simplifed the line below, its actually a function that works out the size of the box
    * that is need for each image as the image size is different based on font type, font size etc
    */
    $measure = array('width' => 300, 'height'=> 120);

    if($measure['width'] > 900){ $measure['width'] = 900; }

    $im = imagecreatetruecolor($measure['width'], $measure['height']); 
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);

    imagefilledrectangle($im, 0, 0, $measure['width'], $measure['height'], $white);     

    imagettftext($im, $font_size, 0, $measure['left'], $measure['top'], $black, $font, '    '.$text);

    if(imagepng($im, $save_path)){

        $status = true;

    }else{

        $status = false;

    }

    imagedestroy($im);

    return $status;

}

谢谢大家的帮助

4

2 回答 2

1

我认为它很好

于 2010-05-19T10:52:36.950 回答
-1

你可以有一个空白的 PNG 文件,而不是每次都创建一个新图像(我们已经知道最大宽度是 900 像素,你有可以使用的固定最大高度吗?),打开它,添加你的文本,然后裁剪它(参见 imagecopy())。

我不确定,但它可能比你目前正在做的更快。

于 2010-05-19T11:36:41.877 回答