提高此图像创建功能的性能

发布于 2024-09-02 10:03:37 字数 1216 浏览 2 评论 0原文

我正在利用 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;

}

感谢大家的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2024-09-09 10:03:37

我认为它很好

I think its good

醉城メ夜风 2024-09-09 10:03:37

您可以有一个空白的 PNG 文件(我们已经知道最大宽度是 900 像素,您有可以使用的固定最大高度吗?),而不是每次都创建新图像,打开它,添加文本,然后裁剪它(参见 imagecopy())。

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

Instead of creating a new image each time, you could have a blank PNG file (we already know that the maximum width is 900px, do you have a fixed maximum height you can use?), open it, add your text, and then crop it (see imagecopy()).

I'm not sure, but it might be faster than what you are currently doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文