imageSlicer.php 1.8 KB

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