Improving Performance on this Image Creation function

Posted by Abs on Stack Overflow See other posts from Stack Overflow or by Abs
Published on 2010-05-19T10:49:30Z Indexed on 2010/05/19 11:40 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

Hello all,

I am making use of GD2 and the image functions to take in a string and then convert that into an image using different fonts at different sizes. The function I use is below.

Currently, its pretty quick but not quick enough. The function gets called about 20 times per user and the images generated are always new ones (different) so caching isn't going to help!

I was hoping to get some ideas on how to make this function faster. Maybe supply more RAM to the script running? Anything else that is specific to this PHP function?

Anything else that I can do to tweak performance of this function?

  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;

}

Thanks all for any help

© Stack Overflow or respective owner

Related posts about php

Related posts about gd2