Qiniu.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Upload\Lib\Store;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu extends Core implements Config
  6. {
  7. public $yun = true;
  8. /**
  9. * 上传文件
  10. *
  11. * @return mixed
  12. */
  13. public function save()
  14. {
  15. list($yun, $file) = $this->yun();
  16. $options = array();
  17. $ret = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config)->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
  18. if (isset($ret['hash']) && isset($ret['key'])) {
  19. $this->file = $ret['key'];
  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'] . '?imageInfo');
  23. if ($data) {
  24. $data = json_decode($data, true);
  25. if (isset($data['width'])) {
  26. $this->limit = array($data['width'], $data['height']);
  27. $this->size = $data['size'];
  28. $this->ext = '.' . $data['format'];
  29. }
  30. }
  31. }
  32. if (isset($file) && $file) {
  33. $this->update($file['id']);
  34. } else {
  35. $this->insert();
  36. }
  37. return $this->output['url'];
  38. }
  39. }
  40. public function file()
  41. {
  42. if ($this->base64 && isset($this->data['local'])) {
  43. $this->data['local'] = str_replace(Dever::data() . 'upload/', '', $this->data['local']);
  44. $this->file = $this->data['local'] . $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. $dest = $this->data['host'] . $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  132. if (isset($config['compress']) && $config['compress'] > 0) {
  133. $dest .= '/q/' . $config['compress'];
  134. }
  135. $dest .= '|imageslim';
  136. }
  137. return $dest;
  138. }
  139. /**
  140. * crop
  141. *
  142. * @return mixed
  143. */
  144. public function handle_c($id, $source = false, $dest = false)
  145. {
  146. $config = Dever::load('upload/pic_crop-one', $id);
  147. if ($config) {
  148. if (!$source) {
  149. $source = $this->output['file'];
  150. }
  151. if (!$dest) {
  152. if (strpos($this->name, '_c') !== false) {
  153. $temp = explode('_c', $this->name);
  154. $this->name = $temp[0];
  155. }
  156. $this->name .= '_c' . $id;
  157. $dest = $this->file();
  158. }
  159. $size = $config['width'] . '_' . $config['height'] . '_2';
  160. $this->img()->crop($source, $size, false, true, $dest);
  161. }
  162. }
  163. }