Oss.php 6.0 KB

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