0

So I have a file named "names.txt" with multiple lines. Each line contains one name. I also have one mockup image named "participant.jpg"

And here it`s my code:

<?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..');
}
?>

So for every new line I want to create a specific photo and "upload" it to my uploads folder.

It only creates the image for the last line and for the rest it returns me this error:

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

This is some $line outputs

Alina Popescu
Adrian Oltean
Patricia-Andrada Leven
Stanescu Gheorghe
11
  • try to create new file with name './uploads/'.$line.'.jpg' before line 12 Commented Oct 10, 2013 at 15:35
  • wat's the difference between last line and other lines? Commented Oct 10, 2013 at 15:40
  • no difference .. just that is the last the line Commented Oct 10, 2013 at 15:44
  • can you please show some outputs of $line aspecially the last one? I mean echo $line,'<br>'; Commented Oct 12, 2013 at 8:48
  • 1
    yeah, use imagejpeg($im, './uploads/' . trim($line) . '.jpg'); when writing in a file Commented Oct 16, 2013 at 21:44

1 Answer 1

1

That problem because the trailing newline of each fgets(), except the last line.

use imagejpeg($im, './uploads/' . trim($line) . '.jpg'); when writing in a file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.