Oss.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $this->output['url'] = '{uploadYun}_'.$this->config['yun'].'_' . $this->file;
  79. return $this->output['file'];
  80. }
  81. /**
  82. * water
  83. *
  84. * @return mixed
  85. */
  86. public function handle_w($id)
  87. {
  88. $config = Dever::load('upload/pic_water-one', $id);
  89. if ($config) {
  90. $this->name .= '_w' . $id;
  91. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  92. $this->img()->mark($this->output['file'], $water, true, $this->file());
  93. }
  94. }
  95. /**
  96. * compress
  97. *
  98. * @return mixed
  99. */
  100. public function handle_p($num, $source = false, $dest = false)
  101. {
  102. if (!$source) {
  103. $source = $this->output['file'];
  104. }
  105. if (!$dest) {
  106. if (strpos($this->name, '_p') !== false) {
  107. $temp = explode('_p', $this->name);
  108. $this->name = $temp[0];
  109. }
  110. $file = $this->output['file'];
  111. $url = $this->output['url'];
  112. $this->name .= '_p' . $num;
  113. $dest = $this->file();
  114. $this->output['file'] = $file;
  115. $this->output['url'] = $url;
  116. }
  117. $this->img()->setType('im');
  118. $this->img()->compress($source, $num, $dest);
  119. }
  120. /**
  121. * webp
  122. *
  123. * @return mixed
  124. */
  125. public function handle_wp($num, $source = false, $dest = false)
  126. {
  127. if (!$source) {
  128. $source = $this->output['file'];
  129. }
  130. if (!$dest) {
  131. if (strpos($this->name, '_wp') !== false) {
  132. $temp = explode('_wp', $this->name);
  133. $this->name = $temp[0];
  134. }
  135. $file = $this->output['file'];
  136. $url = $this->output['url'];
  137. $this->ext = '.webp';
  138. $this->name .= '_wp' . $num;
  139. $dest = $this->file();
  140. $this->output['file'] = $file;
  141. $this->output['url'] = $url;
  142. }
  143. $this->img()->setType('im');
  144. $this->img()->webp($source, $num, $dest);
  145. }
  146. /**
  147. * thumb
  148. *
  149. * @return mixed
  150. */
  151. public function handle_t($id, $source = false, $dest = false, $path = '')
  152. {
  153. $config = Dever::load('upload/pic_thumb-one', $id);
  154. if ($config) {
  155. if (!$source) {
  156. $source = $this->output['file'];
  157. }
  158. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  159. //?x-oss-process=image/resize,q_50
  160. // $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  161. $dest = $source . '?x-oss-process=image/resize,m_lfit,w_'.$config['width'].',h_' . $config['height'];
  162. if (isset($config['compress']) && $config['compress'] > 0) {
  163. $dest .= ',q_' . $config['compress'];
  164. }
  165. }
  166. return $dest;
  167. }
  168. /**
  169. * crop
  170. *
  171. * @return mixed
  172. */
  173. public function handle_c($id, $source = false, $dest = false)
  174. {
  175. $config = Dever::load('upload/pic_crop-one', $id);
  176. if ($config) {
  177. if (!$source) {
  178. $source = $this->output['file'];
  179. }
  180. $dest = $source . '?x-oss-process=image/resize,fill,w_'.$config['width'].',h_' . $config['height'];
  181. }
  182. return $dest;
  183. }
  184. }