我正在使用 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;
}
谢谢大家的帮助