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

* indicates required
Subscribe to plus2net

    plus2net.com







    Applications using GD functions in PHP

    Basic function and application using GD in PHP
    Sin and Cos curve using GD
    Script for Watermarking image on the fly using PHP GD
    Script to generate Captcha using GD with demo
    Getting Image coordinates ( vertical and horizontal ) of points inside a image
    Generating certificates by using templates and user inputs
    Generating certificates by using templates and MySQL table data
    Generating certificates by using templates and MySQL table data with QR code
    Page Hit counter script

    Post your comments , suggestion , error , requirements etc here




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