Qiniu.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $config = Dever::db('upload/yun')->one($this->config['yun']);
  16. $accessKey = $config['appkey'];
  17. $secretKey = $config['appsecret'];
  18. $this->host = $config['host'];
  19. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  20. $bucket = $this->config['bucket'];
  21. $policy = array(
  22. 'callbackUrl' => 'http://your.domain.com/upload_verify_callback.php',
  23. 'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
  24. );
  25. // 生成上传Token
  26. $token = $auth->uploadToken($bucket, null, 3600);
  27. // 构建 UploadManager 对象
  28. $upload = new \Qiniu\Storage\UploadManager();
  29. $this->getName();
  30. $file = $this->file;
  31. if ($this->config['cover'] == 3) {
  32. $this->name($this->output['file'] . '_' . microtime() . '_' . rand(0,1000))->file();
  33. $file = $this->file;
  34. }
  35. if ($this->base64) {
  36. list($ret, $err) = $upload->put($token, $file, $this->data['file']['tmp_name'], $param = null);
  37. } else {
  38. list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
  39. }
  40. if (isset($ret['hash']) && isset($ret['key'])) {
  41. $this->file = $ret['key'];
  42. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
  43. if (!$this->limit) {
  44. $data = Dever::curl($this->output['url'] . '?imageInfo');
  45. if ($data) {
  46. $data = json_decode($data, true);
  47. $this->limit = array($data['width'], $data['height']);
  48. $this->size = $data['size'];
  49. $this->ext = '.' . $data['format'];
  50. }
  51. }
  52. if (isset($file) && $file) {
  53. $this->update($file['id']);
  54. } else {
  55. $this->insert();
  56. }
  57. return $this->output['url'];
  58. }
  59. }
  60. public function file()
  61. {
  62. if ($this->base64) {
  63. $data = pathinfo($this->data['pic']);
  64. $data['dirname'] = str_replace($this->host, '', $data['dirname']);
  65. $this->file = $data['dirname'] . '/' . $data['basename'] . $this->name . $this->ext;
  66. } else {
  67. $this->file = $this->path . $this->name . $this->ext;
  68. }
  69. $this->output['file'] = $this->file;
  70. $this->output['url'] = $this->host . $this->file;
  71. return $this->output['file'];
  72. }
  73. /**
  74. * water
  75. *
  76. * @return mixed
  77. */
  78. public function handle_w($id)
  79. {
  80. $config = Dever::load('upload/pic_water-one', $id);
  81. if ($config) {
  82. $this->name .= '_w' . $id;
  83. $water = array('water'=> $config['water'], 'position'=> ($config['water_position'] ? $config['water_position'] : 1));
  84. $this->img()->mark($this->output['file'], $water, true, $this->file());
  85. }
  86. }
  87. /**
  88. * compress
  89. *
  90. * @return mixed
  91. */
  92. public function handle_p($num, $source = false, $dest = false)
  93. {
  94. if (!$source) {
  95. $source = $this->output['file'];
  96. }
  97. if (!$dest) {
  98. if (strpos($this->name, '_p') !== false) {
  99. $temp = explode('_p', $this->name);
  100. $this->name = $temp[0];
  101. }
  102. $file = $this->output['file'];
  103. $url = $this->output['url'];
  104. $this->name .= '_p' . $num;
  105. $dest = $this->file();
  106. $this->output['file'] = $file;
  107. $this->output['url'] = $url;
  108. }
  109. $this->img()->setType('im');
  110. $this->img()->compress($source, $num, $dest);
  111. }
  112. /**
  113. * webp
  114. *
  115. * @return mixed
  116. */
  117. public function handle_wp($num, $source = false, $dest = false)
  118. {
  119. if (!$source) {
  120. $source = $this->output['file'];
  121. }
  122. if (!$dest) {
  123. if (strpos($this->name, '_wp') !== false) {
  124. $temp = explode('_wp', $this->name);
  125. $this->name = $temp[0];
  126. }
  127. $file = $this->output['file'];
  128. $url = $this->output['url'];
  129. $this->ext = '.webp';
  130. $this->name .= '_wp' . $num;
  131. $dest = $this->file();
  132. $this->output['file'] = $file;
  133. $this->output['url'] = $url;
  134. }
  135. $this->img()->setType('im');
  136. $this->img()->webp($source, $num, $dest);
  137. }
  138. /**
  139. * thumb
  140. *
  141. * @return mixed
  142. */
  143. public function handle_t($id, $source = false, $dest = false, $path = '')
  144. {
  145. $config = Dever::load('upload/pic_thumb-one', $id);
  146. if ($config) {
  147. if (!$source) {
  148. $source = $this->output['file'];
  149. }
  150. $dest = $source . '?imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  151. if (isset($config['compress']) && $config['compress'] > 0) {
  152. $dest = $dest . '/q/' . $config['compress'];
  153. }
  154. }
  155. return $dest;
  156. }
  157. /**
  158. * crop
  159. *
  160. * @return mixed
  161. */
  162. public function handle_c($id, $source = false, $dest = false)
  163. {
  164. $config = Dever::load('upload/pic_crop-one', $id);
  165. if ($config) {
  166. if (!$source) {
  167. $source = $this->output['file'];
  168. }
  169. if (!$dest) {
  170. if (strpos($this->name, '_c') !== false) {
  171. $temp = explode('_c', $this->name);
  172. $this->name = $temp[0];
  173. }
  174. $this->name .= '_c' . $id;
  175. $dest = $this->file();
  176. }
  177. $size = $config['width'] . '_' . $config['height'] . '_2';
  178. $this->img()->crop($source, $size, false, true, $dest);
  179. }
  180. }
  181. }