DEV Community

Cover image for Create dynamic certificates in PHP
Chetan Rohilla
Chetan Rohilla

Posted on • Updated on • Originally published at w3courses.org

Create dynamic certificates in PHP

We will use the PHP GD Library or extension which will provide us the functions to manipulate our images dynamically.

Uses of PHP GD Library:

  • To create dynamic certificates
  • Create dynamic captcha
  • Creating dynamic charts
  • Creating dynamic Reports
  • We can create the watermarks on our images
  • We can resize the images, increase or decrease the quality of images, change image mime type or extension
  • Optimize or Compress Images

Here we will create a dynamic certificate in PHP by using PHP GD Library.

First of all we have to check is PHP GD Extension is enabled or not. Use the code below and check is gd enabled.

<?php

/*Checking the PHP GD Extension Enabled or Not*/

phpinfo();

Enter fullscreen mode Exit fullscreen mode

PHP Info

Now we will create the certificate dynamically by using php gd library.

/*Using PHP GD Library to process images and creating dynamic certificates, captcha, reports etc.*/

/*Creating Certificate Dynamically*/

//Set the Content Type
header('Content-type: image/jpeg');

// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('certificate.jpg');

// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 54, 12, 110);

// Set Path to Font File
$font_path = 'font.ttf';

$name_text = "Chetan Rohilla";

$date_text = date('jS F,Y');

$signature = imagecreatefrompng('signature.png');

imagettftext($jpg_image, 26, 0, 480, 400, $white, $font_path, $name_text);

imagettftext($jpg_image, 20, 0, 220, 670, $white, $font_path, $date_text);

/*signature on image*/
imagecopy($jpg_image, $signature, 780, 620, 0, 0, 200, 58);
/*signature on image*/

// Send Image to Browser
imagejpeg($jpg_image);

// Clear Memory
imagedestroy($jpg_image);
Enter fullscreen mode Exit fullscreen mode

After adding this code, you have to upload certificate.jpg file, signature.png file and font.ttf file in the folder where your php file is located.

Download font.ttf

That’s it Now you can create dynamic certificates in php, watermarks in php, captcha in php, charts in php or process images in php.


Please like share subscribe and give positive feedback to motivate me to write more for you.

For more tutorials please visit my website.

Thanks:)
Happy Coding:)

Top comments (0)