Oss.php 5.9 KB

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