PHPackages                             snipershady/picdiet - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. snipershady/picdiet

ActiveLibrary

snipershady/picdiet
===================

The lightweight image compressor for the web (JPG/WebP)

v1.0.1(1w ago)30GPL-2.0-onlyPHPPHP &gt;=8.3CI passing

Since Mar 10Pushed 1w agoCompare

[ Source](https://github.com/snipershady/picdiet)[ Packagist](https://packagist.org/packages/snipershady/picdiet)[ Docs](https://www.spinfo.it)[ RSS](/packages/snipershady-picdiet/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

PicDiet
=======

[](#picdiet)

[![Latest Version](https://camo.githubusercontent.com/65add23103346fd973e1ba59d1e7e05ef5e526c01955d84c5b6c29b1166026eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736e6970657273686164792f706963646965742e737667)](https://packagist.org/packages/snipershady/picdiet)[![Total Downloads](https://camo.githubusercontent.com/f81b8dbc2cec3f62391509ec12f27edd200bc25cc70d2c7af24a9ecd8d9ea53f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736e6970657273686164792f706963646965742e737667)](https://packagist.org/packages/snipershady/picdiet)[![Monthly Downloads](https://camo.githubusercontent.com/954cdbf68bd2c7a8d3672f083577ac1ffea7487bbccbb8585007a8457d8208bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f736e6970657273686164792f706963646965742e737667)](https://packagist.org/packages/snipershady/picdiet)[![PHP Version](https://camo.githubusercontent.com/55efaef6a013ff461b31688b6e0aadd1b4c77c1b69eebef92bc70d709faac026/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736e6970657273686164792f706963646965742e737667)](https://packagist.org/packages/snipershady/picdiet)[![License](https://camo.githubusercontent.com/888ec41b92f0e8dc6c42846366b19ef41b20c7189c974fa4611d678d41ba017a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736e6970657273686164792f706963646965742e737667)](https://packagist.org/packages/snipershady/picdiet)[![Tests](https://github.com/snipershady/picdiet/actions/workflows/tests.yml/badge.svg)](https://github.com/snipershady/picdiet/actions/workflows/tests.yml)[![codecov](https://camo.githubusercontent.com/2d2e88b5a76a978fd89811b9c9876f15bd73580d6614833a399f1c03d73f241f/68747470733a2f2f636f6465636f762e696f2f67682f736e6970657273686164792f706963646965742f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/snipershady/picdiet)

Lightweight PHP library for compressing and converting images to **WebP** or **JPEG** format, with automatic resizing while preserving the aspect ratio.

---

Requirements
------------

[](#requirements)

RequirementVersionPHP&gt;= 8.3ext-gd\*---

Installation
------------

[](#installation)

```
composer require snipershady/picdiet
```

---

Quick Start
-----------

[](#quick-start)

```
use PicDiet\Service\ImageCompressorService;
use PicDiet\Enum\ImageFormatEnum;

$service = new ImageCompressorService();
$response = $service->compress('/path/to/image.jpg');

if ($response->success) {
    echo $response->path;               // /path/to/image_compressed.webp
    echo $response->compressedFileName; // image_compressed.webp
    echo $response->outputDirectory;    // /path/to
} else {
    echo $response->error;
}
```

---

API Reference
-------------

[](#api-reference)

### `ImageCompressorService::compress()`

[](#imagecompressorservicecompress)

```
public function compress(
    string $sourcePath,
    ImageFormatEnum $format = ImageFormatEnum::WEBP,
    int $maxWidth = 1920,
    int $maxHeight = 1080,
    ?int $quality = null,
): CompressionResponse
```

ParameterTypeDefaultDescription`$sourcePath``string`—Absolute path to the source image`$format``ImageFormatEnum``ImageFormatEnum::WEBP`Output format (`WEBP` or `JPG`)`$maxWidth``int``1920`Maximum output width in pixels`$maxHeight``int``1080`Maximum output height in pixels`$quality``?int``85`Compression quality from `0` (worst) to `100` (best)**Supported input formats:** JPEG, PNG, GIF, WebP.

**Resize behaviour:** if the source image is smaller than `$maxWidth` × `$maxHeight` it is never upscaled. If it is larger, it is scaled down proportionally to fit within the given bounds.

**Output file:** the compressed file is saved in the same directory as the source file with the suffix `_compressed` appended to the original filename.

```
/images/photo.jpg  →  /images/photo_compressed.webp

```

---

### `ImageFormatEnum`

[](#imageformatenum)

```
use PicDiet\Enum\ImageFormatEnum;

ImageFormatEnum::WEBP  // value: 'webp'
ImageFormatEnum::JPG   // value: 'jpg'
```

---

### `CompressionResponse`

[](#compressionresponse)

All properties are `readonly`.

PropertyTypeDescription`success``bool``true` if compression succeeded`path``?string`Full absolute path to the compressed file`compressedFileName``?string`Filename of the compressed file (e.g. `photo_compressed.webp`)`outputDirectory``?string`Directory where the compressed file was saved`originalSize``int`Size of the source file in bytes`compressedSize``int`Size of the compressed file in bytes`format``ImageFormatEnum`Format actually used for compression`error``?string`Error message when `success` is `false`, otherwise `null`---

Usage Examples
--------------

[](#usage-examples)

### Convert to WebP (default)

[](#convert-to-webp-default)

```
use PicDiet\Service\ImageCompressorService;

$service = new ImageCompressorService();
$response = $service->compress('/var/www/uploads/photo.jpg');

if ($response->success) {
    printf(
        "Saved %d bytes (%.1f%% reduction)\n",
        $response->originalSize - $response->compressedSize,
        (1 - $response->compressedSize / $response->originalSize) * 100,
    );
}
```

### Convert to JPEG

[](#convert-to-jpeg)

```
use PicDiet\Enum\ImageFormatEnum;
use PicDiet\Service\ImageCompressorService;

$service = new ImageCompressorService();
$response = $service->compress('/var/www/uploads/photo.png', ImageFormatEnum::JPG);
```

### Custom dimensions and quality

[](#custom-dimensions-and-quality)

```
use PicDiet\Enum\ImageFormatEnum;
use PicDiet\Service\ImageCompressorService;

$service = new ImageCompressorService();
$response = $service->compress(
    sourcePath: '/var/www/uploads/photo.jpg',
    format: ImageFormatEnum::WEBP,
    maxWidth: 1280,
    maxHeight: 720,
    quality: 75,
);
```

### Error handling

[](#error-handling)

```
use PicDiet\Service\ImageCompressorService;

$service = new ImageCompressorService();
$response = $service->compress('/var/www/uploads/photo.jpg');

if (!$response->success) {
    error_log('PicDiet compression failed: ' . $response->error);
    return;
}

// Safe to use $response->path, $response->compressedFileName, etc.
```

### Symfony integration

[](#symfony-integration)

Register the service in the container (if autowiring is not used):

```
# config/services.yaml
services:
    PicDiet\Service\ImageCompressorService: ~
```

Use it in a controller:

```
use PicDiet\Enum\ImageFormatEnum;
use PicDiet\Service\ImageCompressorService;
use Symfony\Component\HttpFoundation\Request;

class ImageController
{
    public function __construct(
        private readonly ImageCompressorService $compressor,
    ) {}

    public function upload(Request $request): void
    {
        $file = $request->files->get('image');
        $uploadPath = '/var/www/uploads/' . $file->getClientOriginalName();
        $file->move('/var/www/uploads', $file->getClientOriginalName());

        $response = $this->compressor->compress(
            sourcePath: $uploadPath,
            format: ImageFormatEnum::WEBP,
        );

        if (!$response->success) {
            throw new \RuntimeException($response->error);
        }

        // $response->path → path to the compressed file
    }
}
```

### Laravel integration

[](#laravel-integration)

```
use PicDiet\Enum\ImageFormatEnum;
use PicDiet\Service\ImageCompressorService;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request): void
    {
        $path = $request->file('image')->store('uploads');
        $fullPath = storage_path('app/' . $path);

        $compressor = new ImageCompressorService();
        $response = $compressor->compress(
            sourcePath: $fullPath,
            format: ImageFormatEnum::WEBP,
        );

        if (!$response->success) {
            abort(500, $response->error);
        }

        // $response->path → path to the compressed file
    }
}
```

---

Development
-----------

[](#development)

Clone the repository and install dependencies:

```
git clone https://github.com/snipershady/picdiet.git
cd picdiet
composer install
```

### Available commands

[](#available-commands)

CommandDescription`composer test`Run the PHPUnit test suite`composer phpstan`Run PHPStan static analysis`composer cs-fix`Fix code style with PHP-CS-Fixer`composer cs-check`Check code style without applying changes`composer rector`Run Rector refactoring`composer rector-dry`Preview Rector changes without applying them`composer quality`Run all quality tools (Rector + PHP-CS-Fixer)`composer quality-check`Check all quality rules without applying changes---

License
-------

[](#license)

Released under the [GPL-2.0-only](https://www.gnu.org/licenses/old-licenses/gpl-2.0.html) license.

---

Author
------

[](#author)

**Stefano Perrini** — [spinfo.it](https://www.spinfo.it)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance98

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

11d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.2

v1.0.1PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/160662d52c89da3351e2a94b3d354074bf30137477fc7586eeacc52e3cb44462?d=identicon)[snipershady](/maintainers/snipershady)

---

Top Contributors

[![snipershady](https://avatars.githubusercontent.com/u/20489856?v=4)](https://github.com/snipershady "snipershady (16 commits)")

---

Tags

image-compressionpicdietwebpfy

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/snipershady-picdiet/health.svg)

```
[![Health](https://phpackages.com/badges/snipershady-picdiet/health.svg)](https://phpackages.com/packages/snipershady-picdiet)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
