The GD Graphics Library is a graphics software library used to dynamically
manipulating or creating new images. It supports many programming languages
including C, PHP, Perl, Python, Ruby, Pascal, etc. In PHP we use GD library to
create JPG, GIF or PNG image. You can run phpinfo() to check GD library is
enabled in your server or not. If you don't have GD library installed then you
can ask your server provider to install GD or you can download it for free
from here.
In the following example we will add text to different position to an image and
save the image in the web server. Let say we want to generate the following
certificate through PHP GD.
Lets write php program to fill up the certificate.
PHP Code
<?php
$yourname = "Aadarsh Senapati";
$date = "09 Dec 2013";
$pos = "2nd";
$image = imagecreatefrompng('certificate.png');
imagealphablending($image, true);
$red = imagecolorallocate($image, 150,0, 0);
// imagefttext("Image", "Font Size", "Rotate Text", "Left Position",
"Top Position", "Font Color", "Font Name", "Text To Print");
imagefttext($image, 30, 0, 40, 154, $red, 'mono.ttf', $yourname);
imagefttext($image, 20, 0, 312, 206, $red, 'mono.ttf', $date);
imagefttext($image, 20, 0, 82, 256, $red, 'mono.ttf', $pos);
/* If you want to display the file in browser */
/*
header('Content-type: image/png');
ImagePng($image);
imagedestroy($image);
*/
/* if you want to save the file in the web server */
/*
$filename = 'certificate_aadarsh.png';
ImagePng($image, $filename);
imagedestroy($image);
*/
/* If you want the user to download the file */
/*
$filename = 'certificate_aadarsh.png';
ImagePng($image,$filename);
header('Pragma: public');
header('Cache-Control: public, no-cache');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename="' .
basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
readfile($filename);
imagedestroy($image);
*/
$filename = 'certificate_aadarsh.png';
ImagePng($image, $filename);
imagedestroy($image);
?>
If you want to display the file in browser:
header('Content-type: image/png');
ImagePng($image);
imagedestroy($image);
If you want to save the file in the web server
$filename = 'certificate_aadarsh.png';
ImagePng($image, $filename);
imagedestroy($image);
If you want the user to save / Download the file to Local PC
$filename = 'certificate_aadarsh.png';
ImagePng($image,$filename);
header('Pragma: public');
header('Cache-Control: public, no-cache');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename="' .
basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
readfile($filename);
imagedestroy($image);
Output:
Download File
Total Downloads: 4658