我已经编写了一个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);
?>
代码有点大。我感谢您的时间和精力。
答案 0 :(得分:1)
试试这个,这将运行命令超快:
<?php
for($i=0;$i < 1;$i++)
{
thefunctionyouwanttocall();
$i = 0;
}
?>
要减慢循环,请执行以下操作:
<?php
for($i=1000;$i > 1;$i--)
{
if($i < 1)
{
thefunctionyouwanttocall();
$i = 1000;
}
}
?>