imageSlicer.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. header('Content-Type: application/json');
  6. $imgurl = htmlspecialchars($_POST["url"]);
  7. $alldata = json_decode($_POST["alldata"]);
  8. //var_dump( $alldata->rects );
  9. $savelocation = $_POST["savelocation"];
  10. //echo $savelocation;
  11. $asocarray = array();
  12. if ( ! is_writable($savelocation)) {
  13. $asocarray["error"] = $savelocation . ' must be writable!!!';
  14. echo json_encode($asocarray);
  15. exit();
  16. }
  17. else{
  18. $im = imagecreatefromjpeg($imgurl);
  19. $counter = 0;
  20. foreach($alldata->rects as $rect){
  21. $filename = $savelocation;
  22. $filename = $filename . $alldata->name . "_";
  23. $filenamecpy = $filename;
  24. foreach($rect->pattern as $key => $val) {
  25. $filename = $filename . $key . ";" . $val . "_";
  26. }
  27. if($rect->idtext != ""){
  28. $filename = $filename . "ID" . ";" . $rect->idtext . "_";
  29. }
  30. //only save image if at least one checkbox is active or a text is set.
  31. if($filenamecpy != $filename){
  32. $filename = substr($filename, 0, -1);
  33. $filename = $filename . ".jpg";
  34. //check if file exists if so, create file with other name and return as json
  35. if(file_exists($filename)) {
  36. $asocarray[$counter]["old"] = $filename;
  37. $filename = $savelocation . "zwischen" . $counter . ".jpg";
  38. $asocarray[$counter]["new"] = $filename;
  39. }
  40. //echo $filename;
  41. $im2 = imagecrop($im, ['x' => $rect->X, 'y' => $rect->Y, 'width' => $rect->W, 'height' => $rect->H]);
  42. imagejpeg($im2, $filename);
  43. imagedestroy($im2);
  44. }
  45. ++$counter;
  46. }
  47. }
  48. echo json_encode($asocarray);
  49. ?>