提高此图像创建功能的性能

时间:2010-05-19 10:49:31

标签: php image gd2

我正在使用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;

}

感谢大家的帮助

2 个答案:

答案 0 :(得分:1)

我认为它很好

答案 1 :(得分:-1)

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

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