BaseImg.Class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Mall\Framework\Core;
  3. class BaseImg
  4. {
  5. // 上传文件名
  6. protected $saveName;
  7. // 上传Base64文件
  8. protected $file;
  9. // 上传的文件正则匹配后的信息
  10. protected $info;
  11. // 错误信息
  12. private $error = '';
  13. protected static $_instance;
  14. private function __construct(){}
  15. public static function getInstance()
  16. {
  17. $key = md5('baseImg64');
  18. if (!isset(self::$_instance[$key])) {
  19. self::$_instance[$key] = new self();
  20. }
  21. return self::$_instance[$key];
  22. }
  23. /**
  24. * @param string $file base64的图片
  25. * @param bool $replace 同名文件是否覆盖
  26. * @param string $dirName 制定目录名称
  27. *
  28. * @return bool|File
  29. */
  30. public function move($file, $replace = true, $dirName = '')
  31. {
  32. $this->file = $file;
  33. $path = UPLOAD_FILE_PATH;
  34. // 检测合法性
  35. if (!$this->baseIsValid()) {
  36. $this->error = '非法上传文件';
  37. return false;
  38. }
  39. // 验证上传
  40. if (!$this->baseCheckImg($this->info[2])) {
  41. $this->error = '非法图像文件';
  42. return false;
  43. }
  44. $path = rtrim($path, DS) . DS;
  45. // 文件保存命名规则
  46. $saveName = $this->baseBuildSaveName($dirName);
  47. $filename = $path . $saveName;
  48. // 检测目录
  49. if (false === $this->checkPath(dirname($filename))) {
  50. return false;
  51. }
  52. /* 不覆盖同名文件 */
  53. if (!$replace && is_file($filename)) {
  54. $this->error = '存在同名文件' . $filename;
  55. return false;
  56. }
  57. if(!file_put_contents($filename, base64_decode(str_replace($this->info[1], '', $file)))){
  58. $this->error = '图像上传失败';
  59. return false;
  60. }
  61. // 返回 File对象实例
  62. $this->setSaveName($saveName);
  63. return $this;
  64. }
  65. protected function baseIsValid()
  66. {
  67. preg_match('/^(data:\s*image\/(\w+);base64,)/', $this->file, $result);
  68. $this->info = $result;
  69. return $result ? true : false;
  70. }
  71. protected function baseCheckImg($extension)
  72. {
  73. if (!in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'], true)) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. protected function baseBuildSaveName($dirName = '')
  79. {
  80. if($dirName){
  81. return $dirName .DS. date('Ymd') . DS . md5(microtime(true)) . '.' . $this->info[2];
  82. }
  83. return date('Ymd') . DS . md5(microtime(true)) . '.' . $this->info[2];
  84. }
  85. /**
  86. * 检查目录是否可写
  87. * @param string $path 目录
  88. * @return boolean
  89. */
  90. protected function checkPath($path)
  91. {
  92. if (is_dir($path)) {
  93. return true;
  94. }
  95. if (mkdir($path, 0755, true)) {
  96. return true;
  97. } else {
  98. $this->error = "目录 {$path} 创建失败!";
  99. return false;
  100. }
  101. }
  102. /**
  103. * 获取上传文件的文件名
  104. * @return string
  105. */
  106. public function getSaveName()
  107. {
  108. return $this->saveName;
  109. }
  110. /**
  111. * 设置上传文件的保存文件名
  112. * @param string $saveName
  113. * @return $this
  114. */
  115. public function setSaveName($saveName)
  116. {
  117. $this->saveName = $saveName;
  118. return $this;
  119. }
  120. /**
  121. * 获取错误信息
  122. * @return mixed
  123. */
  124. public function getError()
  125. {
  126. return $this->error;
  127. }
  128. public function baseImgMove($thumbs, $selfUploadUrl = '')
  129. {
  130. $actionImg = [];
  131. if (!empty($thumbs)) {
  132. if (is_array($thumbs) && !empty($thumbs)) {
  133. foreach ($thumbs as $thumb) {
  134. if ($thumb) {
  135. if ($path = self::_baseImgValidate($thumb, $selfUploadUrl)) {
  136. $actionImg[] = $path;
  137. }
  138. }
  139. }
  140. } else {
  141. if (is_string($thumbs)) {
  142. if ($path = self::_baseImgValidate($thumbs, $selfUploadUrl)) {
  143. $actionImg[] = $path;
  144. }
  145. }
  146. }
  147. }
  148. return json_encode($actionImg);
  149. }
  150. private function _baseImgValidate($thumb, $selfUploadUrl)
  151. {
  152. if(filter_var($thumb, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
  153. preg_match('#^'.preg_quote($selfUploadUrl).'#', $thumb, $myThumb);
  154. if ($myThumb) {
  155. $actionImg = str_replace($selfUploadUrl, '', $thumb);
  156. } else {
  157. $actionImg = $thumb;
  158. }
  159. } else {
  160. if ($file = self::move($thumb)) {
  161. $actionImg = $file->getSaveName();
  162. } else {
  163. $actionImg = $thumb;
  164. }
  165. }
  166. return $actionImg ? $actionImg : false;
  167. }
  168. }