0 Replies - 1481 Views - Last Post: 10 October 2011 - 08:40 AM

#1 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Text-to-Image with GD

Posted 10 October 2011 - 08:40 AM

Description: To use this snippet, place it in a .php file (for sake of example, txt2img.php) on a server with PHP compiled with the GD library, and call it with an image tag passing the string into the 'v' GET parameter: <img src="txt2img.php?v=Hello,%20World!" alt="Hello, World"/>This snippet, when passed a string through a GET parameter, uses the GD library to output a .png image containing the text.
<?php
	if(isset($_GET['v']) && extension_loaded('gd') && function_exists('gd_info')){
		$string = $_GET['v'];
		$font_size = 4;

		$width  = imagefontwidth($font_size)*strlen($string);
		$height = imagefontheight($font_size);
		$img = imagecreate($width,$height);
		$bg    = imagecolorallocate($img, 25, 25, 25);
		$color = imagecolorallocate($img, 255, 255, 255);
		$len = strlen($string);
		$ypos = 0;
		for($i=0;$i<$len;$i++){
			$xpos = $i * imagefontwidth($font_size);
			imagechar($img, $font_size, $xpos, $ypos, $string, $color);
			$string = substr($string, 1);

		}
		header("Content-Type: image/png");
		imagepng($img);
		imagedestroy($img);
	}
?>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1