CropImage.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. class CropImage {
  3. private $src;
  4. private $data;
  5. private $file;
  6. private $dst;
  7. private $type;
  8. private $extension;
  9. private $msg;
  10. function __construct($src, $data, $file) {
  11. $this -> setSrc($src);
  12. $this -> setData($data);
  13. $this -> setFile($file);
  14. $this -> crop($this -> src, $this -> dst, $this -> data);
  15. }
  16. private function setSrc($src) {
  17. if (!empty($src)) {
  18. $type = exif_imagetype($src);
  19. if ($type) {
  20. $this -> src = $src;
  21. $this -> type = $type;
  22. $this -> extension = image_type_to_extension($type);
  23. $this -> setDst();
  24. }
  25. }
  26. }
  27. private function setData($data) {
  28. if (!empty($data)) {
  29. $this -> data = json_decode(stripslashes($data));
  30. }
  31. }
  32. private function setFile($file) {
  33. $errorCode = $file['error'];
  34. if ($errorCode === UPLOAD_ERR_OK) {
  35. $type = exif_imagetype($file['tmp_name']);
  36. if ($type) {
  37. $extension = image_type_to_extension($type);
  38. $src = sys_get_temp_dir() . date('YmdHis') . '.original' . $extension;
  39. if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {
  40. if (file_exists($src)) {
  41. unlink($src);
  42. }
  43. $result = move_uploaded_file($file['tmp_name'], $src);
  44. if ($result) {
  45. $this -> src = $src;
  46. $this -> type = $type;
  47. $this -> extension = $extension;
  48. $this -> setDst();
  49. } else {
  50. $this -> msg = 'Failed to save file';
  51. }
  52. } else {
  53. $this -> msg = 'Please upload image with the following types: JPG, PNG, GIF';
  54. }
  55. } else {
  56. $this -> msg = 'Please upload image file';
  57. }
  58. } else {
  59. $this -> msg = $this -> codeToMessage($errorCode);
  60. }
  61. }
  62. private function setDst() {
  63. $this -> dst = sys_get_temp_dir() . date('YmdHis') . '.png';
  64. }
  65. private function crop($src, $dst, $data) {
  66. if (!empty($src) && !empty($dst) && !empty($data)) {
  67. switch ($this -> type) {
  68. case IMAGETYPE_GIF:
  69. $src_img = imagecreatefromgif($src);
  70. break;
  71. case IMAGETYPE_JPEG:
  72. $src_img = imagecreatefromjpeg($src);
  73. break;
  74. case IMAGETYPE_PNG:
  75. $src_img = imagecreatefrompng($src);
  76. break;
  77. }
  78. if (!$src_img) {
  79. $this -> msg = "Failed to read the image file";
  80. return;
  81. }
  82. $size = getimagesize($src);
  83. $size_w = $size[0]; // natural width
  84. $size_h = $size[1]; // natural height
  85. $src_img_w = $size_w;
  86. $src_img_h = $size_h;
  87. $degrees = $data -> rotate;
  88. // Rotate the source image
  89. if (is_numeric($degrees) && $degrees != 0) {
  90. // PHP's degrees is opposite to CSS's degrees
  91. $new_img = imagerotate( $src_img, -$degrees, imagecolorallocatealpha($src_img, 0, 0, 0, 127) );
  92. imagedestroy($src_img);
  93. $src_img = $new_img;
  94. $deg = abs($degrees) % 180;
  95. $arc = ($deg > 90 ? (180 - $deg) : $deg) * M_PI / 180;
  96. $src_img_w = $size_w * cos($arc) + $size_h * sin($arc);
  97. $src_img_h = $size_w * sin($arc) + $size_h * cos($arc);
  98. // Fix rotated image miss 1px issue when degrees < 0
  99. $src_img_w -= 1;
  100. $src_img_h -= 1;
  101. }
  102. $tmp_img_w = $data -> width;
  103. $tmp_img_h = $data -> height;
  104. $dst_img_w = 220;
  105. $dst_img_h = 220;
  106. $src_x = $data -> x;
  107. $src_y = $data -> y;
  108. if ($src_x <= -$tmp_img_w || $src_x > $src_img_w) {
  109. $src_x = $src_w = $dst_x = $dst_w = 0;
  110. } else if ($src_x <= 0) {
  111. $dst_x = -$src_x;
  112. $src_x = 0;
  113. $src_w = $dst_w = min($src_img_w, $tmp_img_w + $src_x);
  114. } else if ($src_x <= $src_img_w) {
  115. $dst_x = 0;
  116. $src_w = $dst_w = min($tmp_img_w, $src_img_w - $src_x);
  117. }
  118. if ($src_w <= 0 || $src_y <= -$tmp_img_h || $src_y > $src_img_h) {
  119. $src_y = $src_h = $dst_y = $dst_h = 0;
  120. } else if ($src_y <= 0) {
  121. $dst_y = -$src_y;
  122. $src_y = 0;
  123. $src_h = $dst_h = min($src_img_h, $tmp_img_h + $src_y);
  124. } else if ($src_y <= $src_img_h) {
  125. $dst_y = 0;
  126. $src_h = $dst_h = min($tmp_img_h, $src_img_h - $src_y);
  127. }
  128. // Scale to destination position and size
  129. $ratio = $tmp_img_w / $dst_img_w;
  130. $dst_x /= $ratio;
  131. $dst_y /= $ratio;
  132. $dst_w /= $ratio;
  133. $dst_h /= $ratio;
  134. $dst_img = imagecreatetruecolor($dst_img_w, $dst_img_h);
  135. // Add transparent background to destination image
  136. imagefill($dst_img, 0, 0, imagecolorallocatealpha($dst_img, 0, 0, 0, 127));
  137. imagesavealpha($dst_img, true);
  138. $result = imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  139. if ($result) {
  140. if (!imagepng($dst_img, $dst)) {
  141. $this -> msg = "Failed to save the cropped image file";
  142. }
  143. } else {
  144. $this -> msg = "Failed to crop the image file";
  145. }
  146. imagedestroy($src_img);
  147. imagedestroy($dst_img);
  148. }
  149. }
  150. private function codeToMessage($code) {
  151. switch ($code) {
  152. case UPLOAD_ERR_INI_SIZE:
  153. $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  154. break;
  155. case UPLOAD_ERR_FORM_SIZE:
  156. $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  157. break;
  158. case UPLOAD_ERR_PARTIAL:
  159. $message = 'The uploaded file was only partially uploaded';
  160. break;
  161. case UPLOAD_ERR_NO_FILE:
  162. $message = 'No file was uploaded';
  163. break;
  164. case UPLOAD_ERR_NO_TMP_DIR:
  165. $message = 'Missing a temporary folder';
  166. break;
  167. case UPLOAD_ERR_CANT_WRITE:
  168. $message = 'Failed to write file to disk';
  169. break;
  170. case UPLOAD_ERR_EXTENSION:
  171. $message = 'File upload stopped by extension';
  172. break;
  173. default:
  174. $message = 'Unknown upload error';
  175. }
  176. return $message;
  177. }
  178. public function getResult() {
  179. return !empty($this -> data) ? $this -> dst : $this -> src;
  180. }
  181. public function getMsg() {
  182. return $this -> msg;
  183. }
  184. }
  185. //
  186. //$crop = new CropAvatar($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['avatar_file']);
  187. //
  188. //$response = array(
  189. // 'state' => 200,
  190. // 'message' => $crop -> getMsg(),
  191. // 'result' => $crop -> getResult()
  192. //);
  193. //
  194. //echo json_encode($response);