Oss.php 6.1 KB

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