title: Cropping an image from the center into the largest square you can make in Yii2 Imagine?
body: <p>I'm using the Yii2 extension <a href="http://www.yiiframework.com/doc-2.0/yii-imagine-image.html" rel="nofollow noreferrer">Imagine</a> and I need to make <code>150x150</code> images from user uploads.</p>
  
  <p>Currently I am just doing something like this:</p>
  
  <pre><code>use yii\imagine\Image;
  
  ....
  
  Image::thumbnail($save_path, $img_size, $img_size)-&gt;save($save_path);
  </code></pre>
  
  <p>Obviously this can cause issues if one of the dimensions is &lt; <code>150px</code> once resized.</p>
  
  <p>So what I'm trying to primarily figure out is how to crop the image into a <strong>square</strong> <em>before</em> it is resized, so that when I resize it there won't be any <em>aspect ratio</em> issues.</p>
  
  <p>Now, I know you can crop the image with something like:</p>
  
  <pre><code>Image::crop($save_path, $img_size, $img_size, [5, 5]);
  </code></pre>
  
  <p>But problem is doing this <em>before</em> resizing the image will likely not give you what you want since the image may be so large and cropping it <em>after</em> resizing won't work either as one dimension may already have been reduced to &lt; <code>150px</code>.</p>
  
  <p>So what I'm trying to work out is how can I crop the image <em>before</em> resizing to the <strong>maximum</strong> size square possible <em>and</em> from the center out?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok, I have worked out a way to handle this, but was wondering if there was anyway to accomplish the below easily or will I need to code it myself?</p>
  
  <ul>
  <li>Work out the smallest dimension (width or height)</li>
  <li>Then take that dimension and that will be the largest square you can have</li>
  <li>Work out how to position that in the center for the crop</li>
  <li>Now you can do the resize</li>
  <li>If after the resize either side is smaller than 150, create new white background image and then center the new image on that</li>
  <li>Save image</li>
  <li>Done!</li>
  </ul>