neilfurry Posted August 10, 2022 Share Posted August 10, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315163-how-to-generate-multiple-image-from-single-template-using-php/ Share on other sites More sharing options...
Barand Posted August 10, 2022 Share Posted August 10, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315163-how-to-generate-multiple-image-from-single-template-using-php/#findComment-1599217 Share on other sites More sharing options...
Solution neilfurry Posted August 10, 2022 Author Solution Share Posted August 10, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315163-how-to-generate-multiple-image-from-single-template-using-php/#findComment-1599218 Share on other sites More sharing options...
neilfurry Posted August 10, 2022 Author Share Posted August 10, 2022 Thanks so much Barand. i figured it out, i forgot to include $font value on the code you gave. Quote Link to comment https://forums.phpfreaks.com/topic/315163-how-to-generate-multiple-image-from-single-template-using-php/#findComment-1599219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.