Local.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace core\upload;
  3. use core\exception\UploadFileException;
  4. use Exception;
  5. use Grafika\Color;
  6. use Grafika\Grafika;
  7. use Grafika\Position;
  8. /**
  9. * 文件管理驱动类
  10. */
  11. class Local extends BaseUpload
  12. {
  13. //位置
  14. private $position = array(
  15. 'top-left' => 'top-left',
  16. 'top-center' => 'top-center',
  17. 'top-right' => 'top-right',
  18. 'center-left' => 'center-left',
  19. 'center' => 'center',
  20. 'center-right' => 'center-right',
  21. 'bottom-left' => 'bottom-left',
  22. 'bottom-center' => 'bottom-center',
  23. 'bottom-right' => 'bottom-right',
  24. );
  25. protected function initialize(array $config = [])
  26. {
  27. parent::initialize($config);
  28. }
  29. public function upload(string $dir)
  30. {
  31. $this->validate();
  32. mkdirs_or_notexist($dir, 0777);
  33. $this->file->move($dir, $this->file_name);
  34. //错误一般是已经被抛出了
  35. return true;
  36. }
  37. /**
  38. * 远程获取图片
  39. * @param string $url
  40. * @param string|null $key
  41. * @return true
  42. */
  43. public function fetch(string $url, ?string $key)
  44. {
  45. try {
  46. mkdirs_or_notexist(dirname($key), 0777);
  47. $content = @file_get_contents($url);
  48. if (!empty($content)) {
  49. file_put_contents($key, $content);
  50. $image_info = getimagesize($key);
  51. $image_type = $image_info[2];
  52. // if($image_type == IMAGETYPE_JPEG){
  53. // // 保存图片为PNG格式
  54. // $image = imagecreatefromjpeg($key);
  55. // }else if($image_type == IMAGETYPE_PNG){
  56. // // 保存图片为PNG格式
  57. // $image = imagecreatefrompng($key);
  58. // }
  59. if($image_type == IMAGETYPE_WEBP){
  60. // 保存图片为PNG格式
  61. $image = imagecreatefromwebp($key);
  62. }
  63. // 创建图片资源
  64. // 检查图片是否创建成功
  65. if (!empty($image)){
  66. // 图片类型常量定义:IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF
  67. if ($image_type == IMAGETYPE_WEBP) {
  68. $temp_arr = explode('.', $key);
  69. $ext = end($temp_arr);
  70. if($ext == 'jpg' || $ext == 'jpeg'){
  71. // 保存图片为PNG格式
  72. imagejpeg($image, $key);
  73. }else if($ext = 'png'){
  74. // 保存图片为PNG格式
  75. imagepng($image, $key);
  76. }
  77. }
  78. // 释放内存
  79. imagedestroy($image);
  80. }
  81. } else {
  82. throw new UploadFileException(203006);
  83. }
  84. return true;
  85. } catch ( Exception $e ) {
  86. throw new UploadFileException($e->getMessage());
  87. }
  88. }
  89. /**
  90. * base64转图片
  91. * @param string $content
  92. * @param string|null $key
  93. * @return true
  94. */
  95. public function base64(string $content, ?string $key = null)
  96. {
  97. mkdirs_or_notexist(dirname($key));
  98. file_put_contents(url_to_path($key), base64_decode($content));
  99. return true;
  100. }
  101. /**
  102. * 删除本地附件
  103. * @param string $file_name
  104. * @return bool
  105. */
  106. public function delete(string $file_name)
  107. {
  108. $file_path = url_to_path($file_name);
  109. if (file_exists($file_path)) {
  110. $result = unlink($file_path);
  111. // throw new UploadFileException(100013);
  112. }else{
  113. $result = true;
  114. }
  115. //顺便删除相关的缩略图
  116. $dirname = dirname($file_name);
  117. $file_list = [];
  118. search_dir($dirname, $file_list);
  119. if(!empty($file_list)){
  120. $file_arr = explode('/', $file_name);
  121. $only_file_name = end($file_arr);
  122. foreach($file_list as $v){
  123. if(str_contains($v, $only_file_name) && file_exists($v)){
  124. unlink($v);
  125. }
  126. }
  127. }
  128. return $result;
  129. }
  130. /**
  131. * 缩略图
  132. * @param $file_path
  133. * @param $thumb_type
  134. * @return array
  135. * @throws Exception
  136. */
  137. public function thumb($file_path, $thumb_type)
  138. {
  139. //todo 判断缩略图是否存在
  140. $thumb_config = config('upload.thumb.thumb_type');
  141. // ……
  142. //获取文件原名 获取
  143. $file_arr = explode('/', $file_path);
  144. $file_name = end($file_arr);
  145. $thumb_list = [];
  146. //获取文件后缀
  147. foreach ($thumb_config as $k => $v) {
  148. if ($thumb_type == 'all' || $thumb_type == $k || (is_array($thumb_type) && in_array($k, $thumb_type))) {
  149. $new_width = $v['width'];
  150. $new_height = $v['height'];
  151. $new_thumb_path = str_replace($file_name, $new_width . 'x' . $new_height . '_' . $file_name, $file_path);
  152. if (!file_exists($new_thumb_path)) {
  153. $editor = Grafika::createEditor();
  154. $editor->open($image, $file_path);
  155. $editor->resizeFit($image, $new_width, $new_height);
  156. //新缩略图文件名称
  157. $editor->save($image, $new_thumb_path, null, null, false, 0777);
  158. }
  159. $thumb_list[$k] = $new_thumb_path;
  160. }
  161. }
  162. return $thumb_list;
  163. }
  164. /**
  165. * 图片水印
  166. * @param $file_path
  167. * @return mixed
  168. * @throws Exception
  169. */
  170. public function water($file_path)
  171. {
  172. $water_config = [];
  173. if (!empty($water_config)) {
  174. $status = $water_config['status'];//是否启用
  175. if ($status) {
  176. $editor = Grafika::createEditor();
  177. $editor->open($image, $file_path);
  178. if ($water_config['type'] == 'image') {
  179. $water_image = $water_config['image'];
  180. if (!empty($water_image)) {
  181. //判断水印图片是否是本地图片
  182. if (check_file_is_remote($water_image)) {
  183. $file_arr = explode('.', $water_image);
  184. $ext_name = end($file_arr);
  185. $name = $this->createFileName($water_image, $ext_name);
  186. $watermark_image = 'upload/water/' . $name;
  187. $this->fetch($water_image, $watermark_image);
  188. }
  189. if (file_exists($water_image)) {
  190. }
  191. $editor->open($image1, $water_config['image']);
  192. $editor->blend($image, $image1, 'normal', $water_config['opacity'], $this->position[$water_config['position']], $water_config['offset_x'], $water_config['offset_y']);
  193. }
  194. } else {
  195. if ($water_config['text']) {
  196. $position = $this->position[$water_config['position']];
  197. $offset_x = $water_config['offset_x'];//水平偏移值
  198. $offset_y = $water_config['offset_y'];//垂直偏移值
  199. $width = $image->getWidth();
  200. $height = $image->getHeight();
  201. //获取文字信息
  202. $info = imagettfbbox($water_config['size'], $water_config['angle'], $water_config['font'], $water_config['text']);
  203. $minx = min($info[0], $info[2], $info[4], $info[6]);
  204. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  205. $miny = min($info[1], $info[3], $info[5], $info[7]);
  206. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  207. /* 计算文字初始坐标和尺寸 */
  208. $x = $minx;
  209. $y = abs($miny);
  210. $w = $maxx - $minx;
  211. $h = $maxy - $miny;
  212. //转化坐标
  213. $position = new Position($position, $offset_x, $offset_y);
  214. // Position is for $image2. $image1 is canvas.
  215. list($offset_x, $offset_y) = $position->getXY($width, $height, $w, $h);
  216. $editor->text($image, $water_config['text'], $water_config['size'], $offset_x, $offset_y, new Color($water_config['color']), $water_config['font'], $water_config['angle']);
  217. }
  218. $editor->save($image, $file_path);
  219. }
  220. }
  221. return $file_path;
  222. }
  223. }
  224. }