0

所以我有一个名为“names.txt”的多行文件。每行包含一个名称。我还有一张名为“participant.jpg”的模型图片

这是我的代码:

<?php
$handle = fopen("names.txt", "r");

function create_img($line)
{
    $im = imagecreatefromjpeg("./images/participant.jpg");
    $textcolor = imagecolorallocate($im, 0, 0, 0);

    // Write the string at the top left
    imagettftext($im, 50, 0, 80, 640, $textcolor, 'nexa.ttf', $line);

    imagejpeg($im, './uploads/' . $line . '.jpg');
    imagedestroy($im);
}

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        create_img($line);
    }
} else {
    die('Cannot open file..');
}
?>

因此,对于每个新行,我都想创建一张特定的照片并将其“上传”到我的上传文件夹。

它只为最后一行创建图像,其余部分返回此错误:

Warning: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open './uploads/Botond.jpg' for writing: Invalid argument in C:\lab\ccc-badges\badges.php on line 12

这是一些 $line 输出

Alina Popescu
Adrian Oltean
Patricia-Andrada Leven
Stanescu Gheorghe
4

1 回答 1

1

newline这个问题是因为每个的尾随fgets(),除了最后一行。

imagejpeg($im, './uploads/' . trim($line) . '.jpg');写入文件时使用。

于 2013-10-17T21:23:30.577 回答