title: How do I crop the selected part of an image with a preset size (800*400px)?
body: <p>I am trying to create a form for adding a post with the specified image size of 800*400px.</p>
  <p>800px constant width, 400px maximum height of the final image, i.e. less than 400 is allowed.</p>
  <p>The minimum allowed size of a photo uploaded to crop is 800x400</p>
  <p><a href="https://bemall.org/news/add/" rel="nofollow noreferrer">Demonstration</a> of what is available in the &quot;Select image&quot; field.</p>
  <p>Now you can upload an image, select a place for a crop and perform a crop, but the result will be sad, because the proportions of the image are not saved.</p>
  <p>For example, the original image may be 1920*1152px</p>
  <p><a href="https://i.stack.imgur.com/FA6td.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FA6td.png" alt="enter image description here" /></a></p>
  <p>After crop the selected part, the result is as follows</p>
  <p><a href="https://i.stack.imgur.com/CuRqA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CuRqA.png" alt="enter image description here" /></a></p>
  <p>I am trying to create a form for adding a post with the specified image size of 800*400px.</p>
  <p>800px constant width, 400px maximum height of the final image, i.e. less than 400 is allowed.</p>
  <p>The minimum allowed size of a photo uploaded to crop is 800x400</p>
  <p>Demonstration of what is available in the &quot;Select image&quot; field.</p>
  <p>Now you can upload an image, select a place for a crop and perform a crop, but the result will be sad, because the proportions of the image are not saved.</p>
  <p>For example, the original image may be 1920*1152px enter an image description here</p>
  <p>After crop the selected part, the result is as follows enter an image description here</p>
  <p>I need the result that is highlighted in the crop editor with a maximum size of 800*400 or 800 or less than 400, depending on the source image, so that it is saved proportionally.</p>
  <p>This editor I spied on one of the sites, where only 2 parameters are sent to the server at the time of crop, the <strong>url</strong> of the image preloaded on the server and the parameter <strong>y</strong>-apparently the starting point from the upper-left corner. I repeated the same thing in <a href="https://bemall.org/news/add/" rel="nofollow noreferrer">my example</a>.</p>
  <p><strong>Example</strong>: <code>{&quot;url&quot;:&quot;https://site.ru/news/upload_tmp/files/file.jpg&quot;,&quot;y&quot;:&quot;81&quot;}</code></p>
  <p>I found a similar script, tried a lot of different settings, but couldn't get the result.</p>
  <p>Here is the code I'm currently using:</p>
  <pre><code>if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
  
  $postData = file_get_contents('php://input');
  $data = json_decode($postData, true);
  
  $url = $data[&quot;url&quot;];
  $y = $data[&quot;y&quot;];
  
  $_POST['x1'] = '0';
  $_POST['x2'] = '800';
  $_POST['y'] = $y;
  $_POST['y2'] = '400';
  $_POST['img'] = $url;
  $_POST['crop'] = 'files/';
  
  
  
  /**
  * Zoom image
  *
  * Функция работает с PNG, GIF и JPEG изображениями.
  * Масштабирование возможно как с указаниями одной стороны, так и двух, в процентах или пикселях.
  *
  * @param string Расположение исходного файла
  * @param string Расположение конечного файла
  * @param integer Ширина конечного файла
  * @param integer Высота конечного файла
  * @param bool Размеры даны в пискелях или в процентах
  * @return bool
  */
  
  function resize($file_input, $file_output, $w_o, $h_o, $percent = false) {
  list($w_i, $h_i, $type) = getimagesize($file_input);
  if (!$w_i || !$h_i) {
  echo 'It is not possible to get the length and width of the image';
  return;
  }
  $types = array('','gif','jpeg','png');
  $ext = $types[$type];
  if ($ext) {
  $func = 'imagecreatefrom'.$ext;
  $img = $func($file_input);
  } else {
  echo 'Invalid file format';
  return;
  }
  if ($percent) {
  $w_o *= $w_i / 100;
  $h_o *= $h_i / 100;
  }
  if (!$h_o) $h_o = $w_o/($w_i/$h_i);
  if (!$w_o) $w_o = $h_o/($h_i/$w_i);
  $img_o = imagecreatetruecolor($w_o, $h_o);
  imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i);
  if ($type == 2) {
  imagejpeg($img_o,$file_output,100);
  } else {
  $func = 'image'.$ext;
  $func($img_o,$file_output);
  }
  imagedestroy($img_o);
  }
  
  /**
  * Cropping an image
  *
  * Функция работает с PNG, GIF и JPEG изображениями.
  * Обрезка идёт как с указанием абсолютной длины, так и относительной (отрицательной).
  *
  * @param string Расположение исходного файла
  * @param string Расположение конечного файла
  * @param array Координаты обрезки
  * @param bool Размеры даны в пискелях или в процентах
  * @return bool
  */
  function crop($file_input, $file_output, $crop = 'square',$percent = false) {
  list($w_i, $h_i, $type) = getimagesize($file_input);
  if (!$w_i || !$h_i) {
  echo 'It is not possible to get the length and width of the image';
  return;
  }
  $types = array('','gif','jpeg','png');
  $ext = $types[$type];
  if ($ext) {
  $func = 'imagecreatefrom'.$ext;
  $img = $func($file_input);
  } else {
  echo 'Invalid file format';
  return;
  }
  if ($crop == 'square') {
  $min = $w_i;
  if ($w_i &gt; $h_i) $min = $h_i;
  $w_o = $h_o = $min;
  } else {
  list($x_o, $y_o, $w_o, $h_o) = $crop;
  if ($percent) {
      $w_o *= $w_i / 100;
      $h_o *= $h_i / 100;
      $x_o *= $w_i / 100;
      $y_o *= $h_i / 100;
  }
  if ($w_o &lt; 0) $w_o += $w_i;
  $w_o -= $x_o;
  if ($h_o &lt; 0) $h_o += $h_i;
  $h_o -= $y_o;
  }
  $img_o = imagecreatetruecolor($w_o, $h_o);
  imagecopy($img_o, $img, 0, 0, $x_o, $y_o, $w_o, $h_o);
  if ($type == 2) {
  imagejpeg($img_o,$file_output,100);
  
  } else {
  $func = 'image'.$ext;
  $func($img_o,$file_output);
  }
  imagedestroy($img_o);
  }
  
  
  
  function prov($per){
  if (isset($per)) {
  $per = stripslashes($per);
  $per = htmlspecialchars($per);
  $per = addslashes($per);         
  }
  return $per;
  }
  
  if(isset($_POST)){
  $filenew = 'main.png'; // rewritable main.png
  
  $x1 = prov($_POST['x1']);
  $x2 = prov($_POST['x2']);
  $y = prov($_POST['y']);
  $y2 = prov($_POST['y2']);
  $img = prov($_POST['img']);
  $crop = prov($_POST['crop']);
  
  crop($img, $crop.$filenew, array($x1, $y, $x2, $y2));   
  
  //echo $filenew;
  echo '{&quot;result&quot;:&quot;ok&quot;,&quot;data&quot;:&quot;https:\/\/site.ru\/news\/cropImage\/files\/' . $filenew . '&quot;}';
  
  }
  
  }else{
  print '{&quot;result&quot;:&quot;error&quot;,&quot;message&quot;:&quot;\u041f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f&quot;}';
  }
  </code></pre>
  <p>I set up the default parameters differently (all except $url and $y), found more or less decent options, but all the wrong ones :(</p>
  <p>Perhaps you have encountered a similar task and can help me understand this issue?</p>
  <p>Thanks!</p>