DEV Community

Joel Olawanle
Joel Olawanle

Posted on

18 5

Certificate Generator With PHP Using imagettftext function

Introduction

Imagettftext is a function used to write text to an image using TrueType fonts.

imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
view raw description hosted with ❤ by GitHub

Explaining all the parameters involved in this array:

Image:
An image resource is returned by one of the image creation functions, such as imagecreatetruecolor(), imagecreatefrompng(), e.t.c.This image is supposed to be an empty certificate.

Empty Certificate

Size
The font size of the text you intend to place on image.

Angle
The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.

X and Y
The x and y axis are use to set the position of the text using the x and y co-ordinates

Color
The color index. we make use of the imagecolorallocate() to set color to the text we are displaying on the image in RGB format.

Fontfile
The path to the TrueType font you wish to use.It could have an extension of .ttf, .otf, e.t.c.

Text
Finally this is the name attributed to the text you intend to display on the image.

This is the major function behind Certificate generator. A good understanding of this would be very useful in other approaches like Id care generator, ticket generator and lots more…
This approach could also be integrated to your websites, so people get certified after performing a specific task like taking a course instead of doing everything manually which would take time
.

Getting Started

To get started here are the major stuffs needed

  • Font file
  • Empty certificate PNG file
  • And finally a PHP file where all my codes would be for simplicity.

Note: You can tweak anything once you perfectly understand this code and how this function works.

You can give your file any name, but I would name mine index.php

Index.php

The index file will contain the form where peoples name would be fetched from and also the PHP code.
Below is the form where data for certificate generation is gotten from.

<form method="post" action="">
<div class="form-group col-sm-6">
<input type="text" name="name" class="form-control" id="name" placeholder="Enter Name Here...">
</div>
<button type="submit" name="generate" class="btn btn-primary">Generate</button>
</form>
view raw index hosted with ❤ by GitHub

From the above form we are only getting the name and then the code below is the PHP code

<?php
if (isset($_POST['generate'])) {
$name = strtoupper($_POST['name']);
//designed certificate picture
$image = "certi.png";
$createimage = imagecreatefrompng($image);
//this is going to be created once the generate button is clicked
$output = "certificate.png";
//then we make use of the imagecolorallocate inbuilt php function which i used to set color to the text we are displaying on the image in RGB format
$white = imagecolorallocate($createimage, 205, 245, 255);
$black = imagecolorallocate($createimage, 0, 0, 0);
//Then we make use of the angle since we will also make use of it when calling the imagettftext function below
$rotation = 0;
//we then set the x and y axis to fix the position of our text name
$origin_x = 200;
$origin_y=260;
$font_size = 10;
$certificate_text = $name;
//font directory for name
$drFont = dirname(__FILE__)."/developer.ttf";
//function to display name on certificate picture
$text1 = imagettftext($createimage, $font_size, $rotation, $origin_x, $origin_y, $black, $drFont, $certificate_text);
imagepng($createimage,$output,3);
?>
view raw index.php hosted with ❤ by GitHub

This is the basic thing you need to know to place text on image with php.
Here is a link to the complete code - Certificate Generator and you can test it live Here.

If you have any idea on more things that can be done via this function or any function for uploading image on image.Let's talk on Twitter.

P.s: I'm looking to make new dev friends, lets connect on Twitter.

Thanks for reading 👏

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (3)

Collapse
 
hackergaucho profile image
Hacker Gaucho • Edited

great work. I use and recommend the claviska/simpleimage library that adds a little bit of syntactic sugar to GD.

Collapse
 
olawanle_joel profile image
Joel Olawanle

Wao, I check it out and it was awesome

Collapse
 
andymunaff profile image
andymunaff

Will this work for Arabic text.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay