title: How can I trim just the left and right side of an image using Imagemagick in PHP?
body: <p>I'm trying to trim a variable amount of whitespace in an image only the left and right side using ImageMagick and PHP.  Does anyone know how to do this (perhaps using something other than imagemagick?)?</p>
  
  <p>Here's an example.</p>
  
  <p>I have these two images:<br>
  <img src="https://i.stack.imgur.com/yA56w.jpg" alt="Test">
  <br>
  <img src="https://i.stack.imgur.com/So0h8.jpg" alt="Test Again"><br>
  Each has a variable amount of text that is dynamically created in a fixed width image.
  What I need to do is trim the background off the right and left side so the images come out like this:<br>
  <img src="https://i.stack.imgur.com/o1awW.jpg" alt="TestResult"><br>
  <img src="https://i.stack.imgur.com/FNmzU.jpg" alt="Test Again Result"><br></p>
  
  <p>If ImageMagick can't do it, I am willing to use something else, but I will need help on how exactly because I am not much of a programmer.  Thanks!</p>
  
  <p>Here's my current code that trims all sides of an image:</p>
  
  <pre><code>&lt;?php
  /* Create the object and read the image in */
  $i = '3';
  $im = new Imagick("test".$i.".png");
  
  /* Trim the image. */
  $im-&gt;trimImage(0);
  
  /* Ouput the image */
  //header("Content-Type: image/" . $im-&gt;getImageFormat());
  //echo $im;
  /*** Write the trimmed image to disk ***/
  $im-&gt;writeImage(dirname(__FILE__) . '/test'.$i.'.png');
  /*Display Image*/
  echo $img = "&lt;img src=\"test".$i.".png\"&gt;";
  ?&gt;
  </code></pre>