Qiniu.php 9.1 KB

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