承接 x-laravel/spatie-medialibrary-webp-downloader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

x-laravel/spatie-medialibrary-webp-downloader

最新稳定版本:v1.0.0

Composer 安装命令:

composer require x-laravel/spatie-medialibrary-webp-downloader

包简介

Drop-in Downloader for spatie/laravel-medialibrary that converts every fetched image to WebP on the fly.

README 文档

README

Tests PHP Laravel License

Drop-in Downloader replacement for spatie/laravel-medialibrary that converts every remote image into WebP on the fly while it is being downloaded.

Unofficial plugin. Not affiliated with Spatie.

How It Works

  • Implements Spatie's Downloader interface — registers as the media_downloader in config/media-library.php
  • Inherits the SSL & User-Agent behaviour of DefaultDownloader, then re-encodes the temp file as WebP using spatie/image (the same library media-library uses internally)
  • Reads Spatie's existing media-library.image_driver config (gd, imagick, or vips) — no extra wiring required, no extra dependencies pulled in
  • Non-image payloads (PDF, video, archives) and configured skip MIME types pass through untouched

Requirements

  • PHP ^8.3
  • Laravel ^12.0 | ^13.0
  • spatie/laravel-medialibrary ^11.0 | ^12.0
  • spatie/image ^3.0 (already installed transitively by media-library)
  • The PHP extension matching media-library.image_driverext-gd, ext-imagick, or libvips for vips

Installation

composer require x-laravel/spatie-medialibrary-webp-downloader

There is no service provider — wiring happens through Spatie's own config.

Setup

Edit config/media-library.php and swap the downloader binding:

use XLaravel\Spatie\MediaLibrary\WebpDownloader\WebpDownloader;

return [
    // ...
    'media_downloader' => WebpDownloader::class,
    // ...
];

That's it. From now on every addMediaFromUrl() call passes through the WebP converter.

Image Driver

The driver is chosen automatically from Spatie's existing config and forwarded to Spatie\Image\Image::useImageDriver():

// config/media-library.php
'image_driver' => env('IMAGE_DRIVER', 'gd'), // 'gd', 'imagick', or 'vips'

The required PHP extension (ext-gd / ext-imagick) — or libvips for vips — must be loaded.

Tuning

WebpDownloader accepts two optional constructor arguments — all auto-resolved via the container with sensible defaults:

Argument Default Meaning
quality 85 WebP quality (0–100)
skipMimes ['image/svg+xml', 'image/gif'] MIME types that bypass conversion

To override, bind a custom instance in your own service provider:

use XLaravel\Spatie\MediaLibrary\WebpDownloader\WebpDownloader;

$this->app->bind(WebpDownloader::class, fn () => new WebpDownloader(
    quality: 90,
    skipMimes: ['image/svg+xml'],
));

Automatic File Name Correction

WebpDownloaderServiceProvider is auto-discovered. It hooks Media::creating and rewrites the file_name extension to .webp when all of the following hold:

  • The configured media-library.media_downloader is WebpDownloader (or a subclass)
  • The persisted mime_type is image/webp
  • The current file_name does not already end with .webp (case-insensitive)

So obama.jpgobama.webp (basename preserved, extension swapped). Disk uploads of unrelated formats and media added while a different downloader is configured are never touched.

If you want to opt out, remove the provider from config/app.php (or set extra.laravel.dont-discover in your root composer.json) and rename manually with ->usingFileName(...).

Bulk Converting Existing Media

For repositories that already have thousands of JPEGs/PNGs sitting on disk, the package ships an Artisan command that walks the media table and re-encodes them in place:

# Dry run — shows which rows would be converted, touches nothing
php artisan media-library:webp-convert --dry-run

# Convert everything
php artisan media-library:webp-convert

# Scope to a collection or model
php artisan media-library:webp-convert --collection=images
php artisan media-library:webp-convert --model="App\Models\Post"

# Tune chunk size for large tables
php artisan media-library:webp-convert --chunk=500

# Dispatch each row as a queued job (recommended for large libraries)
php artisan media-library:webp-convert --queue
php artisan media-library:webp-convert --queue --queue-connection=redis --queue-name=media

# Incremental migration — only media added after a date
php artisan media-library:webp-convert --since=2026-04-01

# Parallel workers splitting the id range
# Terminal A:
php artisan media-library:webp-convert --id-from=1 --id-to=50000 --queue
# Terminal B:
php artisan media-library:webp-convert --id-from=50001 --id-to=100000 --queue

For each Media row the command:

  1. Skips image/webp, non-image MIME types, and entries in the configured WebpDownloader skipMimes
  2. Reads the file via the row's own disk and the PathGenerator resolved by Spatie (custom path generators are honoured automatically)
  3. Re-encodes to WebP using media-library.image_driver and the quality from the container-bound WebpDownloader instance
  4. Writes the new .webp file, deletes the original, and updates file_name, mime_type, and size on the Media row via saveQuietly() so the auto-rename listener doesn't fire on data we have already corrected

Existing conversions (thumbnails, responsive images) become stale because their source has changed. Run php artisan media-library:regenerate afterwards — the convert command prints a reminder when it finishes.

Testing

# Build first (once per PHP version)
DOCKER_BUILDKIT=0 docker compose --profile php83 build

# Run tests
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up

License

This package is open-sourced software licensed under the MIT license.

x-laravel/spatie-medialibrary-webp-downloader 适用场景与选型建议

x-laravel/spatie-medialibrary-webp-downloader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「image」 「laravel」 「downloader」 「spatie」 「medialibrary」 「Webp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 x-laravel/spatie-medialibrary-webp-downloader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 x-laravel/spatie-medialibrary-webp-downloader 我们能提供哪些服务?
定制开发 / 二次开发

基于 x-laravel/spatie-medialibrary-webp-downloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-10