<?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>Daniel Eneström &#187; Image processing</title>
	<atom:link href="http://blog.enestrom.com/tag/image-processing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.enestrom.com</link>
	<description>Ramblings of a web developer</description>
	<lastBuildDate>Wed, 27 Jan 2010 19:49:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>phpThumb and imagerotate()</title>
		<link>http://blog.enestrom.com/20090509/phpthumb-and-imagerotate/</link>
		<comments>http://blog.enestrom.com/20090509/phpthumb-and-imagerotate/#comments</comments>
		<pubDate>Sat, 09 May 2009 09:41:12 +0000</pubDate>
		<dc:creator>Daniel Eneström</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Image processing]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[imagerotate()]]></category>
		<category><![CDATA[phpThumb]]></category>

		<guid isPermaLink="false">http://blog.enestrom.com/?p=90</guid>
		<description><![CDATA[One of the PHP libraries I use the most is phpThumb, because of its ease to use and portability. Simply putting the folder in the web root is usually sufficient to get started. One thing has annoyed me for a while though, and it is that I couldn&#8217;t get the rotating commands to work.
After some [...]]]></description>
			<content:encoded><![CDATA[<p>One of the PHP libraries I use the most is phpThumb, because of its ease to use and portability. Simply putting the folder in the web root is usually sufficient to get started. One thing has annoyed me for a while though, and it is that I couldn&#8217;t get the rotating commands to work.</p>
<p>After some research I discovered that the <a href="http://se2.php.net/imagerotate()" target="_blank">imagerotate()</a> function in GD library (a library that handles many of the graphics processing in phpThumb) is only available in the bundled version of GD library (that is: the version that sometimes comes bundled with PHP). That is not the case for me and I have my own server with lots of sites on, and didn&#8217;t feel like recompiling PHP just to get the imagerotate function to work.</p>
<p>So, in this new light, I looked for a function replacement using <a href="http://www.imagemagick.org/script/index.php" target="_blank">ImageMagick</a> instead (a much better image processing library, but rarely installed by default) and found this one somewhere. Simply put it at the bottom of your <em>phpthumb.functions.php </em>file and *poof* rotating images in phpThumb will start working.</p>
<div class="geshi no php">
<div class="head">if ( !function_exists( &#39;imagerotate&#39; ) ) {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span> imagerotate<span class="br0">&#40;</span> <span class="re1">$source_image</span><span class="sy0">,</span> <span class="re1">$angle</span><span class="sy0">,</span> <span class="re1">$bgd_color</span><span class="sy0">=</span><span class="kw2">null</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$angle</span> <span class="sy0">=</span> <span class="nu0">360</span><span class="sy0">-</span><span class="re1">$angle</span><span class="sy0">;</span> <span class="co1">// GD rotates CCW, imagick rotates CW</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$temp_src</span> <span class="sy0">=</span> <span class="st0">&#39;/tmp/temp_src.png&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$temp_dst</span> <span class="sy0">=</span> <span class="st0">&#39;/tmp/temp_dst.png&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>imagepng<span class="br0">&#40;</span><span class="re1">$source_image</span><span class="sy0">,</span><span class="re1">$temp_src</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$imagick</span> <span class="sy0">=</span> <span class="kw2">new</span> Imagick<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$imagick</span><span class="sy0">-&gt;</span><span class="me1">readImage</span><span class="br0">&#40;</span><span class="re1">$temp_src</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$imagick</span><span class="sy0">-&gt;</span><span class="me1">rotateImage</span><span class="br0">&#40;</span><span class="kw2">new</span> ImagickPixel<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re1">$angle</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$imagick</span><span class="sy0">-&gt;</span><span class="me1">writeImage</span><span class="br0">&#40;</span><span class="re1">$temp_dst</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//trigger_error( &#39;imagerotate(): could not write to &#39; . $file1 . &#39;, original image returned&#39;, E_USER_WARNING );</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$result</span> <span class="sy0">=</span> imagecreatefrompng<span class="br0">&#40;</span><span class="re1">$temp_dst</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">unlink</span><span class="br0">&#40;</span><span class="re1">$temp_dst</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">unlink</span><span class="br0">&#40;</span><span class="re1">$temp_src</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$result</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.enestrom.com%2F20090509%2Fphpthumb-and-imagerotate%2F&amp;linkname=phpThumb%20and%20imagerotate%28%29"><img src="http://blog.enestrom.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.enestrom.com/20090509/phpthumb-and-imagerotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
