nathan-degraaf/wp-glide

PHP Glide的包装器,通过Composer使用

2.2.0 2023-01-06 19:00 UTC

This package is auto-updated.

Last update: 2024-09-06 22:38:54 UTC


README

PHP Glide/composer的包装器。适用于php@7.2及以上和php@8.0及以上。

基于已不再维护的Aginevs wp-glide 包装器构建。

安装

composer require nathan-degraaf/wp-glide

使用方法

一般配置可以在您主题的function.php文件中进行,尽管我建议为您预设创建一个专门的glide文件。

创建实例

这是一个单例实例,所以您将在应用程序的任何地方获得相同的对象。

$wpGlide = wp_glide();

服务器配置

您应该至少在应用程序中配置一次WpGlide。init方法可以接受四个参数,它们都是可选的。

$wpGlide = wp_glide()->init([
    // Glide server config. See: http://glide.thephpleague.com/2.0/config/setup/
  [
    // Image driver
    'driver'     => 'imagick',
    // Watermarks path
    'watermarks' => new \League\Flysystem\Filesystem(new \League\Flysystem\Adapter\Local(get_template_directory() . '/assets/img')),
  ],
  
  // Base path. By default set to 'img/' and the final URL will look like so: http://example.com/BASE-PATH/SIZE-SLUG/image.jpg.
  'img/',
  
  // Path to WordPress upload directory. If not set the default upload directory will be used.
  'upload_path',
  
  // Cache path. If not set the cache will be placed in cache directory at the root of the default upload path.
  'cache_path'
]);

注册图像大小

您应该像这样注册将由Glide处理的图像大小

$wpGlide->addSize('size_name', [
    'w'  => 1400,
    'q'  => 80,
    'fm' => 'webp',

    'mark'      => 'watermark.png',
    'markw'     => 1000,
    'markh'     => 1000,
    'markalpha' => 55,
    'markfit'   => 'fill',
    'markpos'   => 'center',

])->addSize('size_name_512', [
    'w'  => 512,
    'q'  => 80,
    'fm' => 'webp',

])->addSize('16x9', [
    'w'   => 16 * 10 * 2,
    'h'   => 9 * 10 * 2,
    'fit' => 'crop',
    'q'   => 80,
    'fm'  => 'webp',
]);

在模板中的使用

<!-- Get Glide image URL by it's original URL -->
<img src="<?php echo wp_glide_image('https://yourimage.com', 'size_name'); ?>" />

检索base64编码版本

<!-- Get Glide image URL by it's original URL -->
<img src="<?php echo wp_glide_base64('http://example.com/image.jpg', 'w128'); ?>" />

许可

除非另有说明,此存储库中的所有内容均受MIT许可证保护。

MIT © 2022 Nathan deGraaf