crop.php 7.9 KB

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