PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/crop-avatar/crop-avatar.php

https://gitlab.com/oytunistrator/cropper
PHP | 197 lines | 158 code | 39 blank | 0 comment | 29 complexity | b9f4e75b3ab8a63bc046aa17dfbe09b1 MD5 | raw file
  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 $srcDir = 'img/upload';
  10. private $dstDir = 'img/avatar';
  11. private $msg;
  12. function __construct($src, $data, $file) {
  13. $this -> setSrc($src);
  14. $this -> setData($data);
  15. $this -> setFile($file);
  16. $this -> crop($this -> src, $this -> dst, $this -> data);
  17. }
  18. private function setSrc($src) {
  19. if (!empty($src)) {
  20. $type = exif_imagetype($src);
  21. if ($type) {
  22. $this -> src = $src;
  23. $this -> type = $type;
  24. $this -> extension = image_type_to_extension($type);
  25. $this -> setDst();
  26. }
  27. }
  28. }
  29. private function setData($data) {
  30. if (!empty($data)) {
  31. $this -> data = json_decode(stripslashes($data));
  32. }
  33. }
  34. private function setFile($file) {
  35. $errorCode = $file['error'];
  36. if ($errorCode === UPLOAD_ERR_OK) {
  37. $type = exif_imagetype($file['tmp_name']);
  38. if ($type) {
  39. $dir = $this -> srcDir;
  40. if (!file_exists($dir)) {
  41. mkdir($dir, 0777);
  42. }
  43. $extension = image_type_to_extension($type);
  44. $src = $dir . '/' . date('YmdHis') . $extension;
  45. if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {
  46. if (file_exists($src)) {
  47. unlink($src);
  48. }
  49. $result = move_uploaded_file($file['tmp_name'], $src);
  50. if ($result) {
  51. $this -> src = $src;
  52. $this -> type = $type;
  53. $this -> extension = $extension;
  54. $this -> setDst();
  55. } else {
  56. $this -> msg = 'Failed to save file';
  57. }
  58. } else {
  59. $this -> msg = 'Please upload image with the following types: JPG, PNG, GIF';
  60. }
  61. } else {
  62. $this -> msg = 'Please upload image file';
  63. }
  64. } else {
  65. $this -> msg = $this -> codeToMessage($errorCode);
  66. }
  67. }
  68. private function setDst() {
  69. $dir = $this -> dstDir;
  70. if (!file_exists($dir)) {
  71. mkdir($dir, 0777);
  72. }
  73. $this -> dst = $dir . '/' . date('YmdHis') . $this -> extension;
  74. }
  75. private function crop($src, $dst, $data) {
  76. if (!empty($src) && !empty($dst) && !empty($data)) {
  77. switch ($this -> type) {
  78. case IMAGETYPE_GIF:
  79. $src_img = imagecreatefromgif($src);
  80. break;
  81. case IMAGETYPE_JPEG:
  82. $src_img = imagecreatefromjpeg($src);
  83. break;
  84. case IMAGETYPE_PNG:
  85. $src_img = imagecreatefrompng($src);
  86. break;
  87. }
  88. if (!$src_img) {
  89. $this -> msg = "Failed to read the image file";
  90. return;
  91. }
  92. $dst_img = imagecreatetruecolor(220, 220);
  93. $result = imagecopyresampled($dst_img, $src_img, 0, 0, $data -> x, $data -> y, 220, 220, $data -> width, $data -> height);
  94. if ($result) {
  95. switch ($this -> type) {
  96. case IMAGETYPE_GIF:
  97. $result = imagegif($dst_img, $dst);
  98. break;
  99. case IMAGETYPE_JPEG:
  100. $result = imagejpeg($dst_img, $dst);
  101. break;
  102. case IMAGETYPE_PNG:
  103. $result = imagepng($dst_img, $dst);
  104. break;
  105. }
  106. if (!$result) {
  107. $this -> msg = "Failed to save the cropped image file";
  108. }
  109. } else {
  110. $this -> msg = "Failed to crop the image file";
  111. }
  112. imagedestroy($src_img);
  113. imagedestroy($dst_img);
  114. }
  115. }
  116. private function codeToMessage($code) {
  117. switch ($code) {
  118. case UPLOAD_ERR_INI_SIZE:
  119. $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  120. break;
  121. case UPLOAD_ERR_FORM_SIZE:
  122. $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  123. break;
  124. case UPLOAD_ERR_PARTIAL:
  125. $message = 'The uploaded file was only partially uploaded';
  126. break;
  127. case UPLOAD_ERR_NO_FILE:
  128. $message = 'No file was uploaded';
  129. break;
  130. case UPLOAD_ERR_NO_TMP_DIR:
  131. $message = 'Missing a temporary folder';
  132. break;
  133. case UPLOAD_ERR_CANT_WRITE:
  134. $message = 'Failed to write file to disk';
  135. break;
  136. case UPLOAD_ERR_EXTENSION:
  137. $message = 'File upload stopped by extension';
  138. break;
  139. default:
  140. $message = 'Unknown upload error';
  141. }
  142. return $message;
  143. }
  144. public function getResult() {
  145. return !empty($this -> data) ? $this -> dst : $this -> src;
  146. }
  147. public function getMsg() {
  148. return $this -> msg;
  149. }
  150. }
  151. $crop = new CropAvatar($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['avatar_file']);
  152. $response = array(
  153. 'state' => 200,
  154. 'message' => $crop -> getMsg(),
  155. 'result' => $crop -> getResult()
  156. );
  157. echo json_encode($response);
  158. ?>