Location: PHPKode > scripts > Overlay Text > overlay-text/class.overlay.php
<?php
class overlay_text{
	
	function getTextWidth($font_size,$angle,$font_file,$text){
		$bbox = imagettfbbox($font_size,$angle,$font_file,$text); //creates array
		$bbox_width = $bbox[2] - $bbox[0]; //lower right corner - lower left corner = width
	return $bbox_width;	
	}

	function getTextHeight($font_size,$angle,$font_file,$text){
		$bbox = imagettfbbox($font_size,$angle,$font_file,$text); //creates array
		$bbox_height = $bbox[1] - $bbox[7]; //lower right corner - lower left corner = width
	return $bbox_height;	
	}

	function hex2rgb($color){
    	if (substr($color, 0, 1) == '#'){
        	$color = substr($color, 1);
        }
    	if (strlen($color) == 6){
			$r = hexdec(substr($color, 0,2));
			$g = hexdec(substr($color, 2,2));
			$b = hexdec(substr($color, 4));
   			return array($r, $g, $b);
		}else{
			return array(0, 0, 0);
		}
	}
	
	/**
	 * Constructor - Takes args and creates a new image with text overlayed on top
	 * ARGS:
	 * $txt_string = text to put on image
	 * $hex_color = The color you want the text to be in standard hex format
	 * $int_font_size = The font size you want, input as an integer
	 * $align = where you want the text aligned on the image(options: top_l, top_r, bottom_l, bottom_r, center)
	 * $img_in_path = your image to put text on
	 * $image_out_path = relative path and filename to save new image to
	 */
	function overlay_text($txt_string, $hex_color, $int_font_size, $align, $img_in_path, $img_out_path){
		
		//////////////////////////
		//User-defined Variables
		//////////////////////////
		$font_file			= 'Puritan2.otf'; //path to actual font file. Here it is in same directory
		$margin				= 10;
		
		//////////////////////////
		//get input variables for use
		//////////////////////////
		$bg_img 			= $img_in_path;//image to put text on
		$font_size			= $int_font_size;
		$user_img_path 		= $img_out_path;//file to output finished product
		
		//////////////////////////
		//Create working image
		//////////////////////////
		$bg_img_dims		= getimagesize($bg_img); //returns array of dimensions
		$bg_imagewidth		= $bg_img_dims[0];
		$bg_imageheight		= $bg_img_dims[1];
		$canvas 			= imagecreatetruecolor($bg_imagewidth,$bg_imageheight);
		$image				= imagecreatefromjpeg($bg_img);
		
		//////////////////////////
		//Get the hex and convert it for RGB use in the text
		//////////////////////////
		$rgb_array			= $this->hex2rgb($hex_color);
		$txt_color 			= imagecolorallocate($image, $rgb_array[0], $rgb_array[1], $rgb_array[2]);

		//////////////////////////
		//Get the text's Bounding Box dimensions
		//////////////////////////
		$text_box_width		= $this->getTextWidth($font_size,0,$font_file,$txt_string);
		$text_box_height	= $this->getTextHeight($font_size,0,$font_file,$txt_string);
		
		switch($align){
			case 'top_r':
				$x = $bg_imagewidth - $text_box_width - $margin;
				$y = $font_size + $margin;		
			break;
			case 'top_l':
				$x = $margin;
				$y = $font_size + $margin;
			break;
			case 'bottom_r':
				$x = $bg_imagewidth - $text_box_width - $margin;
				$y = $bg_imageheight - $margin;
			break;
			case 'bottom_l':
				$x = $margin;
				$y = $bg_imageheight - $margin;
			break;
			case 'center':
				$x = ($bg_imagewidth / 2) - ($text_box_width / 2);
				$y = ($bg_imageheight / 2) + ($text_box_height / 2);
			break;
			
		}
		
		imagettftext($image, $font_size, 0, $x, $y, $txt_color, $font_file, $txt_string);

	imagejpeg($image,$user_img_path,100);
	imagedestroy($image);
	}

}
?>
Return current item: Overlay Text