<?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; PHP</title>
	<atom:link href="http://enestrom.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://enestrom.com</link>
	<description>Keep It Simply Stupid</description>
	<lastBuildDate>Fri, 23 Dec 2011 00:50:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>phpThumb and imagerotate()</title>
		<link>http://enestrom.com/20090509/phpthumb-and-imagerotate/</link>
		<comments>http://enestrom.com/20090509/phpthumb-and-imagerotate/#comments</comments>
		<pubDate>Sat, 09 May 2009 09:41:12 +0000</pubDate>
		<dc:creator>daniel</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 &#8230; <a href="http://enestrom.com/20090509/phpthumb-and-imagerotate/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20090509/phpthumb-and-imagerotate/' addthis:title='phpThumb and imagerotate() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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>
<pre lang="php">if ( !function_exists( 'imagerotate' ) ) {
    function imagerotate( $source_image, $angle, $bgd_color=null ) {
        $angle = 360-$angle; // GD rotates CCW, imagick rotates CW
        $temp_src = '/tmp/temp_src.png';
        $temp_dst = '/tmp/temp_dst.png';
        if (!imagepng($source_image,$temp_src)){
            return false;
        }
        $imagick = new Imagick();
        $imagick->readImage($temp_src);
        $imagick->rotateImage(new ImagickPixel(), $angle);
        $imagick->writeImage($temp_dst);
        //trigger_error( 'imagerotate(): could not write to ' . $file1 . ', original image returned', E_USER_WARNING );
        $result = imagecreatefrompng($temp_dst);
        unlink($temp_dst);
        unlink($temp_src);
        return $result;
    }
}</pre>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20090509/phpthumb-and-imagerotate/' addthis:title='phpThumb and imagerotate() ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://enestrom.com/20090509/phpthumb-and-imagerotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AMFPHP and Flex</title>
		<link>http://enestrom.com/20080808/amfphp-and-flex/</link>
		<comments>http://enestrom.com/20080808/amfphp-and-flex/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 13:44:45 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AMFPHP]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Remoting]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://blog.enestrom.com/?p=13</guid>
		<description><![CDATA[Being a LAMP (Linux Apache MySQL PHP) guy as well as a Flex developer it didn&#8217;t take me long to discover AMFPHP. If you haven&#8217;t heard of it I can tell you in short it&#8217;s a framework for sending and &#8230; <a href="http://enestrom.com/20080808/amfphp-and-flex/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20080808/amfphp-and-flex/' addthis:title='AMFPHP and Flex ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Being a LAMP <em>(Linux Apache MySQL PHP)</em> guy as well as a Flex developer it didn&#8217;t take me long to discover <a href="http://www.amfphp.org/" target="_blank">AMFPHP</a>. If you haven&#8217;t heard of it I can tell you in short it&#8217;s a framework for sending and receiving data between Actionscript and PHP, without having to use an XML layer or similar in between, so to speak. It&#8217;s really neat, because lets say you extract an array of rows from your MySQL database, then you can just pass the entire array back to Actionscript and it will be received as an Actionscript array in the other end.</p>
<p>Would you like to try it? This is how to do it:</p>
<p>1. Download the latest AMFPHP package from <a href="http://www.amfphp.org/" target="_blank">www.amfphp.org</a></p>
<p>2. Unpack and (preferably) place the folder called <em>amfphp</em> in the document root on your web server.</p>
<p>3. By default the AMFPHP gateway is set to converting all UTF-8 data to Latin 1. Big NO NO for &#8220;international&#8221; developers like me, so you should open the <em>gateway.php</em> file in the <em>amfphp</em> folder for editing and change the following line (it should be around line 127):</p>
<pre lang="php">
$gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");
</pre>
<p>to </p>
<pre lang="php">
$gateway->setCharsetHandler("iconv","UTF-8","UTF-8");
</pre>
<p>If you don&#8217;t do this all UTF-8 special characters from the database will be distorted on arriving at the Flex application.</p>
<p>4. Now create a simple PHP class file, call it <em>HelloWorld.php</em>, and place it in the <em>/amfphp/services</em> folder. You should use a commenting pattern like that of PHPDoc. Here is a simple HelloWorld example:</p>
<pre lang="php">/**
 * A simple amfphp service.
 */
class HelloWorld
{
	/**
	 * A simple HelloWorld function
	 * @returns A string containing the phrase 'Hello World!'
	 */
	function SayHello()
	{
		return "Hello World!";
	}

	/**
	 * A simple HelloWorld function that bounces back the given string
	 * @returns A string containing the supplied string
	 */
	function SayWhat($string)
	{
		return $string;
	}
}
</pre>
<p>In this class I&#8217;ve put two functions; one that returns the string &#8216;Hello World!&#8217; and another that returns whatever string you send to it. Okay, so let&#8217;s get on with the show&#8230;</p>
<p>5. Now you can test your service by browsing to the <em>browser</em> folder. It should have a URL similar to this: <em>yourserver.com/amfphp/browser</em>. In the service browser you can test whether your class is working as it should.</p>
<p>6. <strong>Flex<br />
</strong>To simplify things I&#8217;ve created a small class for the implementation of this in Flex. There is a number of ways of doing this, but I&#8217;ve chosen the one way that I am most comfortable with.</p>
<p>For ease of use, create an actionscript document in your Flex Projects root directory called <em>AMFPHP.as</em> and past the following code into the file:</p>
<pre lang="actionscript">package {
	import flash.net.NetConnection;
	import flash.net.Responder;

	public class AMFPHP
	{
		private static var gateway:String = "http://www.yourserver.com/amfphp/gateway.php";
		private static var connection:NetConnection = new NetConnection;
		connection.connect(gateway);

		public function AMFPHP() : void
		{
			// Static class
		}

		public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, ... args:*) : void
		{
			trace("AMFPHP("+serviceFunction+")");
			// Create responder
			var responder:Responder = new Responder(resultHandler, faultHandler);
			// Create an array that will temporarily store all the arguments
			var collectArgs:Array = new Array;
			// Add the fixed arguments
			collectArgs.push(serviceFunction);
			collectArgs.push(responder);
			// Loop through the optional arguments and add them too
			for (var i:uint=0; i<args.length; i++)
			{
				collectArgs.push(args[i]);
			}
			// Create a reference to the function we will call
			var callFunction:Function = connection.call;
			// Call the function using the arguments
			callFunction.apply(connection,collectArgs);
		}
	}
}
</pre>
<p>The only thing you have to change here is the URL to the <em>gateway.php </em>file on your server at line 7 in the file.</p>
<p>7. Usage</p>
<p>To use this in your code you will need three things:</p>
<p>• Calling the service using the static AMFPHP.send() function<br />
• A function that handles the result coming from amfphp<br />
• A function that handles errors</p>
<p>This is the first HelloWorld example (without the argument). The first argument for the send() function is a string describing the path to the function, with this structure: <em>[directory.]class.function</em>, within the services folder. A directory is not necessary, but can be convenient if you have packages of many classes.</p>
<pre lang="actionscript">private function helloWorld() : void
{
	AMFPHP.send("HelloWorld.SayHello",onResult,onFault);
}

private function onResult(result:String) : void
{
	trace(result);
}

private function onFault(result:Object) : void
{
	trace(String(result.description));
}
</pre>
<p>Easy huh!?</p>
<p>And the example with the argument is pretty much the same, but with an argument added to the end of the function call. Like this:</p>
<pre lang="actionscript">private function helloWorld() : void
{
	AMFPHP.send("HelloWorld.SayWhat", onResult, onFault, "Hello to you!");
}

private function onResult(result:String) : void
{
	trace(result);
}

private function onFault(result:Object) : void
{
	trace(String(result.description));
}
</pre>
<p>Note that I changed the path to the function from <em>HelloWorld.SayHello</em> to <em>HelloWorld.SayWhat</em> to use the other function in the class.</p>
<p>Well, that's it. Hope it was of some use. Good luck!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20080808/amfphp-and-flex/' addthis:title='AMFPHP and Flex ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://enestrom.com/20080808/amfphp-and-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

