DEV Community

Joel Olawanle
Joel Olawanle

Posted on

Certificate Generator With PHP Using imagettftext function

Introduction

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

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.

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

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 πŸ‘

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.