2010-05-19 34 views
0

我正在使用GD2和图像函数来获取字符串,然后使用不同大小的不同字体将其转换为图像。我使用的功能如下。改善此图像创建功能的性能

目前,它很快但不够快。该函数每个用户被调用大约20次,生成的图像总是新的(不同),所以缓存不会帮助!

我希望得到一些关于如何使这个功能更快的想法。也许提供更多的内存给脚本运行?还有其他特定于此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; 

} 

任何帮助的感谢所有

+0

在你的位置,我会想到图像的数量。要为每个用户制作1张图片(无论如何)都会有所帮助。 – 2010-05-19 11:02:06

回答

-1

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

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