View.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 查看图片的几个方法
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Upload\Src;
  8. use Dever;
  9. use Upload\Src\Store\Local as Handle;
  10. use Upload\Src\Lib\Img;
  11. class View
  12. {
  13. public function files()
  14. {
  15. $data = array();
  16. $key = Dever::input('key');
  17. $cate = Dever::input('cate');
  18. $name = Dever::input('name');
  19. $tag = Dever::input('tag');
  20. $data['cur'] = Dever::input('cur');
  21. $data['search_pg'] = Dever::input('pg', 1);
  22. $param = array();
  23. $param['upload'] = $key;
  24. if ($name) {
  25. $param['source_name'] = $name;
  26. $param['name'] = $name;
  27. }
  28. if ($cate && $cate > 0) {
  29. $param['cate'] = $cate;
  30. }
  31. $data['search_cate'] = $cate;
  32. $data['search_tag'] = $tag;
  33. $data['search_name'] = $name;
  34. $data['file'] = Dever::db('upload/file')->getData($param);
  35. $data['total'] = Dever::total();
  36. $data['cate'] = Dever::db('upload/cate')->state();
  37. $data['config'] = Dever::db('upload/upload')->one($key);
  38. if ($data['file']) {
  39. foreach ($data['file'] as $k => $v) {
  40. $data['file'][$k]['url'] = Dever::upload('{uploadRes}' . $v['file']);
  41. $data['file'][$k]['pic'] = Dever::upload('{uploadRes}' . $v['file']);
  42. }
  43. }
  44. $data = Dever::render('show', $data);
  45. Dever::out($data);
  46. }
  47. public function kindeditorFile()
  48. {
  49. $key = Dever::input('key');
  50. $param['upload'] = $key;
  51. $file = Dever::db('upload/file')->state();
  52. $config = Dever::db('upload/upload')->one($key);
  53. $list = array();
  54. if ($file) {
  55. $i = 0;
  56. foreach ($file as $k => $v) {
  57. if ($v['name'] && $v['file']) {
  58. $list[$i] = array();
  59. $list[$i]['is_dir'] = false;
  60. $list[$i]['has_file'] = false;
  61. $list[$i]['filesize'] = $v['size'];
  62. $list[$i]['dir_path'] = '';
  63. $list[$i]['is_photo'] = true;
  64. $list[$i]['filetype'] = str_replace('.', '', $v['ext']);
  65. $list[$i]['filename'] = $v['source_name'];
  66. $list[$i]['path'] = '';
  67. $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);
  68. $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));
  69. $i++;
  70. }
  71. }
  72. }
  73. $result = array();
  74. //相对于根目录的上一级目录
  75. $result['moveup_dir_path'] = $config['id'];
  76. //相对于根目录的当前目录
  77. $result['current_dir_path'] = '';
  78. //当前目录的URL
  79. $result['current_url'] = '';
  80. //文件数
  81. $result['total_count'] = count($list);
  82. //文件列表数组
  83. $result['file_list'] = $list;
  84. Dever::outDiy($result);
  85. }
  86. # webp
  87. public function webp($file)
  88. {
  89. $host = Dever::config('host')->uploadRes;
  90. $root = Dever::data() . 'upload/';
  91. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  92. $dest = $source . '.webp';
  93. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  94. if (!is_file($dest)) {
  95. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  96. header('Content-type: image/webp');
  97. readfile($source);
  98. } else {
  99. header('Content-type: image/webp');
  100. readfile($dest);
  101. }
  102. } else {
  103. header('Content-type: image/webp');
  104. readfile($source);
  105. }
  106. }
  107. # 根据配置生成缩略图 暂时支持缩略图
  108. public function get($file)
  109. {
  110. $array = $this->getFile($file);
  111. $source = $array[0];
  112. $type = $array[1];
  113. $id = $array[2];
  114. if ($id > 0) {
  115. $host = Dever::config('host')->uploadRes;
  116. $root = Dever::data() . 'upload/';
  117. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  118. if (strstr($source, $host)) {
  119. $source = $root . str_replace($host, '', $source);
  120. if (!is_file($source)) {
  121. $source = $array[0];
  122. }
  123. }
  124. if ($type == 'wp') {
  125. $temp = explode('.', $dest);
  126. $dest = $temp[0] . '.webp';
  127. $file = str_replace($root, $host, $dest);
  128. }
  129. if (!strstr($dest, 'http://') && !is_file($dest)) {
  130. $handle = new Handle();
  131. $method = 'handle_' . $type;
  132. $handle->$method($id, $source, $dest);
  133. }
  134. }
  135. return $file;
  136. }
  137. public function source($file)
  138. {
  139. # 搜索文件信息
  140. $array = $this->getFile($file);
  141. return $array[0];
  142. }
  143. private function getFile($file)
  144. {
  145. $type = $id = false;
  146. if (strstr($file, '_wp')) {
  147. $array = explode('_wp', $file);
  148. $type = 'wp';
  149. } elseif (strstr($file, '_p')) {
  150. $array = explode('_p', $file);
  151. $type = 'p';
  152. } elseif (strstr($file, '_c')) {
  153. $array = explode('_c', $file);
  154. $type = 'c';
  155. } elseif (strstr($file, '_t')) {
  156. $array = explode('_t', $file);
  157. $type = 't';
  158. }
  159. if (isset($array)) {
  160. $temp = explode('.', $array[1]);
  161. $id = $temp[0];
  162. $file = $array[0] . '.' . $temp[1];
  163. }
  164. return array($file, $type, $id);
  165. }
  166. /**
  167. * create 生成图片
  168. * @param array $config
  169. * @param string $filename
  170. *
  171. * @return mixed
  172. */
  173. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  174. {
  175. $this->img = isset($this->img) ? $this->img : new Img();
  176. /*
  177. $config = array
  178. (
  179. 'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
  180. 'param' => array
  181. (
  182. 0 => array
  183. (
  184. 'method' => 'mark',
  185. 'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
  186. //left,top
  187. 'position' => array(100,200),
  188. 'width' => 100,
  189. 'height' => 100,
  190. ),
  191. 1 => array
  192. (
  193. 'method' => 'txt',
  194. 'name' => 'test文字',
  195. 'color' => '#FF0000',
  196. 'position' => array(100,200),
  197. 'size' => 50,
  198. 'angle' => 0,
  199. 'font' => '/www/grow/config/fonts/simsun.ttc',
  200. ),
  201. ),
  202. );
  203. */
  204. if (!$filename) {
  205. $filename = Dever::id();
  206. }
  207. $file = Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
  208. if (!is_file($file) || $update == 1) {
  209. $this->img->setType($type);
  210. $result = $this->img->init($config['background'], $config['param'], true, $file);
  211. if ($result) {
  212. $file = array_pop($result);
  213. }
  214. } else {
  215. }
  216. $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  217. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  218. $file = Dever::pic($file);
  219. return $file;
  220. }
  221. public function creates($config = array(), $filename = '')
  222. {
  223. if(empty($filename)) header("content-type: image/png");
  224. $imageDefault = array(
  225. 'left'=>0,
  226. 'top'=>0,
  227. 'right'=>0,
  228. 'bottom'=>0,
  229. 'width'=>100,
  230. 'height'=>100,
  231. 'opacity'=>100
  232. );
  233. $textDefault = array(
  234. 'text'=>'',
  235. 'left'=>0,
  236. 'top'=>0,
  237. 'fontSize'=>32, //字号
  238. 'fontColor'=>'255,255,255', //字体颜色
  239. 'angle'=>0,
  240. );
  241. $background = $config['background'];//海报最底层得背景
  242. //背景方法
  243. $backgroundInfo = getimagesize($background);
  244. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  245. $background = $backgroundFun($background);
  246. $backgroundWidth = imagesx($background); //背景宽度
  247. $backgroundHeight = imagesy($background); //背景高度
  248. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  249. $color = imagecolorallocate($imageRes, 0, 0, 0);
  250. imagefill($imageRes, 0, 0, $color);
  251. // imageColorTransparent($imageRes, $color); //颜色透明
  252. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  253. //处理了图片
  254. if (!empty($config['image'])) {
  255. foreach ($config['image'] as $key => $val) {
  256. $val = array_merge($imageDefault,$val);
  257. $info = getimagesize($val['url']);
  258. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  259. if ($val['stream']) { //如果传的是字符串图像流
  260. $info = getimagesizefromstring($val['url']);
  261. $function = 'imagecreatefromstring';
  262. }
  263. if (isset($val['radius']) && $val['radius']) {
  264. $res = $this->radius($val['url'], $val['radius']);
  265. } else {
  266. $res = $function($val['url']);
  267. }
  268. $resWidth = $info[0];
  269. $resHeight = $info[1];
  270. //建立画板 ,缩放图片至指定尺寸
  271. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  272. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  273. imagefill($canvas, 0, 0, $color);
  274. imageColorTransparent($canvas, $color);
  275. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  276. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  277. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  278. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  279. //放置图像
  280. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  281. }
  282. }
  283. # 处理文字
  284. if (!empty($config['text'])) {
  285. foreach ($config['text'] as $key => $val) {
  286. $val = array_merge($textDefault,$val);
  287. list($R,$G,$B) = explode(',', $val['fontColor']);
  288. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  289. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  290. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  291. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  292. }
  293. }
  294. # 生成图片
  295. if (!empty($filename)) {
  296. $res = imagepng ($imageRes,$filename,9);
  297. imagedestroy($imageRes);
  298. if(!$res) return false;
  299. return $filename;
  300. } else {
  301. imagepng ($imageRes, null, 9);
  302. imagedestroy($imageRes);
  303. }
  304. }
  305. }