PhpSnip.com

User Stats

Text on Image

Very simple class to use to write text on an image. Supports GIF, PNG, and JPEG. GD must be compiled with php. For the following example, provide your own image please. Example: ========================= $config=array( "text" => "123456", "text_colors" => "0 0 0", // RGB Seperated by spaces "image_loc" => "coupon.jpg", "image_type" => "JPEG", // PNG and GIF Supported // Optional arguments; default is center area on image "x_pos" => "", "y_pos" => "", ); $graphic=new img_add_txt($config);

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 449

Source Code ( 123 lines )

<?
	class img_add_txt{
	
	
		/*
			Created by Richard Sumilang
			http://www.richard-sumilang.com
			
			
			object img_add_txt($array);
			------------------------------------
			This function adds text on top of an image
			
			Usage:
			------------------------------------
			$config=array(
						"text" => "Coupon: Here is your free coupon",
						"text_colors" => "255 68 0", // RGB Seperated by spaces
						"image_loc" => "example.jpg",
						"image_type" => "JPEG", // PNG and GIF Supported
						// Optional arguments; default is center area on image
						"x_pos" => "",
						"y_pos" => "",
						
			);
			
			$graphic=new img_add_txt($config);
		*/
		
		function img_add_txt($config){
	
			// header
			//header("Content-Type: image/gif");
			$this->func_header($config['image_type']);
			
			// set up image
			$im = ImageCreateFromJPEG($config['image_loc']);
			
			// Set up text colors
			$text_colors=explode(" ", $config['text_colors']);
			$text_color = ImageColorAllocate($im, $text_colors['0'], $text_colors['1'], $text_colors['2']);
			
			// get font dimensiona
			$font_height = ImageFontHeight(3);
			$font_width = ImageFontWidth(3);
			
			// get image dimensiona
			$image_height = ImageSY($im);
			$image_width = ImageSX($im);
			
			// get string length
			$length = $font_width * strlen($config['text']);
			
			// set the x, y cords of where the text will be placed
			if(empty($config['x_pos'])){
				// calculate start coordinates for string
				$image_center_x = ($image_width/2)-($length/2);
			}else{
				$image_center_x = $config['x_pos'];
			}
			if(empty($config['y_pos'])){
				// calculate start coordinates for string
				$image_center_y = ($image_height/2)-($font_height/2);
			}else{
				$image_center_y = $config['y_pos'];
			}
			
			// write string
			ImageString($im, 3, $image_center_x, $image_center_y, $config['text'], $text_color);
			
			// output to browser
			$this->output_image($config['image_type'], $im);
		
		}// End img_add_txt
		
		
		/*
			Output the correct header based
			on file type
		*/
		function func_header($var){
			
			switch($var){
				case "PNG":
					header("Content-Type: image/png");
				break;
				
				case "GIF":
					header("Content-Type: image/gif");
				break;
				
				case "JPEG":
					header("Content-Type: image/jpeg");
				break;
			}
			
		}// End func_header
	
	
		/*
			Output the correct image type
			based on type
		*/
		function output_image($var, $pointer){
			
			switch($var){
				case "PNG":
					ImagePNG($pointer);
				break;
				
				case "GIF":
					ImageGIF($pointer);
				break;
				
				case "JPEG":
					ImageJPEG($pointer);
				break;
			}
			
		} // End out output image
			
	} // End of class
?>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise