AHdark
一个多语种开发蒟蒻
AHdark Blog

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

代码

<?php

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

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作为图片存储策略并开启数据万象时,本插件才有效

赞赏
本文链接:https://ahdark.com/technology/778.shtml
本文采用 CC BY-NC-SA 3.0 Unported 协议进行许可
欢迎加入Q群交流:654022768

AH Dark

文章作者

本博客的运营者、主要开发者、主要作者 深度学习算法工程师,后端工程师,嵌入式软件开发工程师 Azure认证 运维工程师

发表评论

textsms
account_circle
email

AHdark Blog

PHP Regex 初练习:将WordPress文章内缩略图加入CI处理
WordPress+COS+数据万象 通过Regex函数将文章内图片替换为数据万象缩略图 节省存储费用、流量费用,增加数据万象费用(真的不贵)
扫描二维码继续阅读
2021-08-03