2010-05-19 81 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())。

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