<?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>Jeremy Cook &#187; Imagick</title>
	<atom:link href="http://jeremycook.ca/tag/imagick/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremycook.ca</link>
	<description>Random musings on web development and PHP</description>
	<lastBuildDate>Thu, 10 May 2012 02:27:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Installing Imagick under Apache on Windows</title>
		<link>http://jeremycook.ca/2010/06/06/installing-imagick-under-apache-on-windows/</link>
		<comments>http://jeremycook.ca/2010/06/06/installing-imagick-under-apache-on-windows/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 16:45:05 +0000</pubDate>
		<dc:creator>Jeremy Cook</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[Imagick]]></category>

		<guid isPermaLink="false">http://jeremycook.ca/?p=114</guid>
		<description><![CDATA[After a few weeks of experimenting on and off I&#8217;ve just managed to get the Imagick extension installed under Apache on Windows (my development environment). While discovering how to make it work was not at all simple the final process of getting all the pieces to play together nicely was pretty easy. In this post [...]]]></description>
			<content:encoded><![CDATA[<p>After a few weeks  of experimenting on and off I&#8217;ve just managed to get the Imagick  extension installed under Apache on Windows (my development  environment). While discovering how to make it work was not at all  simple the final process of getting all the pieces to play together  nicely was pretty easy. In this post I&#8217;ll briefly discuss what  ImageMagick and the Imagick extension for PHP are, why you might want to  use them, how I got them up and running and finally a very simple piece  of example code. I&#8217;m hoping this post will help people avoid the  frustration and effort I went through to get them to work!</p>
<h2>What are  ImageMagick and Imagick?</h2>
<p>The <a href="http://www.imagemagick.org/script/index.php">ImageMagick  website</a> has this definition of what it is:</p>
<blockquote><p>ImageMagick<sup><a href="http://tarr.uspto.gov/servlet/tarr?regser=serial&amp;entry=78333969">®</a></sup> is a software suite to create, edit, and compose bitmap images.  It can   read, convert and write images in a variety of <a href="http://www.imagemagick.org/script/formats.php">formats</a> (over  100) including <a href="http://www.imagemagick.org/script/motion-picture.php">DPX</a>,  <a href="http://www.imagemagick.org/script/high-dynamic-range.php">EXR</a>,   GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF.    Use ImageMagick to translate, flip, mirror, rotate, scale, shear and   transform images, adjust image colors, apply various special effects, or   draw text, lines, polygons, ellipses and Bézier curves.</p></blockquote>
<p>ImageMagick is  typically accessed from the command line and is extremely versatile. It  can also be accessed from other programming languages to manipulate  images and the Windows installation includes optional components to  allow C, C++, Perl or Python to access it.</p>
<p>Imagick is a  native PHP extension that allows a developer to manipulate images using  ImageMagick. It is written and maintained by <a href="http://valokuva.org/?cat=1">Mikko Koppanen</a> and is  available on Linux/Unix as a PECL extension. Mikko Koppanen also has  dll&#8217;s available for download from his website but, as I&#8217;ll explain in a  moment, these can be somewhat problematic for most people to install.</p>
<h2>Why use Imagick  instead of GD?</h2>
<p>Most people use GD for editing images using PHP but I think Imagick offers some real advantages.</p>
<ol>
<li>Imagick/ImageMagick is supposed to be less resource intensive than GD. I can&#8217;t confirm this as I&#8217;ve only read it but if true it could be reason enough to switch.</li>
<li>Imagick offers a fully object oriented interface, throwing exceptions on errors. I personally find this much easier to use than using GD&#8217;s functions and resources, having to check for problems after each function call. With Imagick any errors raise an exception which is easy to handle.</li>
<li>With Imagick my code seems to be simpler and easier to maintain. Again this is a personal opinion but I find I can achieve the same operations with fewer lines of code using Imagick</li>
</ol>
<p>The trick of course is to find a hosting company that has Imagick installed but I can think of several here in North America. The other major problem in using Imagick is that the documentation on the PHP website is scarce at best. Hopefully this will improve over time.</p>
<h2>Why is installing Imagick on Windows difficult?</h2>
<p>Installing Imagick and ImageMagick on Linux/Unix should be pretty easy, I imagine. Installing ImageMagick on Windows is also easy as Windows binaries are available from <a href="http://www.imagemagick.org/script/index.php">their website</a>. If you&#8217;re running PHP on Windows under IIS installing Imagick should also be a simple matter of downloading the dll files from <a href="http://valokuva.org/?p=161">Mikko Koppanen&#8217;s website</a>. Things get tricky if you&#8217;re running PHP under Apache on Windows. The problem is that the official Apache binaries available from the Apache foundation are compiled using Visual C 6 (VC6) while the the Imagick dll file from Mikko Koppanen is compiled using Visual C 9 (VC9). These cannot be used together. If you&#8217;re running PHP under IIS though this shouldn&#8217;t be a problem. The version of PHP that runs under IIS is compiled using VC9 and Mikko Koppanen&#8217;s dll file should load without any problems. Someone else has compiled <a href="http://www.sk89q.com/2010/03/vc6-windows-binaries-for-imagick-2-3-0/">Imagick using VC6</a> but I could not get this to work properly. I managed to get the extension loaded but it kept crashing with weird errors. After a lot of rooting around I finally found <a href="http://www.php.de/tutorials/62106-windows-apache-php-imagick.html">this webpage</a> (in German) that pointed me in the right direction. So, here are the steps I went through to make everything work.</p>
<h2>Installing Imagick and ImageMagick</h2>
<p>These are the steps I went through to get Imagick and ImageMagick working for me. I can&#8217;t promise that it will work for everyone though. Before beginning un-install your current version of Apache and PHP as we&#8217;ll be installing a new version. Make sure you keep any config files that you want to use in your new installation though.</p>
<ol>
<li>Download and install <a href="http://www.imagemagick.org/script/binary-releases.php#windows">ImageMagick from here</a>. I installed ImageMagick-6.6.2-2-Q16-windows-dll.exe. At the end of the installation follow the instructions and open a command prompt to try out ImageMagick. If everything has worked you should see a copy of the ImageMagick logo. Also try to install ImageMagick somewhere easy to find, like C:\imagemagick which is where I put it.</li>
<li>If you&#8217;re running a version of windows that is earlier than Windows 7 install the Visual C++ 2008 redistributable from <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF&amp;displaylang=en">here</a>.</li>
<li>Download a version of Apache compiled with VC9 from ApacheLounge <a href="http://www.apachelounge.com/download/">Apache on Windows page</a>. These are exactly the same as the official Apache binaries but compiled with VC9. According to the website these should have better perfomance, stability and memory management than binaries compiled with VC6.</li>
<li>Install Apache by extracting the files to a location that you want (I use C:\Apache). Start Apache by opening a command prompt, navigating to the bin directory and typing &#8216;httpd.exe&#8217;. At this point if you go to localhost in your browser you should see the &#8216;It works!&#8217; message. Install Apache as a service (if you wish) by typing &#8216;httpd.exe -k install&#8217; at the command prompt. Full instructions are included in the files downloaded in step 3.</li>
<li>Download PHP from the official <a href="http://windows.php.net/download/">PHP on Windows site</a> and install it, restarting Apache afterwards. Make sure you get binaries compiled with VC9. At this point create a phpinfo() file to verify everything is working.</li>
<li>Download the correct <a href="http://valokuva.org/builds/">Imagick dll file</a>. Make sure that if you installed a thread safe version of PHP you get the thread safe version and vice versa. Copy the dll file to your PHP ext directory and add a line to your php.ini extensions section to load the extension. Restart Apache again.</li>
<li>At this point if you view the output of phpinfo() you should see that the Imagick extension has loaded.</li>
<li>Install any other custom PHP extensions you need, remembering to use dll&#8217;s compiled with VC9.</li>
</ol>
<p>Hopefully this should work. If anyone has any problems please post here and I&#8217;ll try to help. I must emphasise again though that I can&#8217;t guarantee this will work for everyone. I can only explain the steps that worked for me.</p>
<h2>A Simple Example using Imagick</h2>
<p>Here&#8217;s a very simple script using Imagick to resize an image. This barely scratches the surface of what the extension can do but it demonstrates how easy it is to perform this very common operation.</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php
try {
$im = new Imagick();
$im-&gt;readImage('test.jpg');
if ($im-&gt;getImageHeight() &gt; 500) {
$im-&gt;resizeImage(0, 500, Imagick::FILTER_LANCZOS, 1);
}
$im-&gt;sepiaToneImage(90.5);
header('Content-type: ' . $im-&gt;getFormat());
echo $im-&gt;getImageBlob();
}
catch (Exception $e) {
echo $e-&gt;getMessage();
}
?&gt;
</pre>
<p>This code creates a new Imagick object and reads an image in from a file. It then check the image height and resizes it if it is higher than 500px. The Imagick::resizeImage() method needs a little explanation. The first two arguments are width and height. If either of these are left as 0 the image is resized proportionately to the other dimension. The third argument is a ImageMagick resize filter to use (the PHP manual has a full list of all Imagick constants) and the fourth argument details whether to blur or sharpen the image (&lt; 1 is sharpening, &gt; 1 is blurring). The script then converts the image to sepia just for fun before outputting it to the browser. To me this code is far simpler than the equivalent of what would have to be written using GD.</p>
<p>I hope this post has been useful as a brief introduction to Imagick and how I got it to work on Windows, hopefully saving someone the time and frustration I went through. I&#8217;ll share more thoughts on using Imagick over the coming weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremycook.ca/2010/06/06/installing-imagick-under-apache-on-windows/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

