Text To Gif

Description: Slick method of changing text to an image- useful to prevent copying text easily.

<?PHP
 
// To draw the GIF - call this script with a URL like the following:
// text_gif.php?msg=testing+class&rot=15&size=48&font=fonts/TIMES.TTF
 
HEADER("Content-type: image/gif");
 
CLASS textGIF {
 VAR $font = 'fonts/TIMES.TTF'; //default font. directory relative to script directory.
 VAR $msg = "undefined"; // default text to display.
 VAR $size = 24;
 VAR $rot = 0; // rotation in degrees.
 VAR $pad = 0; // padding.
 VAR $transparent = 1; // transparency set to on.
 VAR $red = 0; // white text...
 VAR $grn = 0;
 VAR $blu = 0;
 VAR $bg_red = 255; // on black background.
 VAR $bg_grn = 255;
 VAR $bg_blu = 255;
 
FUNCTION draw() {
 $width = 0;
 $height = 0;
 $offset_x = 0;
 $offset_y = 0;
 $bounds = ARRAY();
 $image = "";
 
 // determine font height.
 $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
 IF ($this->rot < 0) {
  $font_height = ABS($bounds[7]-$bounds[1]);
 } ELSE IF ($this->rot > 0) {
  $font_height = ABS($bounds[1]-$bounds[7]);
 } ELSE {
  $font_height = ABS($bounds[7]-$bounds[1]);
 }
 
 // determine bounding box.
 $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
 IF ($this->rot < 0) {
  $width = ABS($bounds[4]-$bounds[0]);
  $height = ABS($bounds[3]-$bounds[7]);
  $offset_y = $font_height;
  $offset_x = 0;
 
 } ELSE IF ($this->rot > 0) {
  $width = ABS($bounds[2]-$bounds[6]);
  $height = ABS($bounds[1]-$bounds[5]);
  $offset_y = ABS($bounds[7]-$bounds[5])+$font_height;
  $offset_x = ABS($bounds[0]-$bounds[6]);
 
 } ELSE {
  $width = ABS($bounds[4]-$bounds[6]);
  $height = ABS($bounds[7]-$bounds[1]);
  $offset_y = $font_height;;
  $offset_x = 0;
 }
 
 $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
 
 $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
 $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
 
 IF ($this->transparent) ImageColorTransparent($image, $background);
 ImageInterlace($image, FALSE);
 
 // render it.
 ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
 
 // output GIF object.
 imageGIF($image);
}
}
 
$text = NEW textGIF;
 
IF (ISSET($msg)) $text->msg = $msg; // text to display
IF (ISSET($font)) $text->font = $font; // font to use (include directory if needed).
IF (ISSET($size)) $text->size = $size; // size in points
IF (ISSET($rot)) $text->rot = $rot; // rotation
IF (ISSET($pad)) $text->pad = $pad; // padding in pixels around text.
IF (ISSET($red)) $text->red = $red; // text color
IF (ISSET($grn)) $text->grn = $grn; // ..
IF (ISSET($blu)) $text->blu = $blu; // ..
IF (ISSET($bg_red)) $text->bg_red = $bg_red; // background color.
IF (ISSET($bg_grn)) $text->bg_grn = $bg_grn; // ..
IF (ISSET($bg_blu)) $text->bg_blu = $bg_blu; // ..
IF (ISSET($tr)) $text->transparent = $tr; // transparency flag (boolean).
 
$text->draw();
 
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.