「水坑」关于 Z-BlogPHP 1.7 缩略图的一些记录

吐槽/反馈/建议:我的咸鱼心  爱发电-@wdssmq  FeedsPub-wdssmq

Z-BlogPHP 1.7 将会提供一个Thumb基础类,本身只实现从「原图」到「缩略图」的转换和保存,简单说就是自动帮你变小。

生成路径为zb_users/cache/thumbs/

主题内的使用示例

function ActivePlugin_acgME()
{
  Add_Filter_Plugin('Filter_Plugin_ViewList_Template', 'acgME_Main');
  Add_Filter_Plugin('Filter_Plugin_Post_Thumbs', 'acgME_Thumbs');
  Add_Filter_Plugin('Filter_Plugin_Zbp_BuildTemplate', 'acgME_BuidTemp');
}
function acgME_Main(&$template)
{
  global $zbp;
  // 遍历文章设置图片
  if ($articles = $template->GetTags('articles')) {
    foreach ($articles as $article) {
      acgME_SetIMG($article);
    }
  }
  // else if ($article = $template->GetTags('article')) {
  //   acgME_SetIMG($article);
  // }
}
// 接口函数
function acgME_Thumbs($article, &$all_images, $width, $height, $count, $clip)
{
  $rndNum = $article->ID % 19 + 1;
  $rndImg = acgME_Path("v-noimg", "host") . $rndNum . ".jpg";
  // $all_images为文章正文内的图片,有可能为空,所以追加一张作为默认;
  $all_images[] = $rndImg;
  Thumb::changeDefaultImg($rndImg); // 有图,但是缩略失败时用这个;
}
// 通过封装函数赋值给属性用于调用
function acgME_SetIMG(&$article)
{
  if (!isset($article->Img_640x360)) {
    $article->Img_640x360 = $article->Thumbs(640, 360, 1, false)[0];
    // ↑此处获取值无法保存,保存请用Metas
  }
}
// 用于幻灯片
function acgME_BuidTemp(&$templates)
{
  global $zbp;
  // 幻灯片
  $templates['n-slide'] = "";
  if (!$zbp->template->HasTemplate("m-slide")) {
    return;
  }
  $uFile = acgME_Path("u-slide");
  if (!is_file($uFile)) {
    return;
  }
  // 核心部分
  $slides = json_decode(file_get_contents($uFile));
  foreach ($slides as $key => &$item) {
    $item->Img_142x80 = Thumb::Thumbs([$item->Img], 142, 80, 1, false)[0];
    $item->Img_647x404 = Thumb::Thumbs([$item->Img], 647, 404, 1, false)[0];
  }
  $zbp->template->SetTags('slides', $slides);
  $templates['n-slide'] = $zbp->template->Output("m-slide");
  $script = $zbp->host . 'zb_users/theme/acgME/script/swiper-3.4.2.jquery.min.js';
  $templates['n-slide'] .= '{php}$footer .= \'<script src="' . script .'"></script>\'{/php}';
  $script = $zbp->host . 'zb_users/theme/acgME/script/swiper-act.js';
  $templates['n-slide'] .= '{php}$footer .= \'<script src="' . $script . '"></script>\'{/php}';
}

模板内调用:

<div class="post-img"><img src="{$article.Img_640x360}" alt="{$article.Title}"></div>

