<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthew Steven Kelly &#187; images</title>
	<atom:link href="http://www.matthewstevenkelly.com/blog/tag/images/feed" rel="self" type="application/rss+xml" />
	<link>http://www.matthewstevenkelly.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 05 Feb 2012 04:43:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Generating an image from text</title>
		<link>http://www.matthewstevenkelly.com/blog/random/generating-image-from-text.html</link>
		<comments>http://www.matthewstevenkelly.com/blog/random/generating-image-from-text.html#comments</comments>
		<pubDate>Tue, 24 Mar 2009 01:23:24 +0000</pubDate>
		<dc:creator>Matthew Steven Kelly</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.matthewstevenkelly.com/blog/?p=496</guid>
		<description><![CDATA[&#8221; /> How did I do that? Generating an image from text can be easy with PHP and GD. This requires GD 1.8 or higher. Check out www.php.net for an exact description of the php functions used in this code. &#8230; <a href="http://www.matthewstevenkelly.com/blog/random/generating-image-from-text.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><?php<br />
if(!isset($_POST['text_value']))<br />
{<br />
$_POST['text_value'] = "This is an image displaying text from the input box";<br />
}<br />
?></p>
<p><img src="/includes/generate_image.php?text=<?php echo $_POST['text_value'];?>&#8221; alt=&#8221;<?php echo $_POST['text_value'];?>&#8221; /></p>
<form method="post" action="http://www.matthewstevenkelly.com/blog/kb/php/generating-image-from-text.html">
<p>Text to display:<br />
<input type="text" maxlength="60" style="width:100%;" name="text_value" value="<?php echo $_POST['text_value'];?>&#8221; /></p>
<input type="submit" name="Generate image" />
</form>
<p>How did I do that?</p>
<p>Generating an image from text can be easy with PHP and GD. This requires GD 1.8 or higher.</p>
<p>Check out www.php.net for an exact description of the php functions used in this code. They are all pretty much built in GD functions.</p>
<p>Now the part that make this code great is that there is no temporary files saved on the server, it is all done in memory. </p>
<p>Save this code as <i>generate_image.php</i></p>
<div style="background-color:#CCCCCC;overflow-x:auto;width:100%;">
<p>&lt;?php<br />
function trimLength($data,$len)<br />
{<br />
  if(strlen($data)>$len)<br />
  {<br />
    $data = substr($data,0,$len);<br />
  }<br />
  return $data;<br />
}</p>
<p>function filterText($data)<br />
{<br />
  return preg_replace(&quot;/[^A-Za-z0-9.,\s\s+]/&quot;,&quot;&quot;,$data);<br />
}</p>
<p>$text = filterText($_GET[&apos;text&apos;]); // remove all illegal characters<br />
$text = trimLength($text, 60); // trim to sixty characters<br />
if($text == &quot;&quot;) { $text = &quot;Text&quot;; }</p>
<p>$font  = 4;<br />
$width  = ImageFontWidth($font) * strlen($text);<br />
$height = ImageFontHeight($font);</p>
<p>header(&quot;Content-type: image/gif;&quot;);<br />
$im = @imagecreatetruecolor($width, $height)<br />
      or die(&apos;Cannot Initialize new GD image stream&apos;);<br />
$text_color = imagecolorallocate($im, 0, 0, 0);<br />
$COULEUR_BLANC=imagecolorallocate($im,255,255,255) ;<br />
imagefilledrectangle($im,0,0,$width,$height,$COULEUR_BLANC) ;<br />
imagestring($im, $font, 0, 0, $text, $text_color);<br />
imagegif($im);<br />
imagedestroy($im);<br />
?&gt;</p></div>
<p>And then on the page you want the image created add this code</p>
<div style="background-color:#CCCCCC;overflow-x:auto;width:100%;">
&lt;img src=&quot;generate_image.php?text=The security code is 9999&quot; alt=&quot;&quot; /&gt;
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.matthewstevenkelly.com/blog/random/generating-image-from-text.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

