When the database ID is used to choose a student photo, validate it first and confirm that the expected file exists before loading it into GD.
<?php
$id = filter_var($row['id'], FILTER_VALIDATE_INT);
$photoPath = __DIR__ . '/photos/' . $id . '.png';
if ($id !== false && is_file($photoPath)) {
$photo = imagecreatefrompng($photoPath);
imagecopy($certificate, $photo, 60, 310, 0, 0, 100, 100);
imagedestroy($photo);
}
?>Use the same checks for logos and other local image assets before copying them onto the certificate.

$QR=imagecreatefromstring(file_get_contents(
"https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl="
.urlencode($row->id)));
imagecopyresampled($img_source, $QR, 475, 40 , 0, 0, 100, 100, 100,100);
$str=imagecreatefrompng("photos/".$row['id'].".png");
imagecopy($img_source,$str, 60, 310 , 0, 0, 100, 100);
We created the path by using file name with directory name in above code and then by using imagecopy() the same is added at a location ( x=60, y=310 ) of the source image.
$str=imagecreatefrompng("logo.png");
imagecopy($img_source,$str, 280, 50 , 0, 0, 50, 50);
Above codes are kept inside the for loop used to generate certificates of all students by taking data from MySQL student table.
| File Name | Details |
|---|---|
| config-pdo.php | PDO Database connection details are stored here. |
| gd-certificate.php | Php script to Generate the image with data, profile photo and logo for each student. |
| gd-template.jpg | The sample template image used to generate the final certificates. |
| sql_dump.txt | SQL Dump to create student table with sample data. |
| readme.txt | Instructions on how to run the script |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.