PHP code example of wiseadmin / wise-image

1. Go to this page and download the library: Download wiseadmin/wise-image library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wiseadmin / wise-image example snippets


return [
    // 驱动选择:gd / imagick
    'driver' => 'gd',

    // 默认输出质量(1-100)
    'default_quality' => 85,

    // 是否自动转为 WebP
    'auto_webp' => true,

    // 安全防护
    'security' => [
        // IP 黑名单
        'ip_blacklist' => [
            'enabled' => false,
            'ips'     => [],
        ],
        // Referer 检查
        'referer' => [
            'enabled'      => false,
            'allow_empty'  => true,
            'whitelist'    => [],
            'blacklist'    => [],
        ],
        // 签名防盗链
        'signature' => [
            'enabled'  => false,
            'secret'   => '',
            'timeout'  => 3600,
        ],
        // 源图保护
        'origin_protect' => [
            'enabled' => false,
        ],
        // 最大尺寸限制
        'max_width'  => 4096,
        'max_height' => 4096,
        // 允许的格式
        'allowed_formats' => ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'],
    ],

    // 缓存配置
    'cache' => [
        'driver' => 'file',               // file / redis
        'ttl'    => 2592000,              // 30天
        'path'   => 'runtime/wise-image/cache',
        'prefix' => 'wise_image:',
    ],

    // 预定义样式
    'styles' => [
        'thumb'   => 'resize,w_200,h_200/quality,q_80',
        'small'   => 'resize,w_400,h_400',
        'medium'  => 'resize,w_800,h_800',
        'watermark' => 'watermark,path_logo.png,pos_southeast',
    ],

    // 字体文件路径(水印文字用)
    'font_path' => null,

    // 内置路由前缀
    'route_prefix' => '/image',
];

use wise\image\facade\Image;

// 打开图片 → 缩放 → 裁剪 → 保存
Image::open('/path/to/photo.jpg')
    ->resize(800, 600)
    ->crop(400, 400, 100, 50)
    ->quality(85)
    ->save('/path/to/output.jpg');

// 创建空白画布 → 生成缩略图
Image::make(800, 600)
    ->open('/path/to/photo.jpg')
    ->resize(800, 600, 'fill')
    ->save('/path/to/thumb.jpg');

Image::open('/path/to/photo.jpg')
    ->resize(1200, 800)           // 缩放
    ->watermark('/path/to/logo.png', 'southeast', 50)  // 右下角水印,透明度50
    ->blur(3)                      // 模糊半径3
    ->brightness(10)               // 亮度+10
    ->contrast(5)                  // 对比度+5
    ->rotate(90)                   // 旋转90度
    ->format('webp')               // 转换为 WebP
    ->quality(80)                  // 质量80
    ->save('/path/to/result.webp');

// 获取图片信息
$info = Image::open('/path/to/photo.jpg')->getInfo();
// ['width' => 1920, 'height' => 1080, 'mime' => 'image/jpeg', ...]

// 解析 URL 参数并执行操作链
Image::open('/path/to/photo.jpg')
    ->parseUrlParams('image/resize,w_400,h_300/quality,q_80/rotate,90')
    ->save('/path/to/result.jpg');

// 在配置中定义样式
// 'styles' => ['thumb' => 'resize,w_200,h_200/quality,q_80']

// 直接使用
Image::open('/path/to/photo.jpg')
    ->style('thumb')
    ->save('/path/to/thumb.jpg');

// 在控制器中直接输出处理后的图片
public function preview()
{
    Image::open('/path/to/photo.jpg')
        ->resize(600, 400)
        ->format('webp')
        ->response();  // 输出 Content-Type: image/webp
}

use wise\image\ImageManager;

class PhotoController
{
    public function process(ImageManager $image)
    {
        $image->open('/path/to/photo.jpg')
            ->resize(400, 300)
            ->save('/path/to/thumb.jpg');

        return json(['ok' => true]);
    }
}
bash
php think wise:image:publish
bash
# 发布配置
php think wise:image:publish

# 强制覆盖已有配置
php think wise:image:publish --force