title: ImageMagick: convert png fail via PHP and works via bash shell
body: <p>I've got a very weird bug which I've yet to find a solution. <strong>UPDATE</strong> see solution below</p>
  
  <p>What I am trying to do is convert a full size picture into a 160x120 thumbnail. It works great with jpg and jpeg files of any size, but not with png.</p>
  
  <p>ImageMagick command: </p>
  
  <pre><code>/opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x&lt;' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'
  </code></pre>
  
  <p>PHP function (shortened)</p>
  
  <pre><code>...
  $cmd = "/opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x&lt;' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'";
  exec($cmd, $output, $retval);
  $errors += $retval;
  if ($errors &gt; 0) {
      die(print_r($output));
  }
  </code></pre>
  
  <p>When this function runs $retval equal 1 which means the convert command failed (thumbnail isn't created).</p>
  
  <p>This is where it gets interesting, if I run the exact same command in my shell, it works.</p>
  
  <pre><code>wedbook:~ wedix$ /opt/local/bin/convert '/WEBSERVER/images/img_0003-192-10.png' -thumbnail x320 -resize '320x&lt;' -resize 50% -gravity center -crop 160x120+0+0 +repage -quality 91 '/WEBSERVER/thumbs/small_img_0003-192-10.png'
  wedbook:~ wedix$ 
  </code></pre>
  
  <p>I've tried using different PHP function such as system, passthru but it didn't work. I thought maybe someone here knew the solution.</p>
  
  <p>I'm using</p>
  
  <ol>
  <li><code>MAMP 1.7.2</code>
  
  <ul>
  <li><code>Apache/2.0.59</code></li>
  <li><code>PHP/5.2.6</code></li>
  </ul></li>
  </ol>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I updated the following dependencies</p>
  
  <ol>
  <li><code>libpng from 1.2.35 to 1.2.37</code></li>
  <li><code>libiconv from 1.12_2 to 1.13_0</code></li>
  <li><code>ImageMagick 6.5.2-4_1 to 6.5.2-9_0</code></li>
  </ol>
  
  <p>However, it did not fix my problem.</p>
  
  <p><strong>2nd UPDATE</strong></p>
  
  <p>I finally found something that might help, when the function runs this is what gets printed in the Apache logs:</p>
  
  <pre><code>dyld: Library not loaded: /opt/local/lib/libiconv.2.dylib
    Referenced from: /opt/local/bin/convert
    Reason: Incompatible library version: convert requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0
  </code></pre>
  
  <p><strong>3rd UPDATE</strong></p>
  
  <p>libiconv.2.dylib is version 8.0.0...</p>
  
  <pre><code>bash-3.2$ otool -L /opt/local/lib/libiconv.2.dylib 
  /opt/local/lib/libiconv.2.dylib:
      /opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.0.0)
      /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
      /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)
  </code></pre>
  
  <p><strong>4th UPDATE</strong></p>
  
  <p>Problem was related to MAMP, see solution below</p>