sabioweb/php-cwebp-package 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

sabioweb/php-cwebp-package

Composer 安装命令:

composer require sabioweb/php-cwebp-package

包简介

A pure PHP package for converting images to WebP format

README 文档

README

A pure PHP package for converting images to WebP format. This package provides a simple and efficient way to convert JPEG, PNG, GIF, and BMP images to WebP format using PHP's GD extension.

Features

  • Convert JPEG, PNG, GIF, and BMP images to WebP
  • Configurable quality settings (0-100)
  • Lossless conversion mode
  • Metadata preservation (when possible)
  • Comprehensive error handling
  • PSR-4 autoloading
  • PHP 8.3+ compatible

Installation

Install via Composer:

composer require sabioweb/php-cwebp-package

Requirements

  • PHP 8.3 or higher
  • GD extension enabled

Quick Start

<?php

use Sabioweb\WebPConverter\SBWebPConverter;
use Sabioweb\WebPConverter\SBConversionOptions;

require 'vendor/autoload.php';

// Create converter instance
$converter = new SBWebPConverter();

// Convert image with default settings
$converter->convert('input.jpg', 'output.webp');

// Convert with custom quality
$options = SBConversionOptions::create()
    ->setQuality(85);

$converter->convert('input.png', 'output.webp', $options);

// Convert with lossless mode
$options = SBConversionOptions::create()
    ->setLossless(true);

$converter->convert('input.png', 'output.webp', $options);

Usage

Basic Conversion

use Sabioweb\WebPConverter\WebPConverter;

$converter = new SBWebPConverter();
$converter->convert('path/to/image.jpg', 'path/to/output.webp');

Advanced Options

use Sabioweb\WebPConverter\SBWebPConverter;
use Sabioweb\WebPConverter\SBConversionOptions;

$converter = new SBWebPConverter();

$options = SBConversionOptions::create()
    ->setQuality(90)
    ->setLossless(false)
    ->preserveMetadata(true);

$converter->convert('input.png', 'output.webp', $options);

Error Handling

use Sabioweb\WebPConverter\SBWebPConverter;
use Sabioweb\WebPConverter\Exceptions\SBInvalidImageException;
use Sabioweb\WebPConverter\Exceptions\SBConversionFailedException;
use Sabioweb\WebPConverter\Exceptions\SBFileNotFoundException;

try {
    $converter = new SBWebPConverter();
    $converter->convert('input.jpg', 'output.webp');
} catch (SBFileNotFoundException $e) {
    echo "File not found: " . $e->getMessage();
} catch (SBInvalidImageException $e) {
    echo "Invalid image: " . $e->getMessage();
} catch (SBConversionFailedException $e) {
    echo "Conversion failed: " . $e->getMessage();
}

API Reference

SBWebPConverter

Main converter class.

Methods

  • convert(string $inputPath, string $outputPath, ?ConversionOptions $options = null): void
    • Converts an image file to WebP format
    • Parameters:
      • $inputPath: Path to input image file
      • $outputPath: Path to output WebP file
      • $options: Optional conversion options

SBConversionOptions

Configuration options for image conversion.

Methods

  • create(): self - Creates a new instance
  • setQuality(int $quality): self - Sets quality (0-100, default: 80)
  • setLossless(bool $lossless): self - Enables/disables lossless mode
  • preserveMetadata(bool $preserve): self - Preserves metadata when possible
  • getQuality(): int - Gets current quality setting
  • isLossless(): bool - Checks if lossless mode is enabled
  • shouldPreserveMetadata(): bool - Checks if metadata preservation is enabled

Supported Formats

  • Input: JPEG, PNG, GIF, BMP
  • Output: WebP

License

MIT License. See LICENSE file for details.

Documentation

For Persian documentation, see README.fa.md.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固