imageSlicer.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. $asocarray["url"][$counter] = $filename;
  35. //check if file exists if so, create file with other name and return as json
  36. if(file_exists($filename)) {
  37. $asocarray["override"][$counter]["old"] = $filename;
  38. $filename = $savelocation . "zwischen" . $counter . ".jpg";
  39. $asocarray["override"][$counter]["new"] = $filename;
  40. }
  41. //echo $filename;
  42. $im2 = imagecrop($im, ['x' => $rect->X, 'y' => $rect->Y, 'width' => $rect->W, 'height' => $rect->H]);
  43. imagejpeg($im2, $filename);
  44. imagedestroy($im2);
  45. }
  46. ++$counter;
  47. }
  48. }
  49. echo json_encode($asocarray);
  50. ?>