<?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>Subesh Pokhrel&#039;s Blog - Magento Development Tips &#187; text</title>
	<atom:link href="http://subesh.com.np/tag/text/feed/" rel="self" type="application/rss+xml" />
	<link>http://subesh.com.np</link>
	<description>PHP &#38; Magento Tips and Tutorials</description>
	<lastBuildDate>Tue, 20 Mar 2012 18:15:10 +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>Converting Text to Image in PHP formatted by alignment</title>
		<link>http://subesh.com.np/2009/11/converting-text-to-image-in-php-formatted-by-alignment/</link>
		<comments>http://subesh.com.np/2009/11/converting-text-to-image-in-php-formatted-by-alignment/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 13:34:31 +0000</pubDate>
		<dc:creator>Subesh Pokhrel</dc:creator>
				<category><![CDATA[GD]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://subesh.com.np/?p=108</guid>
		<description><![CDATA[During one of my project works I came across a situation like changing the input of TINYMCE editor to image. Can you believe what can be the user&#8217;s requirement? Basically what the client needed was to change the text to image and text should be in American Typewriter Font with the option of showing text [...]]]></description>
			<content:encoded><![CDATA[<p>During one of my project works I came across a situation like changing the input of TINYMCE editor to image. Can you believe what can be the user&#8217;s requirement? <img src='http://subesh.com.np/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  Basically what the client needed was to change the text to image and text should be in American Typewriter Font with the option of showing text as image formatted by alignment. i.e Left Alignment, Center Alignment or Right Alignment in a white background image. Then I did some research, this is another way of saying I Googled a lot! lol. I came across two scripts, one which converted text to image and another a function to set alignment. So I thought that why not combine both the codes and publish for others, who may need it as well.</p>
<p><strong>The compiled code</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * Class for converting Text to Image.
 * Font type can be specified
 * The alignment where the text will echo can also be set.
 *
 * @compiled Subesh Pokhrel from PHP.net and PHPclasses.org
 *
 */
define(&quot;ALIGN_LEFT&quot;, &quot;left&quot;);
define(&quot;ALIGN_CENTER&quot;, &quot;center&quot;);
define(&quot;ALIGN_RIGHT&quot;, &quot;right&quot;);

class TextToImage {

	private $im;

	/**
	 * @name 				   : makeImageF
	 *
	 * Function for create image from text with selected font.
	 *
	 * @param String $text     : String to convert into the Image.
	 * @param String $font     : Font name of the text.
	 * @param int    $W        : Width of the Image.
	 * @param int    $H        : Hight of the Image.
	 * @param int	 $X        : x-coordinate of the text into the image.
	 * @param int    $Y        : y-coordinate of the text into the image.
	 * @param int    $fsize    : Font size of text.
	 * @param array  $color	   : RGB color array for text color.
	 * @param array  $bgcolor  : RGB color array for background.
	 *
	 */
	public function makeImageF($text, $font=&quot;CENTURY.TTF&quot;, $W=800, $H=200, $X=0, $Y=0, $fsize=18, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){

		$this-&gt;im = @imagecreate($W, $H)
		or die(&quot;Cannot Initialize new GD image stream&quot;);

		$background_color = imagecolorallocate($this-&gt;im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);		//RGB color background.
		$text_color = imagecolorallocate($this-&gt;im, $color[0], $color[1], $color[2]);			//RGB color text.

		$this-&gt;imagettftextbox($this-&gt;im, $fsize,0, $X,$Y, $text_color, $font, $text,800);
	}

	/**
	* This function works to set alignment in image and write image.
	*/
	public function imagettftextbox(&amp;$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
	{
		$text_lines = explode(&quot;\n&quot;, $text); // Supports manual line breaks!

		$lines = array();
		$line_widths = array();

		$largest_line_height = 0;

		foreach($text_lines as $block)
		{
			$current_line = ''; // Reset current line
			$align=ALIGN_CENTER; // Setting Alignment
			$words = explode(' ', $block); // Split the text into an array of single words

			$first_word = TRUE;

			$last_width = 0;

			for($i = 0; $i &lt; count($words); $i++)
			{
				$item = $words[$i];
				$dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
				$line_width = $dimensions[2] - $dimensions[0];
				$line_height = $dimensions[1] - $dimensions[7];

				if($line_height &gt; $largest_line_height) $largest_line_height = $line_height;

				if($line_width &gt; $max_width &amp;&amp; !$first_word)
				{
					$lines[] = $current_line;

					$line_widths[] = $last_width ? $last_width : $line_width;

					/*if($i == count($words))
					 {
					 continue;
					 }*/

					$current_line = $item;
				}
				else
				{
					$current_line .= ($first_word ? '' : ' ') . $item;
				}

				if($i == count($words) - 1)
				{
					$lines[] = $current_line;

					$line_widths[] = $line_width;
				}

				$last_width = $line_width;

				$first_word = FALSE;
			}

			if($current_line)
			{
				$current_line = $item;
			}
		}

		$i = 0;
		foreach($lines as $line)
		{
			if($align == ALIGN_CENTER)
			{
				$left_offset = ($max_width - $line_widths[$i]) / 2;
			}
			elseif($align == ALIGN_RIGHT)
			{
				$left_offset = ($max_width - $line_widths[$i]);
			}
			imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
			$i++;
		}

		return $largest_line_height * count($lines);
	}

	/**
	 * @name showAsPng
	 *
	 * Function to show text as Png image.
	 *
	 */
	public function showAsPng(){

		header(&quot;Content-type: image/png&quot;);
		return imagepng($this-&gt;im);
	}

}
?&gt;
</pre>
<p>Save this file as <strong>TextToImage.class.php</strong>.</p>
<p>And then use this code to call the classes instance.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/** Downloaded from PHP classes
* Please note that KORONG.TTF font file should be present to run the code.
*/

ini_set(&quot;display_errors&quot;,1);
require_once('TextToImage.class.php');
$_im = new TextToImage();
$_im-&gt;makeImageF(&quot;Thank you ! Subesh Pokhrel \n subesh.com.np&quot;,&quot;KORONG.TTF&quot;);
$_im-&gt;showAsPng();
?&gt;
</pre>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://subesh.com.np/2009/11/converting-text-to-image-in-php-formatted-by-alignment/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

