Qiniu.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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->file = $this->data['local'] . $this->name . $this->ext;
  44. } else {
  45. $this->file = $this->path . $this->name . $this->ext;
  46. }
  47. $this->output['file'] = $this->file;
  48. if (strstr($this->file, 'http')) {
  49. $this->output['url'] = $this->file;
  50. } else {
  51. $this->output['url'] = $this->host . $this->file;
  52. }
  53. //$this->output['url'] = '{uploadYun}' . $this->file;
  54. return $this->output['file'];
  55. }
  56. /**
  57. * water
  58. *
  59. * @return mixed
  60. */
  61. public function handle_w($id)
  62. {
  63. $config = Dever::load('upload/pic_water-one', $id);
  64. if ($config) {
  65. $this->name .= '_w' . $id;
  66. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  67. $this->img()->mark($this->output['file'], $water, true, $this->file());
  68. }
  69. }
  70. /**
  71. * compress
  72. *
  73. * @return mixed
  74. */
  75. public function handle_p($num, $source = false, $dest = false)
  76. {
  77. if (!$source) {
  78. $source = $this->output['file'];
  79. }
  80. if (!$dest) {
  81. if (strpos($this->name, '_p') !== false) {
  82. $temp = explode('_p', $this->name);
  83. $this->name = $temp[0];
  84. }
  85. $file = $this->output['file'];
  86. $url = $this->output['url'];
  87. $this->name .= '_p' . $num;
  88. $dest = $this->file();
  89. $this->output['file'] = $file;
  90. $this->output['url'] = $url;
  91. }
  92. $this->img()->setType('im');
  93. $this->img()->compress($source, $num, $dest);
  94. }
  95. /**
  96. * webp
  97. *
  98. * @return mixed
  99. */
  100. public function handle_wp($num, $source = false, $dest = false)
  101. {
  102. if (!$source) {
  103. $source = $this->output['file'];
  104. }
  105. if (!$dest) {
  106. if (strpos($this->name, '_wp') !== false) {
  107. $temp = explode('_wp', $this->name);
  108. $this->name = $temp[0];
  109. }
  110. $file = $this->output['file'];
  111. $url = $this->output['url'];
  112. $this->ext = '.webp';
  113. $this->name .= '_wp' . $num;
  114. $dest = $this->file();
  115. $this->output['file'] = $file;
  116. $this->output['url'] = $url;
  117. }
  118. $this->img()->setType('im');
  119. $this->img()->webp($source, $num, $dest);
  120. }
  121. /**
  122. * thumb
  123. *
  124. * @return mixed
  125. */
  126. public function handle_t($id, $source = false, $dest = false, $path = '')
  127. {
  128. $config = Dever::load('upload/pic_thumb-one', $id);
  129. if ($config) {
  130. if (!$source) {
  131. $source = $this->output['file'];
  132. }
  133. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  134. $dest = $this->data['host'] . $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  135. if (isset($config['compress']) && $config['compress'] > 0) {
  136. $dest .= '/q/' . $config['compress'];
  137. }
  138. $dest .= '|imageslim';
  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. if (!$dest) {
  155. if (strpos($this->name, '_c') !== false) {
  156. $temp = explode('_c', $this->name);
  157. $this->name = $temp[0];
  158. }
  159. $this->name .= '_c' . $id;
  160. $dest = $this->file();
  161. }
  162. $size = $config['width'] . '_' . $config['height'] . '_2';
  163. $this->img()->crop($source, $size, false, true, $dest);
  164. }
  165. }
  166. }