title: How to set aspect ratio in below code using PHP
body: <p>I tried 2 implementations, shown below. Both are working well but when I tried to mix them it is not working anymore.</p>
  <pre><code>$output['status']=FALSE;
  set_time_limit(0);
  $allowedImageType = array(&quot;image/gif&quot;,   &quot;image/jpeg&quot;,   &quot;image/pjpeg&quot;,   &quot;image/png&quot;,   &quot;image/x-png&quot;  );
  
  if ($_FILES['image_file_input'][&quot;error&quot;] &gt; 0) {
      $output['error']= &quot;File Error&quot;;
  }
  elseif (!in_array($_FILES['image_file_input'][&quot;type&quot;], $allowedImageType)) {
      $output['error']= &quot;Invalid image format&quot;;
  }
  elseif (round($_FILES['image_file_input'][&quot;size&quot;] / 1024) &gt; 4096) {
      $output['error']= &quot;Maximum file upload size is exceeded&quot;;
  } else {
      $temp_path = $_FILES['image_file_input']['tmp_name'];
      $file = pathinfo($_FILES['image_file_input']['name']);
      $fileType = $file[&quot;extension&quot;];
      $photo_name = $productname.'-'.$member_id.&quot;_&quot;.time();
      $fileName1 = $photo_name . '-125x125' . &quot;.jpg&quot;;
      $fileName2 = $photo_name . '-250x250' . &quot;.jpg&quot;;
      $fileName3 = $photo_name . '-500x500' . &quot;.jpg&quot;;
      
      $small_thumbnail_path = &quot;uploads/large/&quot;;
      createFolder($small_thumbnail_path);
      $small_thumbnail = $small_thumbnail_path . $fileName1;
      
      $medium_thumbnail_path = &quot;uploads/large/&quot;;
      createFolder($medium_thumbnail_path);
      $medium_thumbnail = $medium_thumbnail_path . $fileName2;
      
      $large_thumbnail_path = &quot;uploads/large/&quot;;
      createFolder($large_thumbnail_path);
      $large_thumbnail = $large_thumbnail_path . $fileName3;
      
      $thumb1 = createThumbnail($temp_path, $small_thumbnail,$fileType, 125, 125  );
      $thumb2 = createThumbnail($temp_path, $medium_thumbnail, $fileType, 250, 250);
      $thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 500, 500);
              
      if($thumb1 &amp;&amp; $thumb2 &amp;&amp; $thumb3) {
          $output['status']=TRUE;
          $output['small']= $small_thumbnail;
          $output['medium']= $medium_thumbnail;
              $output['large']= $large_thumbnail;
      }
  }
  echo json_encode($output);
  </code></pre>
  <p>Function File</p>
  <pre><code>function createFolder($path)
  {
      if (!file_exists($path)) {
          mkdir($path, 0755, TRUE);
      }
  }
  
  function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
      
      $source = imagecreatefromjpeg($sourcePath);
      
      $width = imagesx($source);
      $height = imagesy($source);
      
      $tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
      
      imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
      
      if (imagejpeg($tnumbImage, $targetPath, 90)) {
          imagedestroy($tnumbImage);
          imagedestroy($source);
          return TRUE;
      } else {
          return FALSE;
      }
  }
  </code></pre>
  <p>Aspect ratio code, which is another one I tried to mix this code. But I was unsuccessful</p>
  <pre><code>$fn = $_FILES['image']['tmp_name'];
  $size = getimagesize($fn);
  $ratio = $size[0]/$size[1]; // width/height
  $photo_name = $productname.'-'.$member_id.&quot;_&quot;.time();
  {
      if( $ratio &gt; 1) {
      $width1 = 500;
      $height1 = 500/$ratio;
  }
  else {
      $width1 = 500*$ratio;
      $height1 = 500;
  }
  $src = imagecreatefromstring(file_get_contents($fn));
  $dst = imagecreatetruecolor($width1,$height1);
  $fileName3 = $photo_name . '-500x500' . &quot;.jpg&quot;;
  imagecopyresampled($dst,$src,0,0,0,0,$width1,$height1,$size[0],$size[1]);
  imagedestroy($src);
  imagepng($dst,$fileName3); // adjust format as needed
  imagedestroy($dst);
      if( $ratio &gt; 1) {
      $width2 = 250;
      $height2 = 250/$ratio;
  }
  else {
      $width2 = 250*$ratio;
      $height2 = 250;
  }
  $src = imagecreatefromstring(file_get_contents($fn));
  $dst = imagecreatetruecolor($width2,$height2);
  $fileName2 = $photo_name . '-250x250' . &quot;.jpg&quot;;
  imagecopyresampled($dst,$src,0,0,0,0,$width2,$height2,$size[0],$size[1]);
  imagedestroy($src);
  imagepng($dst,$fileName2); // adjust format as needed
  imagedestroy($dst);
  }
  </code></pre>
  <p>What I need is to save my image after resizing but in second code there is no condition check, and I can't get image upload folder path. That's why I need to merge these 2 codes.</p>
  <p>Basically I need need to save my image in 3 size formats: 500x500,250x250 and 125x125. Width is fixed, but height is set as per aspect ratio and set upload folder and condition in second code block.</p>