PHP Regex 初练习:将WordPress文章内缩略图加入CI处理

代码

<?php

/**
 * Plugin Name: CI-Regex
 * Description: 更改图片后缀为腾讯云数据万象处理后缀
 * Version: 1.0.4
 * Author: AHdark
 * Author URI: https://www.ahdark.blog
 */

add_filter('the_content', 'cos_set_ci_img');
function cos_set_ci_img($content)
{
    $ci = "?imageMogr2/format/webp/interlace/1/quality/85";
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $images);
    if (!empty($images) && isset($images[1])) {
        foreach ($images[1] as $item) {
            if (preg_match("/[a-zA-z]+:\/\/(.*)\/\d+\.(jpeg|jpg|png|webp|gif)$/im", $item)) {
                $content = str_replace($item, $item . $ci, $content);
            }
            if (preg_match("/https?:\/\/([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?)\/(.*)\/\d+-\d{1,4}x\d{1,4}\.(png|jpg|jpeg|webp|gif)(.*)/im", $item)) {
                preg_match("/https?:\/\/([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?)\/(.*)\/(\d+)-(\d+)x(\d+)\.(jpg|png|gif|webp|jpeg)/im", $item, $thumbpic);
                $content = str_replace($item, "https://" . $thumbpic[1] . "/" . $thumbpic[3] . "/" . $thumbpic[4] . "." . $thumbpic[7] . $ci . "/thumbnail/" . $thumbpic[5] . "x" . $thumbpic[6] . "!", $content);
            }
        }
    }
    return $content;
}

add_filter('the_content', 'cos_set_ci_img_srcset');
function cos_set_ci_img_srcset($content)
{
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?srcset=[\'"]?(.*? \\d{1,4}w)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $images);
    if (!empty($images) && isset($images[1])) {
        foreach ($images[1] as $item) {
            preg_match('/([a-zA-z]+:\/\/[^\s]* \d{1,4}w), ([a-zA-z]+:\/\/[^\s]* \d{1,4}w), ([a-zA-z]+:\/\/[^\s]* \d{1,4}w), ([a-zA-z]+:\/\/[^\s]* \d{1,4}w)/im', $item, $image);
            //print_r($image);
            //print("\n---image---\n");
            $repic = [];
            foreach ($image as $num => $thumbpic) {
                if (
                    $num == 0
                ) {
                    continue;
                }
                //print_r($thumbpic);
                //print("\n");
                $thumbpic = explode(' ', $thumbpic);
                preg_match("/https?:\/\/([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?)\/(.*)\/(\d+)-(\d+)x(\d+)\.(jpg|png|gif|webp|jpeg)/im", $thumbpic[0], $regex_pic);
                if (!empty($regex_pic) && isset($regex_pic[1])) {
                    $thumbpic[0] = "https://" . $regex_pic[1] . "/" . $regex_pic[3] . "/" . $regex_pic[4] . "." . $regex_pic[7] . "?imageMogr2/format/webp/interlace/1/quality/90/thumbnail/" . $regex_pic[5] . "x" . $regex_pic[6] . "!";
                    $repic[$num] = $thumbpic[0] . " " . $thumbpic[1];
                }
            }
            $content = str_replace($item, implode(", ", $repic), $content);
            unset($repic);
            unset($num);
            unset($image);
            unset($thumbpic);
        }
    }
    return $content;
}

使用方法

你只需要在/wp-content/plugin/下创建文件:regex-cos.php并将上述内容复制进文件即可

warning 警告
仅当WordPress博客使用COS作为图片存储策略并开启数据万象时,本插件才有效

PHP Regex 初练习:将WordPress文章内缩略图加入CI处理 的发布基于协议 AHdark Blog License。如欲对此文章内容此文章转载、修改或行使任何超出预览和分享性质的行为,请参考此协议。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