View.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. # 根据配置生成缩略图 暂时支持缩略图
  14. public function get($file)
  15. {
  16. $array = $this->getFile($file);
  17. $source = $array[0];
  18. $type = $array[1];
  19. $id = $array[2];
  20. if ($id > 0) {
  21. $host = Dever::config('host')->uploadRes;
  22. $root = Dever::data() . 'upload/';
  23. $dest = str_replace($host, $root, $file);
  24. if (!strstr($dest, 'http://') && !is_file($dest)) {
  25. $handle = new Handle();
  26. $method = 'handle_' . $type;
  27. $handle->$method($id, $source, $dest);
  28. }
  29. }
  30. return $file;
  31. }
  32. public function source($file)
  33. {
  34. # 搜索文件信息
  35. $array = $this->getFile($file);
  36. return $array[0];
  37. }
  38. private function getFile($file)
  39. {
  40. $type = $id = false;
  41. if (strstr($file, '_c')) {
  42. $array = explode('_c', $file);
  43. $type = 'c';
  44. } elseif (strstr($file, '_t')) {
  45. $array = explode('_t', $file);
  46. $type = 't';
  47. }
  48. if (isset($array)) {
  49. $temp = explode('.', $array[1]);
  50. $id = $temp[0];
  51. $file = $array[0] . '.' . $temp[1];
  52. }
  53. return array($file, $type, $id);
  54. }
  55. /**
  56. * create 生成图片
  57. * @param array $config
  58. * @param string $filename
  59. *
  60. * @return mixed
  61. */
  62. public function create($config = array(), $filename = '')
  63. {
  64. $this->img = isset($this->img) ? $this->img : new Img();
  65. $mark = array
  66. (
  67. 'method' => 'mark',
  68. 'water' => $config['image'][0]['url'],
  69. 'position' => array(100,200),
  70. 'width' => 100,
  71. 'height' => 100,
  72. # 开启圆角
  73. //'radius' => -1,
  74. );
  75. $send[] = $mark;
  76. $mark = array
  77. (
  78. 'method' => 'mark',
  79. 'water' => $config['image'][0]['url'],
  80. 'position' => array(200,500),
  81. 'width' => 100,
  82. 'height' => 100,
  83. # 开启圆角
  84. //'radius' => -1,
  85. );
  86. $send[] = $mark;
  87. $name = $this->img->init($config['background'], $send, true, $config['background'] . '.jpg');
  88. print_r($name);die;
  89. }
  90. public function creates($config = array(), $filename = '')
  91. {
  92. if(empty($filename)) header("content-type: image/png");
  93. $imageDefault = array(
  94. 'left'=>0,
  95. 'top'=>0,
  96. 'right'=>0,
  97. 'bottom'=>0,
  98. 'width'=>100,
  99. 'height'=>100,
  100. 'opacity'=>100
  101. );
  102. $textDefault = array(
  103. 'text'=>'',
  104. 'left'=>0,
  105. 'top'=>0,
  106. 'fontSize'=>32, //字号
  107. 'fontColor'=>'255,255,255', //字体颜色
  108. 'angle'=>0,
  109. );
  110. $background = $config['background'];//海报最底层得背景
  111. //背景方法
  112. $backgroundInfo = getimagesize($background);
  113. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  114. $background = $backgroundFun($background);
  115. $backgroundWidth = imagesx($background); //背景宽度
  116. $backgroundHeight = imagesy($background); //背景高度
  117. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  118. $color = imagecolorallocate($imageRes, 0, 0, 0);
  119. imagefill($imageRes, 0, 0, $color);
  120. // imageColorTransparent($imageRes, $color); //颜色透明
  121. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  122. //处理了图片
  123. if (!empty($config['image'])) {
  124. foreach ($config['image'] as $key => $val) {
  125. $val = array_merge($imageDefault,$val);
  126. $info = getimagesize($val['url']);
  127. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  128. if ($val['stream']) { //如果传的是字符串图像流
  129. $info = getimagesizefromstring($val['url']);
  130. $function = 'imagecreatefromstring';
  131. }
  132. if (isset($val['radius']) && $val['radius']) {
  133. $res = $this->radius($val['url'], $val['radius']);
  134. } else {
  135. $res = $function($val['url']);
  136. }
  137. $resWidth = $info[0];
  138. $resHeight = $info[1];
  139. //建立画板 ,缩放图片至指定尺寸
  140. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  141. $color = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明
  142. imagefill($canvas, 0, 0, $color);
  143. imageColorTransparent($canvas, $color);
  144. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  145. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  146. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  147. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  148. //放置图像
  149. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  150. }
  151. }
  152. # 处理文字
  153. if (!empty($config['text'])) {
  154. foreach ($config['text'] as $key => $val) {
  155. $val = array_merge($textDefault,$val);
  156. list($R,$G,$B) = explode(',', $val['fontColor']);
  157. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  158. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  159. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  160. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  161. }
  162. }
  163. # 生成图片
  164. if (!empty($filename)) {
  165. $res = imagepng ($imageRes,$filename,9);
  166. imagedestroy($imageRes);
  167. if(!$res) return false;
  168. return $filename;
  169. } else {
  170. imagepng ($imageRes, null, 9);
  171. imagedestroy($imageRes);
  172. }
  173. }
  174. }