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 で呼び出すのを防止する_电脑网络_沉冰浮水: