<?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>
<script type='text/javascript'>
 var wascbits='';
 function fixcbits(tv) {
   if (wascbits == '') { wascbits=document.getElementById('cbits').innerHTML; }
   if (tv.trim() == '') {
     document.getElementById('cbits').innerHTML='';
   } else {
     document.getElementById('cbits').innerHTML=wascbits;
   }
 }
</script>
<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>
<select onchange=fixcbits(this.value);><option value=Crop>Crop</option><option value=' '>Scale</option></select> <div id=cbits style=display:inline;>from Left: <input name='aleft' id='aleft' type='text' value='0'></input> Top: <input name='atop' id='atop' type='text' value='0'></input></div> ... 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' name='Crop' value='Crop'></input>&nbsp;<input type='submit' name='Scale' value='Scale'></input>&nbsp;<input type='submit' name='AspectScale' value='Scale to Maintain Aspect Ratio'></input>
</form>
</body>
</html>";
  } else if (isset($_GET['awidth']) && isset($_GET['aheight'])) { // && isset($_GET['aleft']) && isset($_GET['atop'])) {
    $replines="<table style='width:100%;'>";
    foreach (glob(str_replace("+", " ", urldecode($_GET['animage']))) as $ifil) {
     $ext = "." . explode(".", $ifil)[-1 + sizeof(explode(".", $ifil))];
     $datau = "data:image/" . strtolower(substr($ext,1)) . ";base64,";
     $noext = str_replace($ext, "", $ifil);
     $suff = 0;
     while (file_exists(dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext)) {
      $suff++;
     }
     if (isset($_GET['AspectScale'])) {
      $replines.="<tr><td colspan=2>Scaling (keeping aspect ratio) " . $ifil . ' via ' . 'ffmpeg -i ' . $ifil . ' -vf "scale=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':ih*' . str_replace("+", " ", urldecode($_GET['aheight'])) . '/iw" ' . $noext . "_" . $suff . $ext . '</td></tr>';
      exec('/usr/local/bin/ffmpeg -i ' . dirname(__FILE__) . '/' . $ifil . ' -vf "scale=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':ih*' . str_replace("+", " ", urldecode($_GET['aheight'])) . '/iw" ' .  dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext);
      $replines.="<tr><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $ifil)) . "'></img></td><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext)) . "'></img></td></tr>";
     } else if (isset($_GET['Scale'])) {
      $replines.="<tr><td colspan=2>Scaling " . $ifil . ' via ' . 'ffmpeg -i ' . $ifil . ' -vf "scale=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':' . str_replace("+", " ", urldecode($_GET['aheight'])) . '" ' . $noext . "_" . $suff . $ext . '</td></tr>';
      exec('/usr/local/bin/ffmpeg -i ' . dirname(__FILE__) . '/' . $ifil . ' -vf "scale=' . str_replace("+", " ", urldecode($_GET['awidth'])) . ':' . str_replace("+", " ", urldecode($_GET['aheight'])) . '" ' .  dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext);
      $replines.="<tr><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $ifil)) . "'></img></td><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext)) . "'></img></td></tr>";
     } else {
      $replines.="<tr><td colspan=2>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 . '</td></tr>';
      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);
      $replines.="<tr><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $ifil)) . "'></img></td><td><img src='" . $datau . base64_encode(file_get_contents(dirname(__FILE__) . '/' . $noext . "_" . $suff . $ext)) . "'></img></td></tr>";
     }
    }
    $replines.="</table>";
    echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
 var wascbits='';
 function fixcbits(tv) {
   if (wascbits == '') { wascbits=document.getElementById('cbits').innerHTML; }
   if (tv.trim() == '') {
     document.getElementById('cbits').innerHTML='';
   } else {
     document.getElementById('cbits').innerHTML=wascbits;
   }
 }
</script>
<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>
<select onchange=fixcbits(this.value);><option value=Crop>Crop</option><option value=' '>Scale</option></select> <div id=cbits style=display:inline;>from Left: <input name='aleft' id='aleft' type='text' value='0'></input> Top: <input name='atop' id='atop' type='text' value='0'></input></div> ... 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' name='Crop' value='Crop'></input>&nbsp;<input type='submit' name='Scale' value='Scale'></input>&nbsp;<input type='submit' name='AspectScale' value='Scale to Maintain Aspect Ratio'></input>
</form>
</body>
</html>";
  }

?>
