PDA

View Full Version : How to use PHP GD to write text



Invent
19-05-2007, 09:29 PM
This is a small code which explains how to use PHP GD to produce an image with text from a font on a website.


<?php
header("Content-type: image/gif"); // We have to set the header of the file as its an image

$text = @$_GET['text']; // Get the text to write
// The $_GET function grabs the data specified by the ?text parameter on the URL

$font = 'Volter.ttf'; // Set the TTF font we want to use
// This is just a variable set, which we will later use in the imagettftext funciton

$fontsize = "6.5"; // Set the size of the font we want to use
// This again is just a variable like above.



// Create the image
$size = imagettfbbox($fontsize, 0, $font, $text); // Set the positions of the area of the font
$width = $size[2] + $size[0] + 8;
$height = abs($size[1]) + abs($size[7]); // Get values
// We have the right sizes for the text

$im = imagecreate($width, $height);
// Create the image of the right size for the text to fit

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255); // Create the colour white using RGB
$black = imagecolorallocate($im, 0, 0, 0); // Create the colour black using RGB


# The imagecolorallocate uses RGB colours to allocate an individual colour to a string. EG/ $white.

// Make a colour transparent
$colourBlack = imagecolorallocate($im, 255, 255, 255); // Set colour black using RBG
imagecolortransparent($im, $colourBlack); // Make colour black transparent

# imagecolortransparent makes the pre-made string which has been allocated a colour transparent on the image.

// Add the text
imagefttext($im, $fontsize, 0, 0, abs($size[5]), $black, $font, $text);

# imagettftext writes the text to the image, the different stages of the funciton are explain below the php code (from php.net)

imagegif($im); // Output the image using imagegif()
imagedestroy($im); // Delete the image to keep server load down
?>

menno
19-05-2007, 09:41 PM
Hmm.. Thats is funny :P

Really simple to edit with the official habbo letters
Thnx ;)

Invent
19-05-2007, 09:42 PM
English please =D

Want to hide these adverts? Register an account for free!