title: PHP - How do I convert a rectangle image to a square image?
body: <p>How do I change a rectangular image to a square-shaped avatar in php, such that no matter what is the resolution of the uploaded image, it is able to resize to a centralized 42 x 42 pixel avatar. This is the php code I am using. Anyone can advise.</p>
  
  <pre><code>&lt;?php 
  //Name you want to save your file as
  $save = 'myfile1.jpg';
  
  $file = 'original1.jpg'; 
  echo "Creating file: $save"; 
  $size = 0.45; 
  header('Content-type: image/jpeg') ; 
  list($width, $height) = getimagesize($file) ; 
  $modwidth = $width * $size; 
  $modheight = $height * $size; 
  $tn = imagecreatetruecolor($modwidth, $modheight) ; 
  $image = imagecreatefromjpeg($file) ; 
  imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
  
  // Here we are saving the .jpg, you can make this gif or png if you want
  //the file name is set above, and the quality is set to 100%
  imagejpeg($tn, $save, 100) ; 
  ?&gt; 
  </code></pre>