Jump to content

How to generate multiple image from single template using php


Go to solution Solved by neilfurry,

Recommended Posts

Hi,

I need some help please. here is what i want to do:

1. im making a script which will generate bulk id card populating the data from the database and displaying data to a single template and will generate that id cards in return.

here is my script:

$sql = mysqli_query($con, "SELECT * FROM employees");

header('Content-type: image/png');
$image = imagecreatefrompng('id-template.png');
$color = imagecolorallocate($image, 0, 0, 0);
imagepng($image);

while($rs = mysqli_fetch_assoc($sql)){

imagettftext($image, 13, 0, 60,117, $color, $font, $rs['empname']); //employee name location of the template

$save = "id/".strtolower($rs['id']).".png";

imagepng($image, $save, 9, PNG_NO_FILTER);

}

This produces id card, but the data passed to each id card overlaps with the other.

Please help me fix this problem.

Thank you

That's because you keep writing to the same image template. Recreate each time in the loop and destroy at the end to release the memory.

$sql = mysqli_query($con, "SELECT id, empname FROM employees");

header('Content-type: image/png');
$image = imagecreatefrompng('id-template.png');
$color = imagecolorallocate($image, 0, 0, 0);
imagepng($image);

while($rs = mysqli_fetch_assoc($sql)){

    $image = imagecreatefrompng('id-template.png');
    $color = imagecolorallocate($image, 0, 0, 0);
    imagettftext($image, 13, 0, 60,117, $color, $font, $rs['empname']); //employee name location of the template

    $save = "id/".strtolower($rs['id']).".png";

    imagepng($image, $save, 9, PNG_NO_FILTER);
    imagedestroy($image)
}

PS Wouln't it make more sense to have a different image for each employee - or do you only employ clones?

  • Solution
8 minutes ago, Barand said:

That's because you keep writing to the same image template. Recreate each time in the loop and destroy at the end to release the memory.

$sql = mysqli_query($con, "SELECT id, empname FROM employees");

header('Content-type: image/png');
$image = imagecreatefrompng('id-template.png');
$color = imagecolorallocate($image, 0, 0, 0);
imagepng($image);

while($rs = mysqli_fetch_assoc($sql)){

    $image = imagecreatefrompng('id-template.png');
    $color = imagecolorallocate($image, 0, 0, 0);
    imagettftext($image, 13, 0, 60,117, $color, $font, $rs['empname']); //employee name location of the template

    $save = "id/".strtolower($rs['id']).".png";

    imagepng($image, $save, 9, PNG_NO_FILTER);
    imagedestroy($image)
}

 

Now it does not write anything on the id cards when i run that code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.