common.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if(!function_exists('unThumb')){
  13. function unThumb($src){
  14. return str_replace('/s_','/',$src);
  15. }
  16. }
  17. /**
  18. *判断拼团是否结束*/
  19. function isPinkStatus($pink){
  20. if(!$pink) return false;
  21. return \app\wap\model\store\StorePink::isSetPinkOver($pink);
  22. }
  23. /**
  24. * 设置浏览信息
  25. * @param $uid
  26. * @param int $product_id
  27. * @param int $cate
  28. * @param string $type
  29. * @param string $content
  30. * @param int $min
  31. */
  32. function setView($uid,$product_id=0,$cate=0,$type='',$content='',$min=20){
  33. $Db=think\Db::name('store_visit');
  34. $view=$Db->where(['uid'=>$uid,'product_id'=>$product_id])->field('count,add_time,id')->find();
  35. if($view && $type!='search'){
  36. $time=time();
  37. if(($view['add_time']+$min)<$time){
  38. $Db->where(['id'=>$view['id']])->update(['count'=>$view['count']+1,'add_time'=>time()]);
  39. }
  40. }else{
  41. $cate = explode(',',$cate)[0];
  42. $Db->insert([
  43. 'add_time'=>time(),
  44. 'count'=>1,
  45. 'product_id'=>$product_id,
  46. 'cate_id'=>$cate,
  47. 'type'=>$type,
  48. 'uid'=>$uid,
  49. 'content'=>$content
  50. ]);
  51. }
  52. }
  53. /**
  54. * 创建海报图片
  55. * @param array $product
  56. * @return bool|string
  57. */
  58. function createPoster($product = array()){
  59. header("Content-type: image/jpg");
  60. $filePath = 'public/uploads/poster/'.time().'.jpg';
  61. $dir = iconv("UTF-8", "GBK", "public/uploads/poster");
  62. if(!file_exists($dir)) mkdir($dir,0775,true);
  63. $config = array(
  64. 'text'=>array(
  65. array(
  66. 'text'=>substr(iconv_substr($product['store_name'],0,mb_strlen($product['store_name']),'utf-8'),0,24).'...',
  67. 'left'=>50,
  68. 'top'=>500,
  69. 'fontPath'=>ROOT_PATH.'public/static/font/simsunb.ttf', //字体文件
  70. 'fontSize'=>16, //字号
  71. 'fontColor'=>'40,40,40', //字体颜色
  72. 'angle'=>0,
  73. ),
  74. array(
  75. 'text'=>'¥'.$product['price'],
  76. 'left'=>50,
  77. 'top'=>580,
  78. 'fontPath'=>ROOT_PATH.'public/static/font/simsunb.ttf', //字体文件
  79. 'fontSize'=>16, //字号
  80. 'fontColor'=>'40,40,40', //字体颜色
  81. 'angle'=>0,
  82. )
  83. ),
  84. 'image'=>array(
  85. array(
  86. 'url'=>$product['image'],
  87. 'left'=>50,
  88. 'top'=>70,
  89. 'right'=>0,
  90. 'stream'=>0,
  91. 'bottom'=>0,
  92. 'width'=>350,
  93. 'height'=>350,
  94. 'opacity'=>100,
  95. ),
  96. array(
  97. 'url'=>$product['code_path'],
  98. 'left'=>250,
  99. 'top'=>480,
  100. 'right'=>0,
  101. 'stream'=>0,
  102. 'bottom'=>0,
  103. 'width'=>160,
  104. 'height'=>180,
  105. 'opacity'=>100,
  106. ),
  107. ),
  108. 'background'=>ROOT_PATH.UPLOAD_PATH.'/poster/background.jpg' //背景图
  109. );
  110. $imageDefault = array(
  111. 'left'=>0,
  112. 'top'=>0,
  113. 'right'=>0,
  114. 'bottom'=>0,
  115. 'width'=>100,
  116. 'height'=>100,
  117. 'opacity'=>100
  118. );
  119. $textDefault = array(
  120. 'text'=>'2222222222',
  121. 'left'=>0,
  122. 'top'=>0,
  123. 'fontSize'=>32, //字号
  124. 'fontColor'=>'255,255,255', //字体颜色
  125. 'angle'=>0,
  126. );
  127. $background = $config['background'];//海报最底层得背景
  128. //背景方法
  129. $backgroundInfo = getimagesize($background);
  130. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  131. $background = $backgroundFun($background);
  132. // $backgroundWidth = imagesx($background); //背景宽度
  133. // $backgroundHeight = imagesy($background); //背景高度
  134. $backgroundWidth = 460; //背景宽度
  135. $backgroundHeight = 700; //背景高度
  136. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  137. $color = imagecolorallocate($imageRes, 0, 0, 0);
  138. imagefill($imageRes, 0, 0, $color);
  139. // imageColorTransparent($imageRes, $color); //颜色透明
  140. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background)); //处理了图片
  141. if(!empty($config['image'])){
  142. foreach ($config['image'] as $key => $val) {
  143. $val = array_merge($imageDefault,$val);
  144. $info = getimagesize($val['url']);
  145. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  146. if($val['stream']){ //如果传的是字符串图像流
  147. $info = getimagesizefromstring($val['url']);
  148. $function = 'imagecreatefromstring';
  149. }
  150. $res = $function($val['url']);
  151. $resWidth = $info[0];
  152. $resHeight = $info[1];
  153. //建立画板 ,缩放图片至指定尺寸
  154. $canvas=imagecreatetruecolor($val['width'], $val['height']);
  155. imagefill($canvas, 0, 0, $color);
  156. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  157. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  158. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  159. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  160. //放置图像
  161. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  162. } }
  163. //处理文字
  164. if(!empty($config['text'])){
  165. foreach ($config['text'] as $key => $val) {
  166. $val = array_merge($textDefault,$val);
  167. list($R,$G,$B) = explode(',', $val['fontColor']);
  168. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  169. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  170. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  171. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  172. }
  173. }
  174. //生成图片
  175. $res = imagejpeg ($imageRes,$filePath,90); //保存到本地
  176. imagedestroy($imageRes);
  177. if(!$res) return false;
  178. return $filePath;
  179. }