Qiniu.php 8.6 KB

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