Oss.php 5.1 KB

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