Oss.php 5.9 KB

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