Storage.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\weixin\model;
  3. use app\weixin\service\PRedis;
  4. use cmf\lib\Upload;
  5. use think\Model;
  6. use think\Request;
  7. class Storage extends Model
  8. {
  9. /**
  10. * 上传图片
  11. * @param $formId 图片表单名称
  12. * @param $scene 应用场景
  13. * @return array|string
  14. */
  15. public static function uploadImg($file, $scene = 'default', $water = false, $thumb = true)
  16. {
  17. try {
  18. $data = false;
  19. $path = "upload/{$scene}";
  20. if (!is_dir($path)) {
  21. mkdir($path, 0755, true);
  22. }
  23. if(empty($file)){
  24. return false;
  25. }
  26. $info = $file? $file->getInfo() : [];
  27. $tempName = isset($info['tmp_name'])? $info['tmp_name'] : '';
  28. PRedis::set('uploads:file:'.$scene.'_'.date('YmdHis'), ['file'=> $file, 'info'=> $info], 3600);
  29. if(empty($tempName)){
  30. return 3001;
  31. }
  32. $fileSize = config('files.imageSize');
  33. $fileSize = $fileSize? $fileSize * 1024 * 1024 : 50 * 1024 * 1024;
  34. $size = isset($info['size'])? intval($info['size']) : 0;
  35. if($size>$fileSize){
  36. return lang('file_size', ['size'=> intval($fileSize/1024/1024)]);
  37. }
  38. $info = $file->validate(['size' => $fileSize, 'ext' => 'jpg,jpeg,png,gif'])
  39. ->move('./' . $path);
  40. PRedis::set('uploads:info:'.$scene.'_'.date('YmdHis'), ['info'=> $info], 3600);
  41. if ($info) {
  42. $filename = str_replace('\\', '/', $info->getSaveName());
  43. $fileInfo = $info->getInfo();
  44. $name = isset($fileInfo['name']) ? $fileInfo['name'] : '';
  45. $file = "{$scene}/" . $filename;
  46. if($thumb){
  47. $file = Storage::imageThumb($file);
  48. }
  49. if ($water) {
  50. $siteInfo = cmf_get_site_info();
  51. $waterTxt = isset($siteInfo['site_name']) ? trim($siteInfo['site_name']) : '拾光单身';
  52. if ($waterTxt && $water) {
  53. $params = ['num' => 1, 'angle' => 0, 'locate' => 9, 'offset' => -10, 'fontSize' => 14];
  54. $file = Storage::imageWater($file, $waterTxt, $params);
  55. }
  56. }
  57. $data = [
  58. 'name' => $name,
  59. 'file' => $file,
  60. 'preview' => cmf_get_image_preview_url($file),
  61. ];
  62. } else {
  63. return false;
  64. }
  65. return $data;
  66. } catch (\Exception $exception) {
  67. PRedis::set('uploads:error:'.$scene.'_'.date('YmdHis'), $exception, 3600);
  68. return false;
  69. }
  70. }
  71. /**
  72. * 上传图片
  73. * @param $formId 图片表单名称
  74. * @param $scene 应用场景
  75. * @return array|string
  76. */
  77. public static function uploadFile($file, $scene = 'default')
  78. {
  79. $data = [];
  80. $path = "upload/files/{$scene}";
  81. if (!is_dir($path)) {
  82. mkdir($path, 0755, true);
  83. }
  84. $fileTypes = config('files.fileTypes');
  85. $fileSize = config('files.fileSize');
  86. $fileSize = $fileSize ? $fileSize * 1024 * 1024 : 30 * 1024 * 1024;
  87. $fileTypes = $fileTypes ? $fileTypes : ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'];
  88. $info = $file->validate(['size' => $fileSize, 'ext' => implode(',', $fileTypes)])->move('./' . $path);
  89. if ($info) {
  90. $fileInfo = $info->getInfo();
  91. $name = isset($fileInfo['name']) ? $fileInfo['name'] : '';
  92. $data = [
  93. 'name' => $name,
  94. 'file' => $file,
  95. 'preview' => cmf_get_image_preview_url($file),
  96. ];
  97. }
  98. return $data;
  99. }
  100. /**
  101. * 缩略图
  102. * @param $file
  103. * @param int $type
  104. * @return $this
  105. */
  106. public static function imageThumb($file, $type=1, $params=[]){
  107. $siteInfo = cmf_get_site_info();
  108. $thumbWidth = isset($siteInfo['thumb_width'])? intval($siteInfo['thumb_width']) : 0;
  109. $thumbHeight = isset($siteInfo['thumb_height'])? intval($siteInfo['thumb_height']) : 0;
  110. $width = isset($params['width'])? $params['width'] : 0;
  111. $height = isset($params['height'])? $params['height'] : 0;
  112. $thumbWidth = $thumbWidth>600? $thumbWidth : 600;
  113. $thumbHeight = $thumbHeight>600? $thumbHeight : 600;
  114. $thumbWidth = $width? $width : $thumbWidth;
  115. $thumbHeight = $height? $height : $thumbHeight;
  116. // 要加水印的图片
  117. $realFilename = 'upload/' . $file;
  118. if (!file_exists($realFilename)) {
  119. return false;
  120. }
  121. $image = \think\Image::open($realFilename);
  122. $filepath = dirname($realFilename);
  123. $paths = explode('_',basename($realFilename));
  124. $filename = end($paths);
  125. $thumbfile = $filepath . '/thumb_' . $thumbWidth . '_' . $thumbHeight . '_' . $type . '_' . $filename;
  126. $image->thumb($thumbWidth, $thumbHeight, $type)->save($thumbfile);
  127. return preg_replace("/^upload\//", '', $thumbfile);
  128. }
  129. /**
  130. * 文件添加水印
  131. * @param $filename
  132. * @param string $text
  133. * @param array $params
  134. * @return string
  135. */
  136. public static function imageWater($filename, $text = '', $params = [], $deleteOld = true)
  137. {
  138. if (empty($filename)) {
  139. return '';
  140. }
  141. // 字体
  142. $font = isset($params['font']) ? $params['font'] : '';
  143. $font = $font ? $font : 'fonts/msyh.ttc';
  144. // 字体颜色
  145. $color = isset($params['color']) ? $params['color'] : '';
  146. $color = $color ? $color : '#D92E2E';
  147. // 字体大小
  148. $fontSize = isset($params['fontSize']) ? intval($params['fontSize']) : 0;
  149. $fontSize = $fontSize ? $fontSize : 20;
  150. // 文字旋转角度
  151. $angle = isset($params['angle']) ? intval($params['angle']) : -1;
  152. $angle = $angle >= 0 ? $angle : 30;
  153. // 文字偏移
  154. $offset = isset($params['offset']) ? intval($params['offset']) : 0;
  155. $offset = $offset ? $offset : 0;
  156. // 水印文字密集数量
  157. $num = isset($params['num']) ? intval($params['num']) : 0;
  158. $num = $num ? $num : 3;
  159. // 文字位置
  160. $locate = isset($params['locate']) ? intval($params['locate']) : 0;
  161. $locate = $locate ? $locate : 5;
  162. // 要加水印的图片
  163. $realFilename = 'upload/' . $filename;
  164. if (!file_exists($realFilename)) {
  165. return $filename;
  166. }
  167. chmod($realFilename, 0755);
  168. $filepath = dirname($realFilename);
  169. $waterfile = $filepath . '/water_' . basename($realFilename);
  170. @fopen($waterfile, 'w+');
  171. chmod($waterfile, 0755);
  172. $image = \think\Image::open($realFilename);
  173. if ($num > 1) {
  174. $locate = 3;
  175. $tempText = str_repeat($text . ' ', 15);
  176. for ($i = 0; $i < $num * 10; $i++) {
  177. $offset = ($i - 5) * 80;
  178. $image->text($tempText, $font, $fontSize, $color, $locate, $offset, $angle)->save($waterfile);
  179. }
  180. } else {
  181. $image->text($text, $font, $fontSize, $color, $locate, $offset, $angle)->save($waterfile);
  182. }
  183. if ($deleteOld) {
  184. @unlink($realFilename);
  185. }
  186. return preg_replace("/^upload\//", '', $waterfile);
  187. }
  188. }