Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <?php
  2. /*
  3. * !!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE ImageMagick or some other quality image processing libraries
  4. */
  5. $imgUrl = $_POST['imgUrl'];
  6. // original sizes
  7. $imgInitW = $_POST['imgInitW'];
  8. $imgInitH = $_POST['imgInitH'];
  9. // resized sizes
  10. $imgW = $_POST['imgW'];
  11. $imgH = $_POST['imgH'];
  12. // offsets
  13. $imgY1 = $_POST['imgY1'];
  14. $imgX1 = $_POST['imgX1'];
  15. // crop box
  16. $cropW = $_POST['cropW'];
  17. $cropH = $_POST['cropH'];
  18. // rotation angle
  19. $angle = $_POST['rotation'];
  20.  
  21. $jpeg_quality = 100;
  22.  
  23. $output_filename = "temp/croppedImg_".rand();
  24.  
  25. // uncomment line below to save the cropped image in the same location as the original image.
  26. //$output_filename = dirname($imgUrl). "/croppedImg_".rand();
  27.  
  28. $what = getimagesize($imgUrl);
  29.  
  30. switch(strtolower($what['mime']))
  31. {
  32. case 'image/png':
  33. $img_r = imagecreatefrompng($imgUrl);
  34. $source_image = imagecreatefrompng($imgUrl);
  35. $type = '.png';
  36. break;
  37. case 'image/jpeg':
  38. $img_r = imagecreatefromjpeg($imgUrl);
  39. $source_image = imagecreatefromjpeg($imgUrl);
  40. error_log("jpg");
  41. $type = '.jpeg';
  42. break;
  43. case 'image/gif':
  44. $img_r = imagecreatefromgif($imgUrl);
  45. $source_image = imagecreatefromgif($imgUrl);
  46. $type = '.gif';
  47. break;
  48. default: die('image type not supported');
  49. }
  50.  
  51.  
  52. //Check write Access to Directory
  53.  
  54. if(!is_writable(dirname($output_filename))){
  55. $response = Array(
  56. "status" => 'error',
  57. "message" => 'Can`t write cropped File'
  58. );
  59. }else{
  60.  
  61. // resize the original image to size of editor
  62. $resizedImage = imagecreatetruecolor($imgW, $imgH);
  63. imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
  64. // rotate the rezized image
  65. $rotated_image = imagerotate($resizedImage, -$angle, 0);
  66. // find new width & height of rotated image
  67. $rotated_width = imagesx($rotated_image);
  68. $rotated_height = imagesy($rotated_image);
  69. // diff between rotated & original sizes
  70. $dx = $rotated_width - $imgW;
  71. $dy = $rotated_height - $imgH;
  72. // crop rotated image to fit into original rezized rectangle
  73. $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
  74. imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
  75. imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
  76. // crop image into selected area
  77. $final_image = imagecreatetruecolor($cropW, $cropH);
  78. imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
  79. imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
  80. // finally output png image
  81. //imagepng($final_image, $output_filename.$type, $png_quality);
  82. imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
  83. $response = Array(
  84. "status" => 'success',
  85. "url" => $output_filename.$type
  86. );
  87. }
  88. print json_encode($response);
  89.  
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement