/**
* @param $imageDefault
* @param $textDefault
* @param $background
* @param string $filename
* @param array $config
*/
function getbgqrcode($user_id,$wx_headimg,$nickname,$filename){
if(empty($filename))
header("content-type: image/png");
$imageDefault = array(
'left'=>260,
'top'=>580,
'right'=>0,
'bottom'=>0,
'width'=>245,
'height'=>245,
'opacity'=>100
);
$textDefault = array(
'text'=>htmlspecialchars_decode($nickname,ENT_QUOTES),
'fontSize'=>16, //字号
'fontColor'=>'0,0,0', //字体颜色
'angle'=>0,
'fontPath'=> SOURCE_PATH . '/Public/You/font/msyh.ttc',
);
$background = SOURCE_PATH . '/Public/You/img/yaoqing.jpg';//海报最底层得背景
//生成二维码//
$value = C('YOU_OFFICIAL_WEBSITE').'You/Public/register.html?invite_code='.($user_id+C('INVITE_CODE_BASE_NUM'));
$errorCorrectionLevel = 'H';//容错级别
$matrixPointSize = 10;//生成图片大小
ob_clean();
vendor('Qrcode.phpqrcode');
$QR = \QRcode::returnImage($value, $errorCorrectionLevel, $matrixPointSize, 2);
if(0 != $QR['errNo']){
exit();
} else {
$QR = $QR['result'];
}
$logo = VENDOR_PATH .'Qrcode/ewm_tx.png';//准备好的logo图片
if ($logo !== FALSE) {
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//二维码图片宽度
$QR_height = imagesy($QR);//二维码图片高度
$logo_width = imagesx($logo);//logo图片宽度
$logo_height = imagesy($logo);//logo图片高度
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新组合图片并调整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
//背景方法
$backgroundInfo = getimagesize($background);
$ext = image_type_to_extension($backgroundInfo[2], false);
$backgroundFun = 'imagecreatefrom'.$ext;
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); //背景宽度
$backgroundHeight = imagesy($background); //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
//建立画板 ,缩放图片至指定尺寸
$canvas = imagecreatetruecolor($imageDefault['width'], $imageDefault['height']);
imagefill($canvas, 0, 0, $color);
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
imagecopyresampled($canvas, $QR, 0, 0, 0, 0, $imageDefault['width'], $imageDefault['height'],$QR_width,$QR_height);
$imageDefault['left'] = $imageDefault['left']<0?$backgroundWidth- abs($imageDefault['left']) - $imageDefault['width']:$imageDefault['left'];
$imageDefault['top'] = $imageDefault['top']<0?$backgroundHeight- abs($imageDefault['top']) - $imageDefault['height']:$imageDefault['top'];
//放置图像
imagecopymerge($imageRes,$canvas, $imageDefault['left'],$imageDefault['top'],$imageDefault['right'],$imageDefault['bottom'],$imageDefault['width'],$imageDefault['height'],$imageDefault['opacity']);//左,上,右,下,宽度,高度,透明度
if(!empty($wx_headimg) && file_exists(SOURCE_PATH.$wx_headimg)){
//处理头像
$headimg = imagecreatefromstring(file_get_contents(SOURCE_PATH.$wx_headimg));
$width = imagesx($headimg);
$height = imagesy($headimg);
$canvas = imagecreatetruecolor($width,$height);
imagefill($canvas, 0, 0, $color);
imagecopyresampled($canvas, $headimg, 0, 0, 0, 0, $width, $height,$width,$height);
imagecopymerge($imageRes,$canvas, 550,890,0,0,$width,$height,100);//左,上,右,下,宽度,高度,透明度
//处理昵称
list($R,$G,$B) = explode(',', $textDefault['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
if(mb_strlen($textDefault['text']) > 10){
$tmp_text = mb_substr($textDefault['text'], 0, 10, 'utf-8').'...';
} else {
$tmp_text = $textDefault['text'];
}
$box = imagettfbbox($textDefault['fontSize'], 0, $textDefault['fontPath'], $tmp_text);
$text_height = abs($box[1] - $box[7]);
$text_width = abs($box[0] - $box[2]);
imagettftext($imageRes,$textDefault['fontSize'],$textDefault['angle'],550+($width/2)-($text_width/2),$height+890+$text_height+5,$fontColor,$textDefault['fontPath'],$tmp_text);
}
//生成图片
if(!empty($filename)){
$res = imagejpeg ($imageRes,$filename,90);
//保存到本地
imagedestroy($imageRes);
}else{
imagejpeg ($imageRes);
//在浏览器上显示
imagedestroy($imageRes);
}
}