Oss.php 5.8 KB

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