ImageUtil.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/13/013
  6. * Time: 16:14
  7. */
  8. namespace common\helpers;
  9. use Yii;
  10. class ImageUtil
  11. {
  12. /**
  13. * 文件保存根路径
  14. * @var string
  15. */
  16. public $basePath = '@webroot';
  17. /**
  18. * 文件保存根url
  19. * @var string
  20. */
  21. public $baseUrl = '@web';
  22. /**
  23. * 文件保存相对路径
  24. * @var string
  25. */
  26. public $savePath = 'upload';
  27. /**
  28. * a list of file name extensions that are allowed to be uploaded.
  29. * 定义允许上传的文件扩展名
  30. * @var array
  31. */
  32. public $extensions = [
  33. 'image' => ['gif', 'jpg', 'jpeg', 'png', 'bmp'],
  34. 'flash' => ['swf', 'flv'],
  35. 'media' => ['swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'],
  36. 'file' => ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'txt', 'zip', 'rar', 'gz', 'bz2', 'pdf'],
  37. ];
  38. /**
  39. * the maximum number of bytes required for the uploaded file.
  40. * 最大文件大小
  41. * @var int
  42. */
  43. protected $_maxSize = 1000000;
  44. public function upload($image)
  45. {
  46. $base_path = rtrim(Yii::getAlias($this->basePath), '\\/') . '/';
  47. //文件保存目录路径
  48. $save_path = $base_path . rtrim($this->savePath, '\\/') . '/';
  49. //定义允许上传的文件扩展名
  50. $ext_arr = $this->extensions;
  51. //最大文件大小
  52. $max_size = $this->getMaxSize();
  53. // 新建文件夹
  54. $add_time = date("Ym", time());
  55. //$save_path = realpath($save_path) . '/';
  56. //PHP上传失败
  57. if (!empty($image['error'])) {
  58. switch ($image['error']) {
  59. case '1':
  60. $error = '超过php.ini允许的大小。';
  61. break;
  62. case '2':
  63. $error = '超过表单允许的大小。';
  64. break;
  65. case '3':
  66. $error = '图片只有部分被上传。';
  67. break;
  68. case '4':
  69. $error = '请选择图片。';
  70. break;
  71. case '6':
  72. $error = '找不到临时目录。';
  73. break;
  74. case '7':
  75. $error = '写文件到硬盘出错。';
  76. break;
  77. case '8':
  78. $error = 'File upload stopped by extension。';
  79. break;
  80. case '999':
  81. default:
  82. $error = '未知错误。';
  83. }
  84. $this->alert($error);
  85. }
  86. //有上传文件时
  87. if (empty($image) === false) {
  88. //原文件名
  89. $file_name = $image['name'];
  90. //服务器上临时文件名
  91. $tmp_name = $image['tmp_name'];
  92. //文件大小
  93. $file_size = $image['size'];
  94. //检查文件名
  95. if (!$file_name) {
  96. $this->alert("请选择文件。");
  97. }
  98. //检查目录
  99. if (@is_dir($save_path) === false) {
  100. $this->alert("上传目录不存在。");
  101. }
  102. //检查目录写权限
  103. if (@is_writable($save_path) === false) {
  104. $this->alert("上传目录没有写权限。");
  105. }
  106. //检查是否已上传
  107. if (@is_uploaded_file($tmp_name) === false) {
  108. $this->alert("上传失败。");
  109. }
  110. //检查文件大小
  111. if ($file_size > $max_size) {
  112. $this->alert("上传文件大小超过限制。");
  113. }
  114. //获得文件扩展名
  115. $temp_arr = explode(".", $file_name);
  116. $file_ext = array_pop($temp_arr);
  117. $file_ext = trim($file_ext);
  118. $file_ext = strtolower($file_ext);
  119. //检查扩展名
  120. if (in_array($file_ext, $ext_arr['image']) === false) {
  121. $this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr['image']) . "格式。");
  122. }
  123. //新文件名
  124. $new_file_name = $this->generateUuid() . '.' . $file_ext;
  125. if (!file_exists($save_path.$add_time)) {
  126. mkdir($save_path.$add_time, 0777);
  127. }
  128. //移动文件
  129. $file_path = $save_path .$add_time. '/' . $new_file_name;
  130. if (move_uploaded_file($tmp_name, $file_path) === false) {
  131. $this->alert("上传文件失败。");
  132. }
  133. $file_url = '/'.$this->savePath. '/' .$add_time. '/' . $new_file_name;
  134. return $file_url;
  135. }
  136. }
  137. /**
  138. * 生成唯一UUID
  139. */
  140. protected function generateUuid()
  141. {
  142. $str = md5(uniqid(mt_rand(), true));
  143. $uuid = substr($str,0,8);
  144. $uuid .= substr($str,8,4);
  145. $uuid .= substr($str,12,4);
  146. $uuid .= substr($str,16,4);
  147. $uuid .= substr($str,20,12);
  148. return $uuid;
  149. }
  150. /**
  151. * @param string $msg
  152. */
  153. protected function alert($msg)
  154. {
  155. header('Content-type: text/html; charset=UTF-8');
  156. echo json_encode(['error' => 1, 'message' => $msg]);
  157. exit;
  158. }
  159. /**
  160. * @return int
  161. */
  162. public function getMaxSize()
  163. {
  164. return $this->_maxSize;
  165. }
  166. /**
  167. * @param int $maxSize
  168. */
  169. public function setMaxSize($maxSize)
  170. {
  171. $this->_maxSize = intval($maxSize);
  172. }
  173. }