承接 wad/image-compressor 相关项目开发

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

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

wad/image-compressor

Composer 安装命令:

composer require wad/image-compressor

包简介

A lightweight PHP package to compress image files (JPEG, PNG, WebP) with adjustable quality or target size. Easily reduce file sizes in your PHP projects using pure GD.

README 文档

README

A lightweight PHP package to compress image files (JPEG, PNG, WebP) with adjustable quality or target size. Easily reduce file sizes in your PHP projects using pure GD.

🚀 Features

  • ✅ Compress JPEG, PNG, and WebP images
  • ✅ Target compression by percentage or file size (KB)
  • ✅ Compress single or multiple images in batch
  • ✅ Automatically resizes large images (maintains aspect ratio)
  • ✅ Maintains transparency for PNG/WebP
  • ✅ Overwrite originals or save to custom folder
  • ✅ Pure PHP with GD – no external dependencies
  • ✅ Easy integration with Laravel, Symfony, or standalone PHP

⚙️ Requirements

  • PHP 7.2 or higher
  • GD extension enabled (ext-gd)

📦 Installation

Install via Composer:

composer require wad/image-compressor:dev-master

✅ Basic Usage

require 'vendor/autoload.php';

use Wad\ImageCompressor\Compressor;

// Create compressor with default quality (JPEG: 75, PNG/WebP: 6)
$compressor = new Compressor();

// Compress single image by 50% size
$original = __DIR__ . '/image.png';
$compressed = __DIR__ . '/image-compressed.png';

$result = $compressor->compressToPercentage($original, $compressed, 0.5);

if ($result !== false) {
    echo "Compressed successfully to $result bytes";
} else {
    echo "Compression failed";
}

🎯 Compress to Target File Size

$compressor = new Compressor();

$input = __DIR__ . '/banner.jpg';
$output = __DIR__ . '/banner-small.jpg';

// Compress to 300 KB
$result = $compressor->compressToSize($input, $output, 300);

echo $result ? "Compressed to $result bytes" : "Compression failed";

🗂 Batch Compression (By Percentage)

$files = [
    __DIR__ . '/img1.jpg',
    __DIR__ . '/img2.png',
];

$outputDir = __DIR__ . '/compressed_50';

$results = $compressor->compressMultipleToPercentage($files, $outputDir, 0.5);

foreach ($results as $file => $size) {
    echo basename($file) . ": " . ($size ? "$size bytes" : "Failed") . "\n";
}

📏 Batch Compression (By Target Size)

$files = [
    __DIR__ . '/photo1.webp',
    __DIR__ . '/photo2.png',
];

$outputDir = __DIR__ . '/compressed_300kb';

$results = $compressor->compressMultipleToSize($files, $outputDir, 300);

foreach ($results as $file => $size) {
    echo basename($file) . ": " . ($size ? "$size bytes" : "Failed") . "\n";
}

📝 License

MIT License. See the LICENSE file.

👤 Author We Are Developers 📧 suriyatkms1218.in@gmail.com 🌐 https://suriyadev.in

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-29