Oss.php 5.4 KB

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