Oss.php 6.5 KB

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