<?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; Image</title>
	<atom:link href="http://subesh.com.np/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://subesh.com.np</link>
	<description>PHP &#38; Magento Tips and Tutorials</description>
	<lastBuildDate>Wed, 01 Feb 2012 04:09:38 +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>
		<item>
		<title>Image Resize In Magento &amp; cache the resized image</title>
		<link>http://subesh.com.np/2009/11/image-resize-magento-cache-resized-image/</link>
		<comments>http://subesh.com.np/2009/11/image-resize-magento-cache-resized-image/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 12:50:16 +0000</pubDate>
		<dc:creator>Subesh Pokhrel</dc:creator>
				<category><![CDATA[Custom Module]]></category>
		<category><![CDATA[Magento]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://subesh.com.np/?p=105</guid>
		<description><![CDATA[Handling Products image in Magento is quite easy to do. Magento has deviced as very good class and function to create product&#8217;s image by defining widht and height and in addition it caches that image created if you want to get the same sized image next time. But managing other images like category images or [...]]]></description>
			<content:encoded><![CDATA[<p>Handling Products image in Magento is quite easy to do. Magento has deviced as very good class and function to create product&#8217;s image by defining widht and height and in addition it caches that image created if you want to get the same sized image next time. But managing other images like category images or other image you may need in your module can be quite troublesome. I went to the core of the function used to cache product&#8217;s image and came up with the script which can be used to cache any other images, like the product&#8217;s image.</p>
<p>Basically the theroy is to create the image of sizes defined in the function parameter and while it is being fetched for the second time checked if the same size image exists or not. If yes then returnt the http url of that image else create new image.Next trick is to save the new size image in the folder with name as widhtXheight (101X65), which can be helpful to check if image exists or not for the given width and height parameters.</p>
<p>Below is the code for doing this, you can place this code in your helper, for easy access.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * @author Subesh Pokhrel
 * @website subesh.com.np
 *
 */
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract
{

	/**
	 * Returns the resized Image URL
	 *
	 * @param string $imgUrl - This is relative to the the media folder (custom/module/images/example.jpg)
	 * @param int $x Width
	 * @param int $y Height
	 */
	public function getResizedUrl($imgUrl,$x,$y=NULL){
		$imgPath=$this-&gt;splitImageValue($imgUrl,&quot;path&quot;);
		$imgName=$this-&gt;splitImageValue($imgUrl,&quot;name&quot;);

		/**
		 * Path with Directory Seperator
		 */
		$imgPath=str_replace(&quot;/&quot;,DS,$imgPath);

		/**
		 * Absolute full path of Image
		 */
		$imgPathFull=Mage::getBaseDir(&quot;media&quot;).DS.$imgPath.DS.$imgName;

		/**
		 * If Y is not set set it to as X
		 */
		$widht=$x;
		$y?$height=$y:$height=$x;

		/**
		 * Resize folder is widthXheight
		 */
		$resizeFolder=$widht.&quot;X&quot;.$height;

		/**
		 * Image resized path will then be
		 */
		$imageResizedPath=Mage::getBaseDir(&quot;media&quot;).DS.$imgPath.DS.$resizeFolder.DS.$imgName;

		/**
		 * First check in cache i.e image resized path
		 * If not in cache then create image of the width=X and height = Y
		 */
		if (!file_exists($imageResizedPath)&amp;amp;amp;&amp;amp;amp; file_exists($imgPathFull)) :
			$imageObj = new Varien_Image($imgPathFull);
			$imageObj-&gt;constrainOnly(TRUE);
			$imageObj-&gt;keepAspectRatio(TRUE);
			$imageObj-&gt;resize($widht,$height);
			$imageObj-&gt;save($imageResizedPath);
		endif;

		/**
		 * Else image is in cache replace the Image Path with / for http path.
		 */
		$imgUrl=str_replace(DS,&quot;/&quot;,$imgPath);

		/**
		 * Return full http path of the image
		 */
		return Mage::getBaseUrl(&quot;media&quot;).$imgUrl.&quot;/&quot;.$resizeFolder.&quot;/&quot;.$imgName;
	}

	/**
	 * Splits images Path and Name
	 *
	 * Path=custom/module/images/
	 * Name=example.jpg
	 *
	 * @param string $imageValue
	 * @param string $attr
	 * @return string
	 */
	public function splitImageValue($imageValue,$attr=&quot;name&quot;){
		$imArray=explode(&quot;/&quot;,$imageValue);

		$name=$imArray[count($imArray)-1];
		$path=implode(&quot;/&quot;,array_diff($imArray,array($name)));
		if($attr==&quot;path&quot;){
			return $path;
		}
		else
			return $name;

	}
}
</pre>
<p>And you can access this from anywhere by just this.</p>
<pre class="brush: php; title: ; notranslate">

/**
*But remember your base image or big image must be in Root/media/custom/module/images/example.jpg
*/

echo Mage::helper('yourmodulehelper')-&gt;getResizedUrl(&quot;custom/module/images/example.jpg&quot;,101,65)

/**
*By doing this new image will be created in Root/media/custom/module/images/101X65/example.jpg
*/
</pre>
<p>Stay tuned for more on Magento!</p>
]]></content:encoded>
			<wfw:commentRss>http://subesh.com.np/2009/11/image-resize-magento-cache-resized-image/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

