Oss.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. Dever::apply('sdk/oss', 'upload');
  5. use OSS\OssClient;
  6. use OSS\Core\OssException;
  7. class Oss extends Core implements Config
  8. {
  9. public $yun = true;
  10. /**
  11. * 创建文件和目录
  12. *
  13. * @return mixed
  14. */
  15. public function save()
  16. {
  17. $config = Dever::db('upload/yun')->one($this->config['yun']);
  18. $accessKey = $config['appkey'];
  19. $secretKey = $config['appsecret'];
  20. $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
  21. $bucket = $this->config['bucket'];
  22. $this->host = $config['host'];
  23. list($type, $token, $domain, $bucket) = Dever::load('upload/lib/view/oss')->token($config, $this->config);
  24. $data = explode('||', $token);
  25. if ($data[0]) {
  26. $token = $data[0];
  27. $accessKey = $data[1];
  28. $secretKey = $data[2];
  29. } else {
  30. $token = false;
  31. }
  32. $ossClient = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
  33. $this->getName();
  34. $file = $this->file;
  35. if ($this->config['cover'] == 3) {
  36. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  37. $file = $this->file;
  38. }
  39. $options = array();
  40. if ($this->base64) {
  41. $ret = $ossClient->putObject($bucket, $file, $this->data['file']['tmp_name'], $options);
  42. } else {
  43. $ret = $ossClient->uploadFile($bucket, $file, $this->data['file']['tmp_name']);
  44. }
  45. if (isset($ret['info']['url'])) {
  46. $this->file = $this->output['url'];
  47. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
  48. if (!$this->limit) {
  49. $data = Dever::curl($this->output['url'] . '?x-oss-process=image/info');
  50. if ($data) {
  51. $data = json_decode($data, true);
  52. if (isset($data['ImageWidth']['value'])) {
  53. $this->limit = array($data['ImageWidth']['value'], $data['ImageHeight']['value']);
  54. $this->size = $data['FileSize']['value'];
  55. $this->ext = '.' . $data['Format']['value'];
  56. }
  57. }
  58. }
  59. if (isset($file) && $file) {
  60. $this->update($file['id']);
  61. } else {
  62. $this->insert();
  63. }
  64. return $this->output['url'];
  65. }
  66. }
  67. public function file()
  68. {
  69. if ($this->base64) {
  70. $data = pathinfo($this->data['pic']);
  71. $data['dirname'] = str_replace($this->host, '', $data['dirname']);
  72. $this->file = $data['dirname'] . '/' . $data['basename'] . $this->name . $this->ext;
  73. } else {
  74. $this->file = $this->path . $this->name . $this->ext;
  75. }
  76. $this->output['file'] = $this->file;
  77. $this->output['url'] = $this->host . $this->file;
  78. return $this->output['file'];
  79. }
  80. /**
  81. * water
  82. *
  83. * @return mixed
  84. */
  85. public function handle_w($id)
  86. {
  87. $config = Dever::load('upload/pic_water-one', $id);
  88. if ($config) {
  89. $this->name .= '_w' . $id;
  90. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  91. $this->img()->mark($this->output['file'], $water, true, $this->file());
  92. }
  93. }
  94. /**
  95. * compress
  96. *
  97. * @return mixed
  98. */
  99. public function handle_p($num, $source = false, $dest = false)
  100. {
  101. if (!$source) {
  102. $source = $this->output['file'];
  103. }
  104. if (!$dest) {
  105. if (strpos($this->name, '_p') !== false) {
  106. $temp = explode('_p', $this->name);
  107. $this->name = $temp[0];
  108. }
  109. $file = $this->output['file'];
  110. $url = $this->output['url'];
  111. $this->name .= '_p' . $num;
  112. $dest = $this->file();
  113. $this->output['file'] = $file;
  114. $this->output['url'] = $url;
  115. }
  116. $this->img()->setType('im');
  117. $this->img()->compress($source, $num, $dest);
  118. }
  119. /**
  120. * webp
  121. *
  122. * @return mixed
  123. */
  124. public function handle_wp($num, $source = false, $dest = false)
  125. {
  126. if (!$source) {
  127. $source = $this->output['file'];
  128. }
  129. if (!$dest) {
  130. if (strpos($this->name, '_wp') !== false) {
  131. $temp = explode('_wp', $this->name);
  132. $this->name = $temp[0];
  133. }
  134. $file = $this->output['file'];
  135. $url = $this->output['url'];
  136. $this->ext = '.webp';
  137. $this->name .= '_wp' . $num;
  138. $dest = $this->file();
  139. $this->output['file'] = $file;
  140. $this->output['url'] = $url;
  141. }
  142. $this->img()->setType('im');
  143. $this->img()->webp($source, $num, $dest);
  144. }
  145. /**
  146. * thumb
  147. *
  148. * @return mixed
  149. */
  150. public function handle_t($id, $source = false, $dest = false, $path = '')
  151. {
  152. $config = Dever::load('upload/pic_thumb-one', $id);
  153. if ($config) {
  154. if (!$source) {
  155. $source = $this->output['file'];
  156. }
  157. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  158. //?x-oss-process=image/resize,q_50
  159. // $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  160. $dest = $source . '?x-oss-process=image/resize,m_lfit,w_'.$config['width'].',h_' . $config['height'];
  161. if (isset($config['compress']) && $config['compress'] > 0) {
  162. $dest .= ',q_' . $config['compress'];
  163. }
  164. }
  165. return $dest;
  166. }
  167. /**
  168. * crop
  169. *
  170. * @return mixed
  171. */
  172. public function handle_c($id, $source = false, $dest = false)
  173. {
  174. $config = Dever::load('upload/pic_crop-one', $id);
  175. if ($config) {
  176. if (!$source) {
  177. $source = $this->output['file'];
  178. }
  179. $dest = $source . '?x-oss-process=image/resize,fill,w_'.$config['width'].',h_' . $config['height'];
  180. }
  181. return $dest;
  182. }
  183. }