crop.php 8.1 KB

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