在循环中调用PHP GD函数

在循环中调用PHP GD函数,php,Php,我已经编写了一个php脚本来动态生成文本图像。下面给出了脚本生成的演示图像。正如您可以看到的那样,为了在图像上写入文本,我多次调用了imagettfbbox、imagefilledrectangle和imagettftext(代码冗余)。现在,我想在循环中调用这些函数,生成每行文本和背景矩形图像。我想用一个完整的句子分解成几个词。我试图写在图像上的每一行必须包含四个或更少的单词。这可行吗?我需要你们php专家的一些见解 这是剧本 <?php $im = imagecreatefro

我已经编写了一个php脚本来动态生成文本图像。下面给出了脚本生成的演示图像。正如您可以看到的那样,为了在图像上写入文本,我多次调用了imagettfbbox、imagefilledrectangle和imagettftext(代码冗余)。现在,我想在循环中调用这些函数,生成每行文本和背景矩形图像。我想用一个完整的句子分解成几个词。我试图写在图像上的每一行必须包含四个或更少的单词。这可行吗?我需要你们php专家的一些见解

这是剧本

<?php
    $im = imagecreatefromjpeg('green.jpg');
    $white = imagecolorallocate($im, 0, 0, 0);
    $red = imagecolorallocate($im, 255, 0, 0);

    // Path to our font file
    $fontfile = 'theboldfont.ttf';

    // First we create our bounding box for the first text
    $bbox = imagettfbbox(30, 0, $fontfile, 'Unfortunately we never know');

    // Work out the width and height of the text
    $right_text = $bbox[2];     // right co-ordinate
    $left_text = $bbox[0];      // left co-ordinate
    $width_text = abs($right_text - $left_text);        // how wide is it?
    $height_text = abs($bbox[7] - $bbox[1]);            // how tall is it?

    // Position the text
    // Work out a base position for the start of the text
    // This is the midpoint of the available space

    $width_image = imagesx($im);
    $height_image = imagesy($im);

/*  $x = $width_image/2 - $width_text/2;
    $y = $height_image/2 - $height_text/2; */

    $x = $bbox[0] + ($width_image / 2) - ($bbox[4] / 2);
    $y = $bbox[1] + ($height_image / 2) - ($bbox[5] / 2);

    imagefilledrectangle($im, 
                                    $bbox[6] + ($width_image/2) - ($bbox[4]/2) - 10,        // X coordinate of the upper-left corner of the rectangle 
                                    $bbox[7] + ($height_image/2) - ($bbox[5]/2) - 10, 
                                    $bbox[0] + ($width_image / 2) - ($bbox[4] / 2) + ($bbox[2] - $bbox[0]) + 10, 
                                    $bbox[1] + ($height_image / 2) - ($bbox[5] / 2) + 10, 
                                    $red);

    // Write it
    imagettftext($im, 30, 0, $x, $y, $white, $fontfile, 'Unfortunately we never know');

    // Create the next bounding box for the second text
    $bbox = imagettfbbox(30, 0, $fontfile, "and as evidenced by many deaths");

    $x = $bbox[0] + ($width_image / 2) - ($bbox[4] / 2);
    $y = $bbox[1] + ($height_image / 2) - ($bbox[5] / 2) + $height_text * 2;

        imagefilledrectangle($im, 
                                    $bbox[6] + ($width_image/2) - ($bbox[4]/2) - 10,        // X coordinate of the upper-left corner of the rectangle 
                                    $bbox[7] + ($height_image/2) - ($bbox[5]/2) + $height_text +20 , 
                                    $bbox[0] + ($width_image / 2) - ($bbox[4] / 2) + ($bbox[2] - $bbox[0]) + 10, 
                                    $bbox[1] + ($height_image / 2) - ($bbox[5] / 2) + $height_text + 40, 
                                    $red);

    imagettftext($im, 30, 0, $x, $y, $white, $fontfile, "and as evidenced by many deaths");


        // Create the next bounding box for the third text
    $bbox = imagettfbbox(30, 0, $fontfile, "it without a doubt is often fatal");

    $x = $bbox[0] + ($width_image / 2) - ($bbox[4] / 2);
    $y = $bbox[1] + ($height_image / 2) - ($bbox[5] / 2) + $height_text * 4;


            imagefilledrectangle($im, 
                                    $bbox[6] + ($width_image/2) - ($bbox[4]/2) - 10,        // X coordinate of the upper-left corner of the rectangle 
                                    $bbox[7] + ($height_image/2) - ($bbox[5]/2) + $height_text + 80, 
                                    $bbox[0] + ($width_image / 2) - ($bbox[4] / 2) + ($bbox[2] - $bbox[0]) + 10, 
                                    $bbox[1] + ($height_image / 2) - ($bbox[5] / 2) + $height_text + 100, 
                                    $red);

    imagettftext($im, 30, 0, $x, $y, $white, $fontfile, "it without a doubt is often fatal");



    // Output to browser
    header ('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);

?>


代码有点大。我感谢你的时间和努力

试试这个,这将以超快的速度运行命令:

<?php
for($i=0;$i < 1;$i++)
{
thefunctionyouwanttocall();
$i = 0;
}
?>

编辑: 要减慢循环速度,请执行以下操作:

<?php
for($i=1000;$i > 1;$i--)
{
if($i < 1)
{
thefunctionyouwanttocall();
$i = 1000;
}
}
?>

尝试创建一个单独的函数,将文本添加到图像上的指定位置。然后您可以更轻松地循环调用该函数。