Emlog模板添加每日60秒读世界独立页面 - 陌路人博客

Emlog模板添加每日60秒读世界独立页面-陌路人博客-第1张图片陌路人

陌路人博客(blog.imlr.cn)
用心传递快乐,初心不变。

Emlog模板添加每日60秒读世界独立页面

Emlog模板添加每日60秒读世界独立页面-陌路人博客-第2张图片
最近看各个站点都有的每日六十秒读世界的文章或者页面,大多数实现方式大致两种:一种是emlog中相关的插件或者采集,此方法虽然配置简单、方便,但是这个采集文章内容具有很强的时效性,需要每日定时访问触发采集才能发布文章;
一种是直接引用别人的六十秒读世界的api,此方法最大的优点是不用设置采集和发布大量文章,但是形式是仅仅的一张图片,这就过于简单的了,那有还有比前两种更好点又不太简单的实现方式?,答案有是有的,其实就是将接口写在主题中。
实现方式
1.在模板主题的module.PHP文件添加以下函数
<?php
//每日60秒独立页面(laputa.top)
function luyu_sixtys(){ 
$date = file_get_contents("https://www.zhihu.com/api/v4/columns/c_1261258401923026944/items");
$date = json_decode($date);
$content = $date->data[0]->content;
$content = preg_replace('/(<a.*?>[\s\S]*?<\/a>)/','',$content);
$pattern ='<img.*?src="(.*?)">';
preg_match($pattern,$content,$matches);
$src_path = $matches[1];
$src = imagecreatefromstring(file_get_contents($src_path));
$info = getimagesize($src_path);
$x = 0;
$y = 0;
$width = 720;
$height = 350;
$final_width = 720;
$final_height = round($final_width * $height / $width);
$new_image = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
$ext = pathinfo($src_path, PATHINFO_EXTENSION);
$rand_name = date("Ymd") . "." . $ext;
$url= BLOG_URL;
if (!file_exists("content/uploadfile/60s")){
    mkdir ("content/uploadfile/60s",0777,true);
}
imagejpeg($new_image,"content/uploadfile/60s/".$rand_name);
imagedestroy($src);
imagedestroy($new_image);
$content = strip_tags($content,'<p>');
$content = '<img class="" src="'.$url.'/content/uploadfile/60s/'.$rand_name.'" />'.$content;
  return $content;
};
?>
2.在自己使用的模板创建独立页面,以60s.php为例,根据各自的主题在文章内容部分添加调用显示。
<?php echo luyu_sixtys()?>

×

感谢您的支持,我们会一直保持!

Emlog模板添加每日60秒读世界独立页面-陌路人博客- 第4张图片
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:陌路人博客 » Emlog模板添加每日60秒读世界独立页面
标签: 教程 emlog 代码

发表评论

表情

网友评论(0)