title: Image Cropping using CodeIgniter and Cropper
body: <p>I'm using Cropper as a GUI to crop an image.
  Cropper gives me the X and Y position from top and left and the resulting width and height of the image. I'm passing this numbers to hidden input fields.
  In CodeIgniter I'm using imagemagick to crop the image with this code:</p>
  
  <pre><code>$this-&gt;load-&gt;library('image_lib');
  $config['image_library'] = 'imagemagick';
  $config['library_path'] = '/Applications/MAMP/Library/bin/';
  $config['source_image'] = "upload/".$data['image']['file_name'];
  $config['x_axis'] = $post['dataX'];
  $config['y_axis'] = $post['dataY'];
  $config['width'] = $post['dataWidth'];
  $config['height'] = $post['dataHeight'];
  
  $this-&gt;image_lib-&gt;initialize($config);
  
  if ( ! $this-&gt;image_lib-&gt;crop()) {
    echo $this-&gt;image_lib-&gt;display_errors();
  }
  </code></pre>
  
  <p>ImageMagick uses a code like this</p>
  
  <pre><code>$cmd = $this-&gt;library_path.' -quality '.$this-&gt;quality;
  /* ... */
  if ($action === 'crop')
  {
      $cmd .= ' -crop '.$this-&gt;width.'x'.$this-&gt;height.'+'.$this-&gt;x_axis.'+'.$this-&gt;y_axis;
  }
  /* ... */
  $cmd .= ' '.escapeshellarg($this-&gt;full_src_path).' '.escapeshellarg($this-&gt;full_dst_path).' 2&gt;&amp;1';
  /* ... */
  @exec($cmd, $output, $retval);
  </code></pre>
  
  <p>Basically there is a line: -crop 100x500+10+10. 
  This line crops the image at 4 axis:</p>
  
  <ol>
  <li>crop: X-Axis at 10px from the left</li>
  <li>crop: Y-Axis at 10px from the top</li>
  <li>crop: X-Axis at $width-100 from the right</li>
  <li>crop: Y-Axis at $height-500 from the bottom</li>
  </ol>
  
  <p>Additionally I store the new dimensions in my database:</p>
  
  <pre><code>$this-&gt;db-&gt;where('id', $id);
  $this-&gt;db-&gt;update("images", array(
    'width' =&gt; $post['dataWidth'],
    'height' =&gt; $post['dataHeight']
  ));
  </code></pre>
  
  <p>After a crop I can compare the dimensions of the file and the values in the database. Unless I don't change the aspect ratio of my crop the values are the same.
  When I change the aspect ratio, the Image crops in the old aspect ratio.
  I'm not able to figure out why.</p>
  
  <p>If you need more code, please tell me.</p>