joseluisq/gimage

Example with text array

tenq opened this issue · 3 comments

tenq commented

Hi, I want generate a restaurant menu with front/verse. How I do array with menu items.

Thanks

Did you try https://joseluisq.github.io/gimage/examples/text-embedding/?
You can create a canvas as base image, then every text that you want to use you can append it to canvas but for each text as well you can adjust the position how to display them in canvas. Check out the Text class too https://joseluisq.github.io/gimage/classes/text/

tenq commented

yes, i'm using it. After a few attempts I managed to come up with a code that apparently will serve me. The goal is to pass an array of products (and their respective values) and create an image for sharing on social networks. Look at the code I got (if it's not the best option and I want to suggest changes, thank you), but I'm still starting development.

`
$figure = new Figure(400, 250);
$figure
->setBackgroundColor(47, 42, 39)
->create();

    $final_text = new stdClass();
    $x = 40;
    $y = 20;
    //simulate array
    $products[] = array ('txt'=> 'texto 001');
    $products[] = array ('txt'=> 'texto 004');
    $products[] = array ('txt'=> 'texto 007');
    $products[] = array ('txt'=> 'texto 005');
    $products[] = array ('txt'=> 'texto 006');

    $canvas = new Canvas($figure);

    foreach ($products as $txt) {

      $text = new Text($txt['txt']);
      $y = $y + 30;

      $text
          ->setWidth(400)
          ->setHeight(250)
          ->setAlign('center') // "none" by default
          ->setLineHeight(1.2)
          ->setLeft($x)
          ->setTop($y)
          ->setSize(22)
          ->setColor(255, 255, 255)
          ->setFontface(FCPATH . 'uploads/fonts/Roboto-Black.ttf');

        //$final_text= (object) array_merge((array) $final_text, (array) $text);
         $canvas
        ->append($text);

   }


    $canvas
        ->toPNG()
        ->draw()
        ->output();
        //->save('text.png');
}

`

In general your example is fine for your requirement.

So I'm going to close the issue for now.