使用以下脚本,我将文本分成句子,并从每个句子动态创建图像。我的for循环没有正确执行。我不懂为什么?尽管文本包含4个不同的句子,但图像创建功能仅被调用一次,仅创建一个图像。该脚本的目的是创建四个图像并将其保存到硬盘驱动器中。我需要你的建议。
<?php
$text = "Hello PHP! some sentences. very difficult, yes? that's for sure!?!";
$chunks = preg_split('#[\r\n]#', $text, -1, PREG_SPLIT_NO_EMPTY);
foreach($chunks as $val)
{
preg_match_all('#(?:\s[a-z]\.(?:[a-z]\.)?|.)+?[.?!]+#i', $val, $paragraph);
foreach($paragraph[0] as $val)
{
$sentences[] = ltrim($val);
}
}
$sentencesCount = count($sentences);
for ($i=0; $i <$sentencesCount ; $i++)
{
//$value = $sentences[$i];
//echo $value . "<br />";
$image = imagecreatetruecolor(200, 50);
imagefilledrectangle($image, 0, 0, 199, 49, 0xFFFFFF); //white
$fontfile = 'verdana.ttf';
imagefttext($image, 20, 0, 20, 35, 0x000000, $fontfile, $sentences[$i]);
header('Content-type: image/png');
imagepng($image);
//Save the image after the current timestamp.
imagepng($image, "savedimages/" . time() . ".png");
//Clean up.
imagedestroy($image);
}
?>