WordPress 图片裁剪插件Aqua-Resizer使用方法

Gavin Wu 2022年04月21日 8:04 Wordpress 4,032 Views

WordPress 图片裁剪插件Aqua-Resizer使用方法|微言心语
aq_resizer可让您实时调整和裁剪通过媒体上传器上传的WordPress图片的大小。它依靠WP的本机功能来调整图像的大小,并检查图像是否已调整大小,以免浪费服务器资源来重新生成图像。aq_resizer的裁切完全是调用Wordpress内置裁图机制,通过添加一个函数来裁切图像,而不是利用底层的PHP来处理图像。不同于Wordpress内置函数add_image_size(),AR的裁切当访问页面模板碰到函数的时候,才开始裁切,不干预新上传的图片。而且,裁切后的图像会直接在文件名后添加宽高数字存储在原图路径,第二次访问直接调用。

优势

调用Wordpress内部机制裁图,不依赖任何第三方,安全,稳定。
缓存图片与Wordpress缩略图格式一致,存储在自己服务器,安全放心。
前端直接输出缩略图的URL,对SEO友好,对CDN友好。
仅对需要的图片裁切需要的尺寸,不会对所有图片都“一刀切”。
通过对各个位置的函数写参数,可以很轻松的控制图片的裁切。

劣势

对于新做的主题首次访问,如果图片量太大,会造成服务器内存爆掉。
修改裁图的函数会新裁切出图片,旧图会占用空间。
换主题的时候,仍然存在多余图片不好清理的问题。
只能裁剪本地图片,不能处理远程图片;

使用方法

一、下载Aqua-Resizer
二、在wordpress里使用Aqua-Resizer

将解压后将PHP文件,放在主题文件夹里,并在主题function.php里添加以下代码

1
require_once('aq_resizer.php');
三、使用方法
1
aq_resize( $url, $width, $height, $crop, $single, $upscale )
$url 图片的地址,必须
$width 需要设置图片的宽度,必须
$height 需要设置图片的高度
$crop 是否剪裁图片
$single 是否返回包含url、高度、宽度的数组,默认true
$upscale 图片本身小的时候,是否放大后才剪裁,默认false
四、使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$attachment_args = array(
    'post_type' => 'attachment',
    'numberposts' => 1,          // 调用图片附件的数量,-1为全部
    'post_status' => null,
    'post_parent' => $post->ID,
    'orderby' => 'menu_order ID'
);
$attachments = get_posts($attachment_args);
if ($attachments) {
    foreach ($attachments as $gall_image) {
        $att_img =  wp_get_attachment_url($gall_image->ID);

        echo '<a class="grouped_elements" rel="group1" href="' . $att_img . '" title="' . $gall_image->post_title . '">';
        echo  '<img src="' . aq_resize($att_img, 240, 100, true) . '" alt=""/>';
        echo '</a>';
    }
}?>

这里会输出文章内的所有图片,并按2400*100尺寸裁剪输出。

进阶使用

目前在网上能搜索到的就只有上面的内容,但与我想要还有很大差距。于是只有自己动手将代码与缩略图合并使用,并通过输入数字控制长宽,经过几次测试最终得到以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
*Wordpress 图片裁剪插件Aqua-Resizer使用方法
*URL:https://wuean.com/wordpress-crop-pictureaq_resizer-php.html
*Author URI: https://wuean.com
*/

function wutheme_thumbnail($width = 160, $height = 100)
{
    global $post;
    if (has_post_thumbnail()) {
        $timthumb_src = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
            echo '<a href="' . get_permalink() . '">';
            echo '<img src="' . aq_resize($timthumb_src, $width,$height, true).'" title="' . get_the_title() . '" alt="' . get_the_title() . '"/>';
            echo '</a>';
    } else {
        $content = $post->post_content;
        preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
        $n = count($strResult[1]);
        if ($n > 0) {
            echo '<a href="' . get_permalink() . '">';
            echo '<img src="' . aq_resize($strResult[1][0] , $width,$height, true) .'" title="' . get_the_title() . '" alt="' . get_the_title() . '"/>';
            echo '</a>';
        } else {

        }
    }
}

在需要缩略图的地方输出

1
<?php wutheme_thumbnail(240, 140); ?>//修改240和140来裁剪图片宽和高

输出结构样式为

1
<a href="#文章标题"><img src="http://xxx.com/xxx-240x140.jpg" title="#文章标题" alt="#文章标题"/></a>

这种方法使用起来更加简单和灵活!
你可以使用我修改好的文件

参考文档

1、https://www.dukeyin.com/2018/08/wordpress-image-resize/
2、https://www.npc.ink/8185.html
3、https://github.com/syamilmj/Aqua-Resizer

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top