View.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 kindeditorFile()
  14. {
  15. echo 11;die;
  16. }
  17. # webp
  18. public function webp($file)
  19. {
  20. $host = Dever::config('host')->uploadRes;
  21. $root = Dever::data() . 'upload/';
  22. $source = str_replace(array('{uploadRes}', $host), $root, $file);
  23. $dest = $source . '.webp';
  24. if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
  25. if (!is_file($dest)) {
  26. Dever::run('cwebp '.$source.' -o ' . $dest . '');
  27. header('Content-type: image/webp');
  28. readfile($source);
  29. } else {
  30. header('Content-type: image/webp');
  31. readfile($dest);
  32. }
  33. } else {
  34. header('Content-type: image/webp');
  35. readfile($source);
  36. }
  37. }
  38. # 根据配置生成缩略图 暂时支持缩略图
  39. public function get($file)
  40. {
  41. $array = $this->getFile($file);
  42. $source = $array[0];
  43. $type = $array[1];
  44. $id = $array[2];
  45. if ($id > 0) {
  46. $host = Dever::config('host')->uploadRes;
  47. $root = Dever::data() . 'upload/';
  48. $dest = str_replace(array('{uploadRes}', $host), $root, $file);
  49. if (strstr($source, $host)) {
  50. $source = $root . str_replace($host, '', $source);
  51. if (!is_file($source)) {
  52. $source = $array[0];
  53. }
  54. }
  55. if ($type == 'wp') {
  56. $temp = explode('.', $dest);
  57. $dest = $temp[0] . '.webp';
  58. $file = str_replace($root, $host, $dest);
  59. }
  60. if (!strstr($dest, 'http://') && !is_file($dest)) {
  61. $handle = new Handle();
  62. $method = 'handle_' . $type;
  63. $handle->$method($id, $source, $dest);
  64. }
  65. }
  66. return $file;
  67. }
  68. public function source($file)
  69. {
  70. # 搜索文件信息
  71. $array = $this->getFile($file);
  72. return $array[0];
  73. }
  74. private function getFile($file)
  75. {
  76. $type = $id = false;
  77. if (strstr($file, '_wp')) {
  78. $array = explode('_wp', $file);
  79. $type = 'wp';
  80. } elseif (strstr($file, '_p')) {
  81. $array = explode('_p', $file);
  82. $type = 'p';
  83. } elseif (strstr($file, '_c')) {
  84. $array = explode('_c', $file);
  85. $type = 'c';
  86. } elseif (strstr($file, '_t')) {
  87. $array = explode('_t', $file);
  88. $type = 't';
  89. }
  90. if (isset($array)) {
  91. $temp = explode('.', $array[1]);
  92. $id = $temp[0];
  93. $file = $array[0] . '.' . $temp[1];
  94. }
  95. return array($file, $type, $id);
  96. }
  97. /**
  98. * create 生成图片
  99. * @param array $config
  100. * @param string $filename
  101. *
  102. * @return mixed
  103. */
  104. public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
  105. {
  106. $this->img = isset($this->img) ? $this->img : new Img();
  107. /*
  108. $config = array
  109. (
  110. 'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',
  111. 'param' => array
  112. (
  113. 0 => array
  114. (
  115. 'method' => 'mark',
  116. 'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',
  117. //left,top
  118. 'position' => array(100,200),
  119. 'width' => 100,
  120. 'height' => 100,
  121. ),
  122. 1 => array
  123. (
  124. 'method' => 'txt',
  125. 'name' => 'test文字',
  126. 'color' => '#FF0000',
  127. 'position' => array(100,200),
  128. 'size' => 50,
  129. 'angle' => 0,
  130. 'font' => '/www/grow/config/fonts/simsun.ttc',
  131. ),
  132. ),
  133. );
  134. */
  135. if (!$filename) {
  136. $filename = Dever::id();
  137. }
  138. $file = Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';
  139. if (!is_file($file) || $update == 1) {
  140. $this->img->setType($type);
  141. $result = $this->img->init($config['background'], $config['param'], true, $file);
  142. if ($result) {
  143. $file = array_pop($result);
  144. }
  145. } else {
  146. }
  147. $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  148. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  149. $file = Dever::pic($file);
  150. return $file;
  151. }
  152. public function creates($config = array(), $filename = '')
  153. {
  154. if(empty($filename)) header("content-type: image/png");
  155. $imageDefault = array(
  156. 'left'=>0,
  157. 'top'=>0,
  158. 'right'=>0,
  159. 'bottom'=>0,
  160. 'width'=>100,
  161. 'height'=>100,
  162. 'opacity'=>100
  163. );
  164. $textDefault = array(
  165. 'text'=>'',
  166. 'left'=>0,
  167. 'top'=>0,
  168. 'fontSize'=>32, //字号
  169. 'fontColor'=>'255,255,255', //字体颜色
  170. 'angle'=>0,
  171. );
  172. $background = $config['background'];//海报最底层得背景
  173. //背景方法
  174. $backgroundInfo = getimagesize($background);
  175. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  176. $background = $backgroundFun($background);
  177. $backgroundWidth = imagesx($background); //背景宽度
  178. $backgroundHeight = imagesy($background); //背景高度
  179. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  180. $color = imagecolorallocate($imageRes, 0, 0, 0);
  181. imagefill($imageRes, 0, 0, $color);
  182. // imageColorTransparent($imageRes, $color); //颜色透明
  183. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  184. //处理了图片
  185. if (!empty($config['image'])) {
  186. foreach ($config['image'] as $key => $val) {
  187. $val = array_merge($imageDefault,$val);
  188. $info = getimagesize($val['url']);
  189. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  190. if ($val['stream']) { //如果传的是字符串图像流
  191. $info = getimagesizefromstring($val['url']);
  192. $function = 'imagecreatefromstring';
  193. }
  194. if (isset($val['radius']) && $val['radius']) {
  195. $res = $this->radius($val['url'], $val['radius']);
  196. } else {
  197. $res = $function($val['url']);
  198. }
  199. $resWidth = $info[0];
  200. $resHeight = $info[1];
  201. //建立画板 ,缩放图片至指定尺寸
  202. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  203. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  204. imagefill($canvas, 0, 0, $color);
  205. imageColorTransparent($canvas, $color);
  206. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  207. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  208. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  209. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  210. //放置图像
  211. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  212. }
  213. }
  214. # 处理文字
  215. if (!empty($config['text'])) {
  216. foreach ($config['text'] as $key => $val) {
  217. $val = array_merge($textDefault,$val);
  218. list($R,$G,$B) = explode(',', $val['fontColor']);
  219. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  220. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  221. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  222. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  223. }
  224. }
  225. # 生成图片
  226. if (!empty($filename)) {
  227. $res = imagepng ($imageRes,$filename,9);
  228. imagedestroy($imageRes);
  229. if(!$res) return false;
  230. return $filename;
  231. } else {
  232. imagepng ($imageRes, null, 9);
  233. imagedestroy($imageRes);
  234. }
  235. }
  236. }