首先说说下面这两个文件
phpqrcode.php
qrcode.php
这两个文件必须要用到 具体什么用处你不需要知道
下方有集成包 大家想加到其他主题的可以下载看研究
步骤1
在你的模板创建个php文件例如我的haibao.php
创建好后你想放在那放在那 要记住路劲 需要用到
<div class="dialog_overlay"></div>
<div class="bigger-share">
<div class="row-share">
<div class="img-share">
<img src="<?php echo poster_share($logid, Url::log($logid), img_url($thumbs,$logid,$log_content), $log_title, $date, $blogname, $bloginfo);?>" alt="<?php echo $log_title; ?>">
<div class="action-haibao">
<a href="<?php echo poster_share($logid, Url::log($logid), img_url($thumbs,$logid,$log_content), $log_title, $date, $blogname, $bloginfo);?>" download="<?php echo $log_title; ?>"><i class="fa fa-share-alt"></i></a>
</div>
</div>
<div class="share-item">
<h3>分享文本海报</h3>
<div class="button">
<a href="javascript:;" onclick="shareToWeibo('<?php echo Url::log($logid);?>','<?php echo $log_title;?>','<?php echo Url::log($logid);?>','')" title="分享到微博" class="btn btn-danger"><i class="fa fa-weibo"></i> 新浪微博</a>
</div>
<div class="button">
<a href="javascript:;" onclick="shareToQzone('<?php echo Url::log($logid);?>','<?php echo $log_title;?>','<?php echo Url::log($logid);?>')" title="分享到QQ好友" class="btn btn-info"> <i class="fa fa-qq"></i> QQ好友</a>
</div>
<div class="button">
<a href="javascript:;" onclick="shareToQzone('<?php echo Url::log($logid);?>','<?php echo $log_title;?>','<?php echo Url::log($logid);?>')" title="分享到QQ空间" class="btn btn-warning"> <i class="fa fa-qzone"></i> QQ空间</a>
</div>
<div class="button">
<a href="<?php echo poster_share($logid, Url::log($logid), img_url($thumbs,$logid,$log_content), $log_title, $date, $blogname, $bloginfo);?>" download="<?php echo $log_title; ?>" class="btn btn-primary">
<i class="fa fa-cloud-download"></i> 下载海报</a>
</div> </div>
<span class="share-close">
<i class="fa fa-close"></i>
</span>
<div class="text-weixin">
<p>长按图片转发给朋友</p>
</div>
</div>
</div>
创建好后开始第二步骤
步骤2
打开你的文章页 一般都是echo_log.php引入即可
引入代码:
<?php include View::getView('inc/haibao');?>
打开echo_log.php
放入加载点
<a class="share-haibao"><i class="fa fa-share-alt"></i> 生成海报</a>
打开heeder.php
载入
require_once View::getView('functions');
打开footer.php
放在任意地方 即可
<?php include View::getView('inc/haibao');?>
放入js fee模板无需放入请忽略这步
$('.share-haibao').click(function() {
$('.bigger-share').addClass('haibao-on');
$('.dialog_overlay').show();
$('.dialog_overlay').click(function() {
$('.bigger-share').removeClass('haibao-on');
$('.dialog_overlay').hide();
});
});
$('.share-close').click(function() {
$('.bigger-share').removeClass('haibao-on');
$('.dialog_overlay').hide();
});
步骤3
新建一个functions.php
放到模板目录里
<?php
/**
* 局部函数库
*/
if(!defined('EMLOG_ROOT')) {exit('error!');}
//获取图片
function img_url($thumburl,$gid,$content,$open=false){
if($thumburl){
return $thumburl;
}elseif(img_zw($content)){
return img_zw($content);
}elseif(img_fj($gid) && $open){
return img_fj($gid);
}else{
return TEMPLATE_URL.'images/post.jpg';
}
}
//抓取远程连接内容
function myCurl($url, $ip = '114.114.114.114'){
$ch = curl_init(); // Curl 初始化
$timeout = 30; // 超时时间:30s
$ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'; // 伪造抓取 UA
curl_setopt($ch, CURLOPT_URL, $url); // 设置 Curl 目标
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Curl 请求有返回的值
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置抓取超时时间
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 跟踪重定向
curl_setopt($ch, CURLOPT_ENCODING, ""); // 设置编码
curl_setopt($ch, CURLOPT_REFERER, $url); // 伪造来源网址
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip)); //伪造IP
curl_setopt($ch, CURLOPT_USERAGENT, $ua); // 伪造ua
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // 取消gzip压缩
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch); // 结束 Curl
return $content; // 函数返回内容
}
//海报图生成
function createSharePng($gData,$fileName){
//创建画布
$width = 600;
$height = 800;
$im = imagecreatetruecolor($width, $height);
//填充画布背景色
$color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $color);
//字体文件
$font_file_1 = EMLOG_ROOT."/content/templates/fee/static/hanyixizhongyuan.ttf";
$font_file_2 = EMLOG_ROOT."/content/templates/fee/static/Montserrat-Regular.ttf";
//设定字体的颜色
$color_0 = ImageColorAllocate ($im, 0, 0, 0);
$color_255 = ImageColorAllocate ($im, 255, 255, 255);
$color_153 = imagecolorallocate($im, 153, 153, 153);
$color_200 = imagecolorallocate($im, 200, 200, 200);
//图片
$source_path = myCurl($gData["pic"]);
$head_img = imagecreatefromstring($source_path);
$target_height = 480;
$target_width = 600;
$source_info = getimagesizefromstring($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_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;
}
// 裁剪
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
imagecopy($cropped_image, $head_img, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
imagecopyresampled($im, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
imagedestroy($cropped_image);
//时间
$day = date('d', $gData["date"]);
$day_width = imagettfbbox(70, 0, $font_file_2, $day);
$day_width = abs($day_width[2] - $day_width[0]);
$year = date('Y/m', $gData["date"]);
$year_width = imagettfbbox(22, 0, $font_file_2, $year);
$year_width = abs($year_width[2] - $year_width[0]);
$day_left = ($year_width - $day_width) / 2;
// 13
imagettftext($im, 70, 0, 50 + $day_left, 390, $color_255, $font_file_2, $day);
//————————
imageline($im, 50, 405, 50 + $year_width, 405, $color_255);
imageline($im, 50, 406, 50 + $year_width, 406, $color_255);
// 2019/02
imagettftext($im, 22, 0, 50, 440, $color_255, $font_file_2, $year);
//标题
$str = $gData['title'];
if(mb_strlen($str)>16){
$str = mb_substr($str,0,15,"UTF8")."...";
}
imagettftext($im, 24,0, 40, 550, $color_0 ,$font_file_1, $str);
//
imagettftext($im, 15,0, 40, 610, $color_153 ,$font_file_1, $gData["author"]);
//-------------------------------------
$style = array($color_200,$color_200,$color_200,$color_200,$color_200,$color_200,$color_255,$color_255,$color_255,$color_255,$color_255,$color_255);
imagesetstyle($im, $style);
imageline($im, 0, 650, 600, 650, IMG_COLOR_STYLED);
//
imagettftext($im, 18,0, 40, 715, $color_0 ,$font_file_1, $gData["blogname"]);
imagettftext($im, 14,0, 40, 755, $color_153 ,$font_file_1, $gData["bloginfo"]);
//二维码
$qrcode_str = file_get_contents($gData["code"]);
$qrcode_size = getimagesizefromstring($qrcode_str);
$qrcode_img = imagecreatefromstring($qrcode_str);
imagecopyresized($im, $qrcode_img, 460, 670, 0, 0, 110, 110, $qrcode_size[0], $qrcode_size[1]);
//ob_clean();
//输出图片
imagepng ($im,$fileName);
//释放空间
imagedestroy($im);
imagedestroy($head_img);
//return true;
}
//poster
function poster_share($id, $url, $pic, $title, $date, $blogname, $bloginfo){
//图片上传目录
$upload_dir = 'content/uploadfile/fee_file/poster/';
//图片名称
$filename = 'Poster-' . $id . '.png';
//图片地址
$file = EMLOG_ROOT . '/' . $upload_dir . $filename;
//返回浏览连接
$src = BLOG_URL . $upload_dir . $filename;
//判断图片是否存在或已超过15天,创建缓存文件
if( !is_file($file) || ( time() - filemtime($file) ) > 1296000 ){
global $CACHE;
$log_cache_sort = $CACHE->readCache('logsort');
$sort = $log_cache_sort[$id]['name'] ? $log_cache_sort[$id]['name'] : '文章';
global $Tconfig;
$author = '作者:'.$Tconfig['username'].' 发布在「'.$sort.'」';
//二维码
$code = TEMPLATE_URL . 'share/qrcode.php?data=' . $url;
//数据
$gData = [
'pic' => $pic,
'code' => $code,
'title' => $title,
'date' => $date,
'author' => $author,
'blogname' => $blogname,
'bloginfo' => $bloginfo
];
createSharePng($gData,$file);
}
return $src;
}
步骤4
创建图片上传路劲content/uploadfile/fee_file/poster/
打开content/uploadfile/
创建fee_file
然后在打开此文件在创建poster
即可
结尾
方法既然已经发出来了但是可能不怎么完整 一时间想不出来还有啥没写上去 如果有缺少的下方留言我在写上去。
请登录后发表评论
注册
请登录后查看评论内容