0

I've some code like this:

<?php

$file = fopen("inputfile.txt", "r");
$i = 0;
while (!feof($file)) {
    $line_of_text .= fgets($file);
}
$myText = explode("\n", $line_of_text);
fclose($file);
print_r($myText);

$arrayCount = count($myText);

for ($x = 0; $x < $arrayCount; $x++) {
    $image = imagecreatefromjpeg('flower.jpg');

    // First we create our stamp image manually from GD
    $stamp = imagecreatetruecolor(200, 70);

    imagefilledrectangle($stamp, 0, 0, 199, 169, 0x0000FF);

    imagefilledrectangle($stamp, 9, 9, 190, 60, 0xFFFFFF);

    imagestring($stamp, 5, 20, 20, $myText[$x], 0x0000FF);

    imagestring($stamp, $myFont, 20, 40, '(c) 2017', 0x0000FF);
    //imagettftext ( $stamp, 5, 0, 20, 20, 20, 'arial.ttf' , "thanks!");

    // Set the margins for the stamp and get the height/width of the stamp image
    $right = 250;

    $bottom = 250;

    $sx = imagesx($stamp);

    $sy = imagesy($stamp);

    // Merge the stamp onto our photo with an opacity of 50%
    imagecopymerge($image, $stamp, imagesx($image) - $sx - $right, imagesy($image) - $sy - $bottom, 0, 0, imagesx($stamp), imagesy($stamp), 40);

    // Save the image to file and free memory
    imagepng($image, 'flower_stamp-'.$x.'.png');


    imagedestroy($image);
}

?>

And the inputfile.txt is like this:

Category Name 1
Category Name 2
Category Name 3

When I run this code it will generate new images with watermark.

The problem is I'm using imagestring and I can't define my custom font. How can I change the font family before watermark?

Sarah
  • 15
  • 3
  • See here for reading a file line by line: https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line – Brett Gregson Oct 30 '19 at 15:52
  • Technically the code you posted has zero to do with your question `How can I generate multiple image with different text.`. As such where is your code attempt to solve the immediate problem? – GetSet Oct 30 '19 at 15:53
  • 1
    Thank you @BrettGregson . you've saved my life. – Sarah Oct 30 '19 at 19:21
  • Thank you @GetSet , I've tried with new code. and I was a bit successful. But I have problem with font family. – Sarah Oct 30 '19 at 19:23
  • Pleasure @Sarah! What's the problem you're having with the font? – Brett Gregson Oct 30 '19 at 20:57
  • @Sarah, the documentation says you have to register the custom font with `imageloadfont()`, ref https://www.php.net/manual/en/function.imageloadfont.php – GetSet Oct 31 '19 at 09:15

0 Answers0