douou9094747 2019-05-27 16:14
浏览 46

生成不同文本图像的问题

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?

  • 写回答

2条回答 默认 最新

  • dssqq64884 2019-05-27 16:23
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)