Using Data from MySQL with ImageString() to create Image

Here is a blank template ( certificate ).
certificate template

By using this template we will generate certificate for each student by using their data from MySQL student table. Here is one sample output
certificate with MySQL table student data

Part 1 : How certificate is prepared by using user input through a HTML form



Generating certificates using standard templates by adding data from MySQL table using PHP GD

Getting records from MySQL database

We are using Php Data Object ( PDO ) to connect to database and getting the records by using looping. We can use MySQLi also.

There are 35 records in our student table. We will loop through the records by using foreach() and inside the loop we will create the certificates by using the data of each student.
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 NameDetails
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

Adding QR code to certificate

Each student is identified by its unique ID column. Using this unique ID we can create one unique QR code and add the same to each certificate. We can display the certificate with QR code or provide a link to download the certificate as JPG file.
How QR code is embedded in Certificate ( image ) using MySQL table data

Adding Logo, QR code and profile photo to image
GD functions
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer