我正在使用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;
}
任何帮助的感谢所有
在你的位置,我会想到图像的数量。要为每个用户制作1张图片(无论如何)都会有所帮助。 – 2010-05-19 11:02:06