需求:生成海报(需要有二维码,相关文字)
A , B , C, D 这四大个点的内容需要是变化的
D:一般是设计的二维码
主方法:
//生成邀请卡
public function index()
{
//接收参数
$params=$this->input->post();
//查询相关数据
$result = '';
//时间
$time = "海报时间";
//获取http协议
if(isset($_SERVER['HTTP_X_CLIENT_SCHEME'])){
$scheme = $_SERVER['HTTP_X_CLIENT_SCHEME'] . '://';
}elseif(isset($_SERVER['REQUEST_SCHEME'])){
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
}else{
$scheme = 'http://';
}
//域名
$domain = $_SERVER['SERVER_NAME'] ;
//二维码地址
$url="";
//获取水印生成图片
$codeurl= $this->watermark($url,$params['live_id'],'',$params['bg_type']);
//头像地址
$codeparams['headimg'] = '';
//背景图片(底图)
$codeparams['background'] = '';
//时间
$codeparams['time'] = $time;
//图片
$codeparams['sponsor'] = '';
//标题
$codeparams['title'] = '';
//表示id
$codeparams['live_id'] = ;
//缩略
$this->pic($codeparams);
//合成图片
$pic = $this->compicture($codeurl,$codeparams,'show',$params['bg_type']);
//返回参数
if($pic){
//返回值(url数据,展示输出)
//获取http协议
if(isset($_SERVER['HTTP_X_CLIENT_SCHEME'])){
$scheme = $_SERVER['HTTP_X_CLIENT_SCHEME'] . '://';
}elseif(isset($_SERVER['REQUEST_SCHEME'])){
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
}else{
$scheme = 'http://';
}
//域名
$domain = $_SERVER['SERVER_NAME'] ;
//获取其他参数
$url = $scheme.$domain.$pic;
$rebackdata['url'] = $url;
ajaxReturn(['status'=>'0','msg'=>'成功','data'=>$rebackdata]);
}else{
ajaxReturn(['status'=>'1','msg'=>'失败','data'=>'']);
}
}
/***********************************公共方法开始*********************************/
//生成缩略图
public function pic($params){
//创建文件夹
$path_dir = "";
if (!is_dir($path_dir)) {
mkdir($path_dir, 0777, true);
}
//获取图片,生成缩略图
$sponsorold = imagecreatefromstring(file_get_contents(ltrim($params['sponsor'],'/')));
$sponsorxWidthold = imagesx($sponsorold);
$sponsoryWidthold = imagesy($sponsorold);
$new_width = $sponsorxWidthold*0.3;
$new_height =$sponsoryWidthold*0.3;
$this->imagepress($params['sponsor'], $new_width, $new_height,$params['live_id']);
}
//生成二维码
function watermark($url, $live_id,$show,$type){
//地址
$bg_img_url = str_replace('\\', '/', strstr(__DIR__, 'app', true));
//图片获取
$bg_img = $bg_img_url . '相对地址';
//背景图片获取到画布
$img = imagecreatefromstring(file_get_contents($bg_img));
//获取画布宽度(计算文字横向距离)
$imgWidth = imagesx($img);
$size = intval($imgWidth * 0.006);
//清除缓存区
ob_clean();
//应用三方
$this->load->library('Qrcode');
//二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = $size;//生成图片大小
//生成二维码图片
$path_dir = "保存地址";
if (!is_dir($path_dir)) {
mkdir($path_dir, 0777, true);
}
//合成文件路径
if($show){
$filename = $path_dir.'/'.$live_id.'show'.'.png';
}else{
$filename = $path_dir.'/'.$live_id.'.png';
}
//删除旧文件
if(file_exists($filename)){
unlink($filename);
}
//重新生成文件
if(!file_exists($filename))
{
QRcode::png($url,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; //已经生成的原始二维码图片文件
$QR = imagecreatefromstring(file_get_contents($QR));
}
//返回存储地址
if($show){
$rebackurl = '/存储相对路径/'.$live_id.'show'.'.png';
}else{
$rebackurl = '/存储相对路径/'.$live_id.'.png';
}
return $rebackurl;
}
//合成图片
function compicture($codeurl,$params,$show,$bg_type=1){
//背景图片(根据$bg_type的组合)
$bg_img_url = str_replace('\\', '/', strstr(__DIR__, 'app', true));
$bg_img = $bg_img_url . '相对地址';
//字体路径
$font_xk = $bg_img_url . 'static/font/msyhdb.TTF';
//背景图片获取到画布
$img = imagecreatefromstring(file_get_contents($bg_img));
if($bg_type==6){
$blacktitle = 45;//字体颜色 RGB 标题
$black = 25;//字体颜色 RGB
}else{
$blacktitle = imagecolorallocate($img, 17,115,215);//字体颜色 RGB 标题
$black = imagecolorallocate($img, 0,0,0);//字体颜色 RGB
}
//获取画布宽度(计算文字横向距离)
$imgWidth = imagesx($img);
//获取画布高度(计算文字竖向高度)
$imgheight = imagesy($img);
//字体大小(普通)
$pubfontsize = $imgheight * 0.022;
//计算间距
$spacingsix = $imgheight * 0.0102;
//背景
$background = imagecreatefromstring(file_get_contents($params['background']));
$backgroundWidth = imagesx($background);
$backgroundx = ($imgWidth-$backgroundWidth)/1.9;
$backgroundy = $imgheight*0.1;
imagecopymerge($img, $background, $backgroundx, $backgroundy, 0,0, imagesx($background), imagesy($background), 100);
//logo
$headimg = imagecreatefromstring(file_get_contents($params['headimg']));
$headimgWidth = imagesx($headimg);
$headimgx = ($imgWidth-$headimgWidth)/1.9;
$headimgy = $imgheight*0.1;
imagecopymerge($img, $headimg, $headimgx, $headimgy, 0,0, imagesx($headimg), imagesy($headimg), 100);
//渲染项目图片
$sponsornew = 'upload/invitation_ugc/kv/7943.png';
$sponsor = imagecreatefromstring(file_get_contents($sponsornew));
$sponsorWidth = imagesx($sponsor);
$sponsorx = ($imgWidth-$sponsorWidth)/2;
$sponsory = $imgheight*0.35;
imagecopymerge($img, $sponsor, $sponsorx, $sponsory, 0,0, imagesx($sponsor), imagesy($sponsor), 100);
//二维码合并
$image_2 = imagecreatefromstring(file_get_contents($codeurl));
$img2Width = imagesx($image_2);
$img2x = ($imgWidth-$img2Width)/1.96;
$img2y = $imgheight*0.63;
imagecopymerge($img, $image_2, $img2x, $img2y, 0,0, imagesx($image_2), imagesy($image_2), 100);
//渲染标题(邀请你一起观看直播)
$title = '邀请你一起观看直播';
//获取文字宽度
$titlex = ($imgWidth-$spacingsix*strlen($title))/1.96;
//获取文字高度
$titley = $imgheight*0.25;
imagefttext($img, $pubfontsize, 0, $titlex, $titley, $blacktitle, $font_xk, $title);
//渲染时间
$time =$params['time'];
//获取文字宽度
$timex = ($imgWidth-$spacingsix*strlen($time))/1.85;
$timey = $imgheight*0.3;
$textsize = $imgheight * 0.015;
imagefttext($img, $textsize, 0, $timex, $timey, $black, $font_xk, $time);
//渲染标题(需要分段:如果字符过长需要折中渲染)
$title = $params['title'];
//切分数据
$chunkdata = $this->chunkstr($title,'10','12');
//第一行数据
if($chunkdata[0]){
//获取文字高度
$titley = $imgheight*0.56;
imagefttext($img, $pubfontsize, 0, $sponsorx, $titley, $black, $font_xk, $chunkdata[0]);
}
//第二行数据
if($chunkdata[1]){
$titley = $imgheight*0.6;
imagefttext($img, $pubfontsize, 0, $sponsorx, $titley, $black, $font_xk, $chunkdata[1]);
}
//渲染标题(长按识别二维码参加直播)
$address ='长按识别二维码参加直播';
//获取文字宽度
$addressx = $imgWidth*0.33;
$addressy = $imgheight*0.8;
$addresssize = $imgheight * 0.013;
imagefttext($img, $addresssize, 0, $addressx, $addressy, $black, $font_xk, $address);
//地址(图片合成后的保存地址)
$path = $bg_img_url . '海报生成后保存地址';
//创建文件
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
//标志(正式区使用live_id)
if($show){
$manage_id = $params['live_id'].'show';
}else{
$manage_id = $params['live_id'];
}
//文件加上水印
$upload = $path . '/' . $manage_id . '.jpg';
//合成图片并保存
imagejpeg($img, $upload);
imagedestroy($img);
//返回相对地址
$returnurl = '相对路径'.$manage_id.'.jpg';
return $returnurl;
}
//压缩图片
public function imagepress($filepath, $new_width, $new_height,$live_id)
{
//获取http协议
if(isset($_SERVER['HTTP_X_CLIENT_SCHEME'])){
$scheme = $_SERVER['HTTP_X_CLIENT_SCHEME'] . '://';
}elseif(isset($_SERVER['REQUEST_SCHEME'])){
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
}else{
$scheme = 'http://';
}
//域名
$domain = $_SERVER['SERVER_NAME'] ;
//获取其他参数
$filepath = $scheme.$domain.$filepath;
$source_info = getimagesize($filepath);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $new_height / $new_width;
// 源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($filepath);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($filepath);
break;
case 'image/png':
$source_image = imagecreatefrompng($filepath);
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($new_width, $new_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $new_width, $new_height, $cropped_width, $cropped_height);
header('Content-Type: image/jpeg');
imagejpeg($target_image,'./upload/invitation_ugc/kv/'.$live_id.'.png');
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
//切割长度(切割指定数据并且不能出现切割错误)
public function chunkstr($string,$len,$lenmore){
$start = 0;
$strlen = mb_strlen($string);
while ($strlen) {
if(empty($array)){
$array[] = mb_substr($string,$start,$len,"utf8");
$string = mb_substr($string, $len, $strlen,"utf8");
$strlen = mb_strlen($string);
}else{
$array[] = mb_substr($string,$start,$lenmore,"utf8");
$string = mb_substr($string, $lenmore, $strlen,"utf8");
$strlen = mb_strlen($string);
}
}
return $array;
}