Qiniu.php 5.2 KB

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