Local.php 8.8 KB

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