Qiniu.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $method = Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file);
  18. $ret = $method->upload($file, $this->data['file']['tmp_name'], $options, $this->base64);
  19. if (isset($ret['hash']) && isset($ret['key'])) {
  20. $this->file = $ret['key'];
  21. if (!$this->limit) {
  22. $data = Dever::curl($this->output['url'] . '?imageInfo');
  23. if ($data) {
  24. $data = Dever::json_decode($data);
  25. if (isset($data['width'])) {
  26. $this->limit = array($data['width'], $data['height']);
  27. $this->size = $data['size'];
  28. $this->ext = '.' . $data['format'];
  29. $this->limit();
  30. if ($this->output['status'] == -1) {
  31. $method->delete($this->file);
  32. return $this->output;
  33. }
  34. } else {
  35. $this->output['status'] = -1;
  36. $this->output['message'] = '上传失败';
  37. $method->delete($this->file);
  38. return $this->output;
  39. }
  40. } else {
  41. $this->output['status'] = -1;
  42. $this->output['message'] = '上传失败';
  43. $method->delete($this->file);
  44. return $this->output;
  45. }
  46. }
  47. $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'clear' => true));
  48. if (isset($file) && $file) {
  49. $this->update($file['id']);
  50. } else {
  51. $this->insert();
  52. }
  53. return $this->output['url'];
  54. }
  55. }
  56. public function file()
  57. {
  58. if ($this->base64 && isset($this->data['local'])) {
  59. $data = parse_url($this->data['local']);
  60. $data['path'] = str_replace(Dever::data() . 'upload/', '', $data['path']);
  61. $this->path = ltrim($data['path'], '/');
  62. }
  63. $this->file = $this->path . $this->name . $this->ext;
  64. $this->output['file'] = $this->file;
  65. $this->output['url'] = $this->host . $this->file;
  66. //$this->output['url'] = '{uploadYun}' . $this->file;
  67. return $this->output['file'];
  68. }
  69. /**
  70. * water
  71. *
  72. * @return mixed
  73. */
  74. public function handle_w($id, $source = false)
  75. {
  76. $config = Dever::load('upload/pic_water-one', $id);
  77. if ($config) {
  78. if (isset(Dever::config('base')->handle_w) && Dever::config('base')->handle_w) {
  79. # 走本地水印
  80. list($yun, $file) = $this->yun();
  81. $this->name .= '_w' . $id;
  82. $file = $this->file();
  83. $options = array();
  84. Dever::load('upload/lib/view/qiniu')->connect($yun, $this->config, $file)->upload($file, Dever::config('base')->handle_w, $options);
  85. } else {
  86. # 走七牛的水印
  87. # 参考:https://developer.qiniu.com/dora/1316/image-watermarking-processing-watermark
  88. # 暂时未实现
  89. return;
  90. if (!$source) {
  91. $source = $this->output['file'];
  92. }
  93. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  94. if (!strstr($source, 'http')) {
  95. $source = $this->data['host'] . $source;
  96. }
  97. if (strstr($source, '?')) {
  98. $prefix = '&';
  99. } else {
  100. $prefix = '?';
  101. }
  102. $dest = $source . $prefix . 'watermark/1/image/a29kbzovL2RldmVsb3Blci1kb2N1bWVudHMtaW1hZ2UvcWluaXV5dW4ucG5n/dissolve/50/gravity/SouthEast/dx/20/dy/20/ws/0.2';
  103. $dest .= '|imageslim';
  104. }
  105. }
  106. }
  107. /**
  108. * compress
  109. *
  110. * @return mixed
  111. */
  112. public function handle_p($num, $source = false, $dest = false)
  113. {
  114. if (!$source) {
  115. $source = $this->output['file'];
  116. }
  117. if (!$dest) {
  118. if (strpos($this->name, '_p') !== false) {
  119. $temp = explode('_p', $this->name);
  120. $this->name = $temp[0];
  121. }
  122. $file = $this->output['file'];
  123. $url = $this->output['url'];
  124. $this->name .= '_p' . $num;
  125. $dest = $this->file();
  126. $this->output['file'] = $file;
  127. $this->output['url'] = $url;
  128. }
  129. $this->img()->setType('im');
  130. $this->img()->compress($source, $num, $dest);
  131. }
  132. /**
  133. * webp
  134. *
  135. * @return mixed
  136. */
  137. public function handle_wp($num, $source = false, $dest = false)
  138. {
  139. if (!$source) {
  140. $source = $this->output['file'];
  141. }
  142. if (!$dest) {
  143. if (strpos($this->name, '_wp') !== false) {
  144. $temp = explode('_wp', $this->name);
  145. $this->name = $temp[0];
  146. }
  147. $file = $this->output['file'];
  148. $url = $this->output['url'];
  149. $this->ext = '.webp';
  150. $this->name .= '_wp' . $num;
  151. $dest = $this->file();
  152. $this->output['file'] = $file;
  153. $this->output['url'] = $url;
  154. }
  155. $this->img()->setType('im');
  156. $this->img()->webp($source, $num, $dest);
  157. }
  158. /**
  159. * thumb
  160. *
  161. * @return mixed
  162. */
  163. public function handle_t($id, $source = false, $dest = false, $path = '')
  164. {
  165. $config = Dever::load('upload/pic_thumb-one', $id);
  166. if ($config) {
  167. if (!$source) {
  168. $source = $this->output['file'];
  169. }
  170. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  171. if (!strstr($source, 'http')) {
  172. $source = $this->data['host'] . $source;
  173. }
  174. /*
  175. if (strstr($source, '?')) {
  176. $prefix = '&';
  177. } else {
  178. $prefix = '?';
  179. }*/
  180. $prefix = '';
  181. if (!strstr($source, 'imageMogr2')) {
  182. $prefix = '?imageMogr2';
  183. }
  184. if (strstr($source, '|imageslim')) {
  185. $source = str_replace('|imageslim', '', $source);
  186. }
  187. //$dest = $source . $prefix . 'imageView2/2/w/'.$config['width'].'/h/'.$config['height'];
  188. if ($config['height'] <= 0) {
  189. $config['height'] = '';
  190. }
  191. $dest = $source . $prefix . '/thumbnail/'.$config['width'].'x'.$config['height'] . '>';
  192. if (isset($config['compress']) && $config['compress'] > 0) {
  193. //$dest .= '/q/' . $config['compress'];
  194. $dest .= '/quality/' . $config['compress'];
  195. }
  196. $dest .= '|imageslim';
  197. $this->output['url'] = $dest;
  198. }
  199. return $dest;
  200. }
  201. /**
  202. * crop
  203. *
  204. * @return mixed
  205. */
  206. public function handle_c($id, $source = false, $dest = false, $path = '')
  207. {
  208. $config = Dever::load('upload/pic_crop-one', $id);
  209. if ($config) {
  210. if (!$source) {
  211. $source = $this->output['file'];
  212. }
  213. //?imageView2/2/w/360/h/270/format/png/q/75|imageslim
  214. if (!strstr($source, 'http')) {
  215. $source = $this->data['host'] . $source;
  216. }
  217. $temp = parse_url($source);
  218. $file = ltrim($temp['path'], '/');
  219. $file = Dever::db('upload/file')->one(array('file' => $file));
  220. if (strstr($source, '|imageslim')) {
  221. $source = str_replace('|imageslim', '', $source);
  222. }
  223. $x = $y = 0;
  224. if ($file) {
  225. if (strstr($source, 'thumbnail/')) {
  226. $temp = explode('thumbnail/', $source);
  227. if (isset($temp[1])) {
  228. $temp = explode('x>', $temp[1]);
  229. $width = $file['width'];
  230. $file['width'] = $temp[0];
  231. $radio = $file['width']/$width;
  232. if (isset($temp[1]) && $temp[1]) {
  233. $file['height'] = $temp[1];
  234. } else {
  235. $file['height'] = round($radio*$file['height'], 2);
  236. }
  237. }
  238. }
  239. list($x, $y) = $this->img()->get_position($file['width'], $file['height'], $config['width'], $config['height'], 'crop', $config['type']);
  240. }
  241. $prefix = '';
  242. if (!strstr($source, 'imageMogr2')) {
  243. $prefix = '?imageMogr2';
  244. }
  245. $dest = $source . $prefix . '/crop/!'.$config['width'].'x'.$config['height'].'a'.$x.'a' . $y;
  246. $dest .= '|imageslim';
  247. $this->output['url'] = $dest;
  248. }
  249. return $dest;
  250. }
  251. }