Use PDO or MySQLi, fetch associative rows, validate values before drawing them, and build output file names from validated IDs rather than raw user input.
<?php
$stmt = $pdo->query('SELECT id, name, class, mark, gender FROM student ORDER BY id');
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$id = filter_var($row['id'], FILTER_VALIDATE_INT);
if ($id === false) {
continue;
}
$image = imagecreatefromjpeg(__DIR__ . '/gd-template.jpg');
$color = imagecolorallocate($image, 0, 0, 255);
imagestring($image, 5, 200, 200, (string) $row['name'], $color);
imagejpeg($image, __DIR__ . '/outputs/' . $id . '.jpg');
imagedestroy($image);
}
?>The original certificate-generation example is retained below for comparison and learning.


require 'config-pdo.php'; // Database connection object
/// collect data from student table ///
$sql="SELECT * FROM student";
foreach($dbo->query($sql) as $row){
// generating certificates using each student record.
}
The full code is here.
<?php
header("content-type: image/jpg");
$file_name='gd-template.jpg'; // file using standard template
require 'config-pdo.php'; // database connection details
/// collect data from student table ///
$sql="SELECT * FROM student LIMIT 0,5 ";
foreach($dbo->query($sql) as $row){
$x=200; // Horizontal position to add name
$y=200; // vertical position to add name
$img_source=imagecreatefromjpeg($file_name);
/// adding name ////
$text_color=imagecolorallocate($img_source,0,0,255);
ImageString($img_source,5,$x,$y,$row['name'],$text_color);
$y=230; // vertical position to add class
ImageString($img_source,5,$x,$y,$row['class'],$text_color);
$y=260; // vertical position to add mark
ImageString($img_source,5,$x,$y,$row['mark'],$text_color);
$y=290; // vertical position to add gender
ImageString($img_source,5,$x,$y,$row['gender'],$text_color);
$x=60; // Horizontal position to add unique id
$y=399; // vertical position to add unique id
ImageString($img_source,5,$x,$y,"ID:".$row['id'],$text_color);
// adding date and time ///
$x=421; // Horizontal position
$y=399; // vertical position
$today=new DateTime; // date object
$str_date=$today->format('d-m-Y H:i:s');
$text_color=imagecolorallocate($img_source,255,0,0);
ImageString($img_source,4,$x,$y,$str_date,$text_color);
// end ///
imagejpeg($img_source,"outputs/$row['id'].jpg");
imagedestroy($img_source);
}
?>
Download the script and sample template ( image ).
| File Name | Details |
|---|---|
| config-pdo.php | PDO Database connection details are stored here. |
| gd-certificate.php | Php script to Generate the image with data from MySQL student table. |
| 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.
31-10-2023 | |
| I'm using windows 11, mysql, php, html. Graphs format and are drawn ok full screen. However, when I minimze my window the graph remains fullscreen size???? Is there a way to fix this so my graphs will display correctly for all devices. | |
05-11-2023 | |
| This script is generating image only, using the same we can add our CSS ( style ) to make it responsive. The style code required to make it rsponsive can be added while displaying if you are dynamically showing the image. One sample is here <img scr=our_image.jpg class='my_rosponsive_style'> You can also update the width and height of the template used to generate different size image. | |