daiyanmozumder/snaptrip

A Laravel package for resizing, compressing, watermarking, and converting images to WebP using Python.

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/daiyanmozumder/snaptrip

v2.0.0 2025-11-09 22:35 UTC

This package is auto-updated.

Last update: 2026-01-09 23:09:34 UTC


README

Smart Laravel Image Resizer, Compressor & Watermarker โ€” powered by Python.

SnapTrip is a Laravel package that seamlessly integrates a powerful Python-based image optimizer into your PHP workflow.
It can resize, compress to a target file size (default 400 KB), and optionally apply a watermark to your images โ€” all in one call.
All optimized images are automatically converted into WebP format for better performance and reduced file size.

๐Ÿš€ Features

  • ๐Ÿ”ง Resize images by width and height
  • ๐Ÿ“ฆ Compress images to a target size (default 400 KB)
  • ๐ŸŒ Converts all outputs to WebP format
  • ๐Ÿ’ง Optional watermark overlay (supports logo path from DB or file)
  • โšก Simple static facade for quick use
  • ๐Ÿง  Python-powered (using Pillow library for accurate results)
  • ๐Ÿงฉ Fully configurable & reusable across projects

๐Ÿ“ฆ Installation

1๏ธโƒฃ Install the package via Composer

composer require daiyanmozumder/snaptrip


2๏ธโƒฃ Install Python dependencies

This package uses a Python script to handle image optimization.

You need to have Python 3.8+ and Pillow installed.

โœ… Check if Python is installed:
python --version


If not, download and install Python
.

โœ… Install required Python libraries:
pip install pillow


(Optionally, for advanced image processing)

pip install numpy

3๏ธโƒฃ (Optional) Publish the config file
php artisan vendor:publish --tag=snaptrip-config


This creates config/snaptrip.php, where you can set default compression and paths.

โš™๏ธ Configuration

config/snaptrip.php

return [
'default_target_kb' => 400, // default compression size in KB
'default_output_folder' => 'uploads/optimized', // default output path
];

๐Ÿง  Usage
โœ… Example 1: Basic compression
use SnapTrip;

SnapTrip::optimize(
filePath: 'uploads/originals/image.jpg'
);


This will compress the image to 400 KB, save it as .webp, and return the optimized file path.

โœ… Example 2: Custom target size
SnapTrip::optimize(
filePath: 'uploads/originals/image.png',
targetKb: 250
);


Compresses and converts to .webp at roughly 250 KB.

โœ… Example 3: Resize + compress + watermark
SnapTrip::optimize(
filePath: 'uploads/originals/image.jpg',
targetKb: 350,
width: 800,
height: 600,
outputFolder: 'uploads/products',
watermarkLogo: 'storage/logos/company_logo.png'
);


This will:

Resize the image to 800x600

Compress to ~350 KB

Add watermark logo from the given path

Save result in uploads/products folder

Return .webp file path

โœ… Example 4: From a controller
use App\Http\Controllers\Controller;
use SnapTrip;

class ProductController extends Controller
{
public function upload(Request $request)
{
$file = $request->file('image');
$path = $file->store('uploads/originals');

        $optimizedPath = SnapTrip::optimize(
            filePath: $path,
            targetKb: 400,
            watermarkLogo: auth()->user()->company_logo // or null
        );

        return response()->json([
            'original' => $path,
            'optimized' => $optimizedPath,
        ]);
    }
}

๐Ÿงฉ How It Works

Laravel calls SnapTripService from the facade.

The service triggers a Python script (image_optimizer.py) via PythonRunner.

The Python script:

Opens the image using Pillow.

Resizes it if width/height are provided.

Compresses until the target file size (KB) is reached.

Adds watermark if logo path is provided.

Converts the result to .webp format.

Returns the optimized image path back to Laravel.

๐Ÿงช Testing Environment

You can test the environment and ensure Python is available using:

php artisan snaptrip:check-environment


Expected output:

โœ… Python found: Python 3.11.4
โœ… Pillow installed

๐Ÿ’ก Error Handling

SnapTrip gracefully handles:

Error	Description
Python not installed	Python is missing from system PATH
Pillow not installed	Missing dependency โ€” install via pip install pillow
Invalid image path	Provided image does not exist or is unreadable
Permission denied	Storage folder not writable

Youโ€™ll get descriptive exceptions for each case.

๐Ÿงฐ Example Output
Input	Output
uploads/originals/product.jpg	uploads/optimized/product_optimized.webp
๐Ÿ” Troubleshooting
โŒ Python not recognized

Make sure Python is added to your system PATH.
Try closing and reopening your terminal after installation.

โŒ Permission denied

Ensure your Laravel storage and uploads directories are writable.

chmod -R 775 storage uploads

๐Ÿงฉ Roadmap

Multi-threaded batch compression

Custom watermark position & opacity

Integration with Laravel Queues

Support for S3 or remote storage optimization

๐Ÿง‘โ€๐Ÿ’ป Author

Daiyan Mozumder

๐Ÿ“œ License

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

๐Ÿ’ฌ Contributing

Pull requests are welcome!
If youโ€™d like to improve SnapTrip, please fork the repository and submit a PR.

๐Ÿ Example Folder Structure (after install)
laravel_project/
โ”œโ”€โ”€ vendor/
โ”‚   โ””โ”€โ”€ daiyanmozumder/
โ”‚       โ””โ”€โ”€ snaptrip/
โ”‚           โ”œโ”€โ”€ src/
โ”‚           โ”œโ”€โ”€ python/
โ”‚           โ”‚   โ””โ”€โ”€ image_optimizer.py
โ”‚           โ””โ”€โ”€ config/snaptrip.php
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ snaptrip.php
โ””โ”€โ”€ storage/
โ””โ”€โ”€ app/uploads/optimized/


๐Ÿ’ก Tip: SnapTrip is designed for effortless plug-and-play image optimization with minimal setup โ€” perfect for high-performance Laravel apps that demand small, sharp, and optimized images in WebP format.


---

Would you like me to now generate the **actual `image_optimizer.py` file** and the **corresponding Laravel PHP service