关于图片裁切部分的代码笔记

  public function handle()
  {
    if ($this->shouldClip) {
      // 原图:宽/高
      $src_scale = ($this->srcWidth / $this->srcHeight);
      // 新图:宽/高
      $dst_scale = ($this->dstWidth / $this->dstHeight);

      // 高度:原/新
      $h_scale = ($this->srcHeight / $this->dstHeight);
      // 宽度:原/新
      $w_scale = ($this->srcWidth / $this->dstWidth);

      // 原图需要裁切至的目标宽度(横向)
      $w_des = ($this->dstWidth * $h_scale);
      // 原图需要裁切至的目标高度(纵向)
      $h_des = ($this->dstHeight * $w_scale);

      // 新旧比例变化判断裁切方向
      if ($src_scale >= $dst_scale) {
        // 24:15 // 原图 8:5
        // 12:10 // 目标 6:5
        // x:15 = 12:10
        // x = 18 = 12*(15/10) = 15*(12/10) // $w_des
        // 原图高度不变,宽度裁切至 $w_des
        $dst_widthx = (($this->srcWidth - $w_des) / 2); // ((24-18)/2)
        $this->clip($dst_widthx, 0, $w_des, $this->srcHeight);
        // 先将原图裁切至 18:15,再缩放至 12:10
        $this->zoom($this->dstWidth, $this->dstHeight);
      } else {
        // 24:15 // 原图 8:5
        // 40:20 // 目标 10:5
        // 24:y = 40:20
        // y = 12 = 20*(24/40) = 24/(40/20) // $h_des
        // 原图宽度不变,高度裁切至 $h_des
        $dst_heighty = (($this->srcHeight - $h_des) / 2); // ((15-12)/2)
        $this->clip(0, $dst_heighty, $this->srcWidth, $h_des);
        // 理论上,当目标尺寸大于原图尺寸时,这里可以选择返回 24:12 的图片,即上一步裁切好的尺寸
        $this->zoom($this->dstWidth, $this->dstHeight); // 这里实际会返回 24:15
      }
    } else {
      $this->zoom($this->dstWidth);
    }
    $this->save();
    imagedestroy($this->srcRes);
  }

其他

「备忘」访止别人用 iframe 调用你的页面_电脑网络_沉冰浮水:

https://www.wdssmq.com/post/20160730633.html


爱发电

本文标题:《「水坑」关于 Z-BlogPHP 1.7 缩略图的一些记录》作者:沉冰浮水
原文链接:https://www.wdssmq.com/post/20210224481.html
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

干货,牛,貌似,,,我看不懂~~~ 回复
发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

网站分类

搜索

本周旧文

2024 年了,姑且备份下嘟特存档。。

……,一个不知名的小众样式库 + 内联样式混写这种入坑姿势确实很有槽点,但是,「已经开始学」并且能够持续是绝对值得肯定的。。

在贴吧看过很多提问了,就有种错觉:好多人为了提一个问题专门注册了贴吧,问题本身可能得到有效回答,也可能没有(和提问的点及具体姿势有关。。但无论如何,之后就和注销了账号一样没有然后了,好像之后永远不用学相应的东西一样。。

《恶魔娃娃》

- 他们正研究你究竟是真正的大人,还是伪装成大人的小孩

- 我自己都研究很久了

乐高 DC 里,(基本就蝙蝠侠家,,年龄最小的那个无论是谁感觉人设都会变得一样 - -

所以,就感觉和祥林嫂一样,每天都需要向外「签到」自己的情绪感受,然而又并没有什么「需要」我这样的签到……

只要梦里出现学校的概念,形式和反抗好像也都不重要了,,天还没亮,骑自行车去上学,没走多远「想起来」并不需要上学的,下来搬起车原地调头然后推回家。。

《K 星异客》里,「青鸟」真的是等来的,或者说是突然出现然后引发改变的外部因素。。。

梦的最后也是又一次意识到教室里的同学只是幻境中的投影,被困住的只有我;虽然也终于总结出,在梦里从来没有和同学的投影有过冲突和争执,甚至还附和我反驳「管理者」身份被进一步放大并不特指某一个人的老师。。

就和大脑会突然播放某一首歌一样,「讨厌自己」这句话也是自己突然就会播放一次,然后频率更高。。

学校,学校,学校,总是梦到学校,,就好像灵魂被诅咒禁固一样。。

爱发电支持者

最新留言

友情链接

  • 订阅本站的 RSS 2.0 新闻聚合
召唤伊斯特瓦尔