I'm creating a student ID card system, where each student will have his/her own.
For this I am using the intervention image, to pick up the student data and put in the standard ID card template.
The basic code for this I did was:
<?php
[...]
$card = Image::make('card_id-model.png');
$card->text('CARD ID', 20, 40, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
$card->text('NAME: STUDENT\'S NAME', 20, 60, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
$card->text('CODE: STUDENT\'S CODE', 20, 80, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
$card->save('card_id - student\'s code.png');
This way is slower, considering that I will call the function every time I create the image. So I decided to instantiate the image first, since it will be used for all card id. Well, but I'm experiencing problems applying it in a loop to generate several at the same time since the texts are overwriting.
<?php
[...]
$card = Image::make('card_id-model.png');
$card->text('CARD ID', 20, 40, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
for ($students as $student) {
$card->text('NAME: ' . $student->name, 20, 60, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
$card->text('CODE: ' . $student->code, 20, 80, function($font) {
$font->file('arial.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
$card->save('card_id - ' . $student->code . '.png');
}
That was the best and quickest way I found to mass-generate. Anybody got any tips?
i usually do a design first in AI, then get the SVG out of it, then fill in with CSS+HTML. Since PHP can help filling it in, i use it to generate the HTML only.
Then the HTML will be transformed to PDF and then i just print it from there.
In any case, PHP is not ideal to generate images eventhough there's a way to do it. Use Browser's power to do it. Google Chrome is robust, but if you need it to be runable on server, you can use wkhtmltopdf.
You are having the issue with text overwriting, because you are calling the text function on original object. Instead you need to do modifications on copy of element, and then save it in array.
See example.
$generatedCards = [];
foreach ($students as $student) {
$generatedCard = clone $card;
$generatedCard->text('...');
$generatedCards[] = $generatedCard;
}