Qiniu.php 5.7 KB

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