<?php
/*
Thanks to https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg

For reducing or increasing size of an image we can use ffmpeg  by this command
 
1.ffmpeg - i input.png -vf scale=w:h output.png  
2.//where  -i is input parameter,  w is width of image in pixels ,  h  is height of image in pixels and output.png output file name
3.eg .ffmpeg - i input.png -vf scale=310:240 output.png
 
The above command simply scale image to the given  size width 400 px and height 400 px but the images aspect ratio may be destroy .
 
If you want to maintain the aspect ration of original image  you can use this command
1.ffmpeg - i input.png -vf scale=310:ih*240/iw output.png  
 
This command will maintain the original aspect ratio of image eg. if image has aspect ratio 2:3 then the image conversion will be de into 2:3 aspect ratio 
we can also convert image into own aspect ratio e.g. if an image has 2:3 but we want 1:1 
this can be done by this command
 
1.ffmpeg -i input.jpg -vf scale="'if(gt(a,1/1),320,-1)':'if(gt(a,1/1),-1,240)'" output.png


reference https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg
 
 
2) cropping
 
 
To get the a part  of an image ffmpeg can be use

1.ffmpeg -i input.png -vf  "crop=w:h:x:y" input_crop.png
2. 
3.where -vf  video filter
4.w : width , h height , x and y are the left top coordinates of image
 
 
*/ 

  if (!isset($_GET['animage'])) {
    echo "<!doctype html>
<html>
<head>
<title>Help out ffmpeg with Cropping an Image File(spec) - RJM Programming - April, 2019 ... thanks to https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg</title>
</head>
<body>
<h1>Help out ffmpeg with Cropping an Image File(spec)</h1>
<h3>RJM Programming - April, 2019</h3>
<h4>Thanks to <a target=_blank title='https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg' href='https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg'>https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg</a></h4>
<form action='./help_ffmpeg.php' method='GET'>
Input Image File(spec): <input name='animage' id='animage' type='text' value=''></input></br>
Crop from Left: <input name='aleft' id='aleft' type='text' value='0'></input> Top: <input name='atop' id='atop' type='text' value='0'></input> ... by ...   Width: <input name='awidth' id='awidth' type='text' value='0'></input> and Height: <input name='aheight' id='aheight' type='text' value='0'></input><br>
<input type='submit' value='Crop'></input>
</form>
</body>
</html>";
  } else if (isset($_GET['awidth']) && isset($_GET['aheight']) && isset($_GET['aleft']) && isset($_GET['atop'])) {
    $replines="";
    foreach (glob(str_replace("+", " ", urldecode($_GET['animage']))) as $ifil) {
     $ext = "." . explode(".", $ifil)[-1 + sizeof(explode(".", $ifil))];
     $noext = str_replace($ext, "", $ifil);
     $suff = 0;
     while (file_exists(dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext)) {
      $suff++;
     }
     $replines.="<br>Cropping " . $ifil . ' via ' . 'ffmpeg -i ' . $ifil . ' -vf "crop=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':' . str_replace("+", " ", urldecode($_GET['aheight'])) . ':' . str_replace("+", " ", urldecode($_GET['aleft'])) . ':' . str_replace("+", " ", urldecode($_GET['atop'])) . '" ' . $noext . "_" . $suff . $ext;
     exec('/usr/local/bin/ffmpeg -i ' . dirname(__FILE__) . '/' . $ifil . ' -vf "crop=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':' . str_replace("+", " ", urldecode($_GET['aheight'])) . ':' . str_replace("+", " ", urldecode($_GET['aleft'])) . ':' . str_replace("+", " ", urldecode($_GET['atop'])) . '" ' .  dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext);
    }
    echo "<!doctype html>
<html>
<head>
<title>Help out ffmpeg with Cropping an Image File(spec) - RJM Programming - April, 2019 ... thanks to https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg</title>
</head>
<body>
<h1>Help out ffmpeg with Cropping an Image File(spec)</h1>
<h3>RJM Programming - April, 2019</h3>
<h4>Thanks to <a target=_blank title='https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg' href='https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg'>https://www.oodlestechnologies.com/blogs/Crop-and-scale-image-using-ffmpeg</a></h4>
" . $replines . "
<br><br><form action='./help_ffmpeg.php' method='GET'>
Input Image File(spec): <input name='animage' id='animage' type='text' value=''></input></br>
Crop from Left: <input name='aleft' id='aleft' type='text' value='0'></input> Top: <input name='atop' id='atop' type='text' value='0'></input> ... by ...   Width: <input name='awidth' id='awidth' type='text' value='0'></input> and Height: <input name='aheight' id='aheight' type='text' value='0'></input><br>
<input type='submit' value='Crop'></input>
</form>
</body>
</html>";
  }

?>
