Ueditor.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\sys\controller;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. class Ueditor
  6. {
  7. public function index($action)
  8. {
  9. if (method_exists($this, $action)) {
  10. return $this->$action();
  11. } else {
  12. return;
  13. }
  14. }
  15. /**
  16. * 文件上传配置
  17. */
  18. public function config()
  19. {
  20. return json(config('ueditor'));
  21. }
  22. /**
  23. * 图片抓取
  24. */
  25. public function catchimage()
  26. {
  27. $list = array();
  28. $request = request();
  29. $source = $request->param('source');
  30. $accessKeyId = config('app.ali_oss_access_key_id');
  31. $accessKeySecret = config('app.ali_oss_access_key_secret');
  32. $endpoint = config('app.ali_oss_end_point');
  33. $bucket = config('app.ali_oss_bucket');
  34. $bucketUrl = config('app.ali_oss_bindurl');
  35. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  36. $list = [];
  37. try {
  38. foreach ($source as $imgUrl) {
  39. $ext = pathinfo($imgUrl, PATHINFO_EXTENSION);
  40. if (empty($ext)) {
  41. $query = parse_url($imgUrl, PHP_URL_QUERY);
  42. parse_str($query, $urlParam);
  43. if (isset($urlParam['wx_fmt'])) {
  44. $ext = $urlParam['wx_fmt'];
  45. } else {
  46. array_push($list, [
  47. "state" => '文件类型获取失败',
  48. "url" => '',
  49. "size" => '',
  50. "title" => '',
  51. "original" => '',
  52. "source" => $imgUrl
  53. ]);
  54. continue;
  55. }
  56. }
  57. // 判断文件格式
  58. if(!in_array($ext, ['png','PNG','jpg','JPG', 'jpeg','JPEG', 'bmp','BMP', 'gif',"GIF","WEBP", 'webp', 'psd',"PSD", 'svg', 'tiff'])){
  59. array_push($list, [
  60. "state" => '文件类型不支持',
  61. "url" => '',
  62. "size" => '',
  63. "title" => '',
  64. "original" => '',
  65. "source" => $imgUrl
  66. ]);
  67. continue;
  68. }
  69. $content = file_get_contents($imgUrl);
  70. $size = strlen($content);
  71. $path = 'wxcontent' . DIRECTORY_SEPARATOR . date('Ymd') . DIRECTORY_SEPARATOR . time() . rand(1000, 9999) . '.' . $ext;
  72. $oss->putObject($bucket, $path, $content);
  73. array_push($list, array(
  74. "state" => 'SUCCESS',
  75. "url" => 'https://' . $bucketUrl . '/' . $path,
  76. "size" => $size,
  77. "title" => '',
  78. "original" => '',
  79. "source" => $imgUrl
  80. ));
  81. }
  82. } catch (OssException $e) {
  83. trace($e, 'error');
  84. }
  85. return json(['state' => 'SUCCESS', 'list' => $list]);
  86. }
  87. /**
  88. * 文件上传
  89. */
  90. public function uploadimage()
  91. {
  92. $url = request()->param('upfile');
  93. $bucketUrl = config('app.ali_oss_bindurl');
  94. $data = [
  95. 'original' => "",
  96. 'size' => '',
  97. 'state' => "SUCCESS",
  98. 'title' => "",
  99. 'type' => "",
  100. 'url' => 'https://' . $bucketUrl . '/' . $url,
  101. ];
  102. return json($data);
  103. }
  104. /**
  105. * 涂鸦图片上传
  106. */
  107. public function uploadscrawl()
  108. {
  109. $url = request()->param('upfile');
  110. $bucketUrl = config('app.ali_oss_bindurl');
  111. $data = [
  112. 'original' => "",
  113. 'size' => '',
  114. 'state' => "SUCCESS",
  115. 'title' => "",
  116. 'type' => "",
  117. 'url' => 'https://' . $bucketUrl . '/' . $url,
  118. ];
  119. return json($data);
  120. }
  121. public function uploadvideo()
  122. {
  123. $url = request()->param('upfile');
  124. $bucketUrl = config('app.ali_oss_bindurl');
  125. $data = [
  126. 'original' => "",
  127. 'size' => '',
  128. 'state' => "SUCCESS",
  129. 'title' => "",
  130. 'type' => "",
  131. 'url' => 'https://' . $bucketUrl . '/' . $url,
  132. ];
  133. return json($data);
  134. }
  135. }