January 19, 2016 2:04 pm

Editing images with PHP (Creating a custom certificate for users)

To show you how to edit images in PHP we are going to create a script which takes a couple of arguments (such as the certificate recipient’s name and the issuer name and some text describing what the certificate is for) and both displays and saves the certificate as a file.text On image

The images below show how the application work:

scrshot1

 

scrshot2-certificate

The project’s file structure consists of a certificate.php file which will be responsible for creating the certificate, an index.php file where users would fill the data necessary for the certificate and where the certificate will be displayed. The project also contains a templates folder with a single image which contains the template of the certificate which is the certificate’s frame without filled text and an img folder with a certs subfolder which contains all the generated certificates.

srcshot3-fileStructure

Generating certificates with Certificate.php
We create a function which takes three parameters; the name of the certificate recipient, the thing for which he is recognized and the name of the person who gives the certificate. Thereafter, we check if any of the fields is too long and stop further execution in that case.

Thereafter, we store a new image resource using the imagecreatefrompng built-in function and pass it the path to our certificate template.

We proceed by defining a few variables. $fontSize would contain the default font size that we will be using for our text, the $font variable would contain the path to our custom font file and $black would be used to add black color to the text – we use imagecolorallocate and pass it the image resource and the usual rgb values in the subsequent parameters (the color corresponds to rgb(25,25,25))

Afterwards, we retrieve the image’s width using the imagesx built-in (by passing it the image resource).

We use the image’s width to horizontally center some of the text in the image.

To display the certificate recipient’s name, we get the edges of the text with the recipient’s name using imagettfbbox. To properly calculate the edges, you would need to pass it a valid font size, edge, a path to a font file and the desired text. After we know the edges of that text, we add the left edge to the right edge to get the text’s width (imagettfbbox would return an array with the edges as different numeric keys).

Finally, we display the text using imagettftext. We pass it the image resource, the desired font size, the edge, the x position (for x we subtract the width of the text from the image’s width and divide it by 2 so the text would be centered), the y position, the desired color (the color takes a return value from imagecolorallocate) and as a last argument – we pass the text that we want to be displayed.

Thereafter, we repeat this process a few more times with the different types of text (we pass different text and y position)

Finally, to display the image we set the Content-Type header to image/png and echo the image resource with the imagepng built-in function. This will display the image in the browser.

Now, all we have to do is save the image to a file which we do with the same function (imagepng) but we pass it a second argument which would be the path where we want the file to be stored.

Allowing users to craft their certificate

 

To do this, we add a few lines of PHP to index.php. We check if the request method is POST and If all parameters are set, we call the function that we just built and pass it the user inputs.

If the request method is not POST (the user has not submitted our form) we create a form which urges the user for the inputs necessary for the certificate.

Tutorial Categories:

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:
  • Very interesting script. Thank you very much.

  • Moud Abbas

    nice script and very usefully, GOD bless you

  • Yogesh More

    how to download demo ?