<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged resize</title>
    <link href="https://passingcuriosity.com/tags/resize/resize.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/resize/resize.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2010-10-11T00:00:00Z</updated>
    <entry>
    <title>Using ImageMagick and GD2 together with Drupal ImageCache</title>
    <link href="https://passingcuriosity.com/2010/drupal-imagecache-imagemagick-gd/" />
    <id>https://passingcuriosity.com/2010/drupal-imagecache-imagemagick-gd/</id>
    <published>2010-10-11T00:00:00Z</published>
    <updated>2010-10-11T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>A lot of Drupal sites use <a href="http://drupal.org/project/imagecache">ImageCache</a> and <a href="http://drupal.org/project/imageapi">ImageAPI</a> to automatically
scale and process images for display. Most of them (the vast majority, unless
I miss my guess) will be using the <a href="http://www.php.net/gd">GD</a> toolkit and running with <code>mod_php</code>
embedded in Apache. This is fine in most cases, but sometimes it’s definitely
the wrong thing to do; in my case, I’m processing large image files (~4M or
so) and keeping my memory limit down. The easiest way to do this is to offload
the processing to another process. Something like ImageMagick perhaps? With a
custom action from <a href="http://drupal.org/project/imagecache_actions">ImageCache Actions</a>, this is simple!</p>
<p>A quick Google led me to an article called <a href="http://drupal.org/node/641372">Create PDF thumbnails with
imagecache and ImageMagick while GD is still the default
toolkit</a> which supplies the following code
(simplified a little by removing the PDF-y bits):</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode php"><code class="sourceCode php"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">&lt;?php</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">$w</span> <span class="op">=</span> <span class="dv">246</span><span class="ot">;</span> <span class="co">// change to your preferred thumbnail width</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="op">!</span>_imageapi_imagemagick_convert(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'[0]'</span><span class="ot">,</span> <span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span><span class="ot">,</span> <span class="dt">array</span>(<span class="dv">0</span> =&gt; <span class="st">'-thumbnail '</span><span class="op">.</span><span class="va">$w</span>))) <span class="cf">return</span> <span class="kw">FALSE</span><span class="ot">;</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="va">$img</span> <span class="op">=</span> <span class="fu">imagecreatefrompng</span>(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">;</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>file_delete(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">;</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;<span class="kw">resource</span> <span class="op">=</span> <span class="va">$img</span><span class="ot">;</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;toolkit <span class="op">=</span> <span class="st">'imageapi_gd'</span><span class="ot">;</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;info <span class="op">=</span> <span class="dt">array</span>(<span class="st">'extension'</span> =&gt; <span class="st">'jpeg'</span>)<span class="ot">;</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="cf">return</span> <span class="kw">TRUE</span><span class="ot">;</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">?&gt;</span></span></code></pre></div>
<p>Alas, this code it pretty useless: because it stomps on <code>$image-&gt;info</code> any
further actions on this <code>$image</code> will probably break.</p>
<p>Thankfully, there’s an easy fix: when you update <code>$image</code>, make sure you
update everything that needs fixing. Here’s the amended code:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode php"><code class="sourceCode php"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">&lt;?php</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co">// &quot;Thumbnail&quot; the image</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="va">$width</span> <span class="op">=</span> <span class="dv">600</span><span class="ot">;</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="op">!</span>_imageapi_imagemagick_convert(<span class="va">$image</span>-&gt;source<span class="ot">,</span> <span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span><span class="ot">,</span> <span class="dt">array</span>(<span class="dv">0</span> =&gt; <span class="st">'-thumbnail '</span><span class="op">.</span><span class="va">$width</span>))) <span class="cf">return</span> <span class="kw">FALSE</span><span class="ot">;</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="co">// Load it back in as a GD resource</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="va">$img</span> <span class="op">=</span> <span class="fu">imagecreatefrompng</span>(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">;</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="co">// Get the &quot;deets&quot; on the new image</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="va">$info</span> <span class="op">=</span> <span class="fu">getimagesize</span>(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">;</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;<span class="kw">resource</span> <span class="op">=</span> <span class="va">$img</span><span class="ot">;</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;toolkit <span class="op">=</span> <span class="st">'imageapi_gd'</span><span class="ot">;</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="va">$image</span>-&gt;info <span class="op">=</span> <span class="dt">array</span>(</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>  <span class="st">'width'</span> =&gt; <span class="va">$info</span>[<span class="dv">0</span>]<span class="ot">,</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>  <span class="st">'height'</span> =&gt; <span class="va">$info</span>[<span class="dv">1</span>]<span class="ot">,</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>  <span class="st">'extension'</span> =&gt; <span class="st">'png'</span><span class="ot">,</span></span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a>  <span class="st">'file_size'</span> =&gt; <span class="fu">filesize</span>(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">,</span></span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a>  <span class="st">'mime_type'</span> =&gt; <span class="va">$info</span>[<span class="st">'mime'</span>]<span class="ot">,</span></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a>)<span class="ot">;</span></span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a><span class="co">// Clean up</span></span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a>file_delete(<span class="va">$image</span>-&gt;source<span class="op">.</span><span class="st">'.png'</span>)<span class="ot">;</span></span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a><span class="cf">return</span> <span class="va">$image</span><span class="ot">;</span></span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a><span class="kw">?&gt;</span></span></code></pre></div>
<p>Make sure that you’ve enabled the ImageMagick toolkit, drop this code in a
custom action and you’ll be on externally processing images in no time!</p>]]></summary>
</entry>

</feed>
