Laravel EnsoLaravel Enso
Guide
Back End
Front End
GitHub
Guide
Back End
Front End
GitHub
  • Back End

    • Action Logger
    • Addresses
    • Algolia
    • Algolia Webshop
    • API
    • Audits
    • Avatars
    • Cache Chain
    • Calendar
    • Cargo Partner
    • Categories
    • Charts
    • CI/CD
    • Cli
    • CNP Validator
    • Comments
    • Commercial
    • Companies
    • Core
    • Countries
    • Currencies
    • Data Export
    • Data Import
    • Discounts
    • Documents
    • Dynamic Methods
    • EAV
    • Emag
    • Enums
    • Excel
    • Facebook
    • Files
    • Filters
    • Financials
    • Forms
    • Frisbo
    • Google
    • Helpers
    • Holidays
    • How-to
    • Image Transformer
    • Impersonate
    • Interactions
    • Inventory
    • IO
    • Localisation
    • Lockable Models
    • Logs
    • Measurement Units
    • Meili Search
    • Meili Search Webshop
    • Menus
    • Migrator
    • Monitored Emails
    • Notifications
    • Packaging Units
    • PDF
    • People
    • Permissions
    • Product Eav
    • Product Lots
    • Products
    • Questionnaires
    • Rememberable
    • Roles
    • Sale Channels
    • Searchable
    • Select
    • Sentry
    • Services
    • Smart Bill
    • Sms Advert
    • Stripe
    • Tables
    • Tasks
    • Teams
    • Ticketing
    • Track Who
    • Tutorials
    • Typesense
    • Typesense Webshop
    • Unit Conversion
    • Upgrade
    • UPS
    • User Groups
    • Users
    • Versions
    • Virtual Call Center
    • Vouchers
    • Webshop
    • Webshop Commercial

Image Transformer

LicenseStableDownloadsPHPIssuesMerge Requests

Description

Image Transformer provides a small service for validating, resizing, and optimizing uploaded images.

It wraps Intervention Image for resize operations and Spatie's image optimizer for post-processing, while enforcing a limited set of supported image mime types before any transformation is attempted.

The package is useful in upload flows where images should be normalized before storage, for example avatars, covers, gallery images, or document attachments that need size limits and optimization.

Installation

Install the package:

composer require laravel-enso/image-transformer

The package relies on:

  • intervention/image-laravel for reading and resizing images
  • spatie/laravel-image-optimizer for optimization

To use resizing, the runtime must have at least one supported image extension installed:

  • gd
  • imagick

Features

  • Validates uploaded files before transformation.
  • Supports png, jpeg, gif, and webp images.
  • Optimizes images in place through Spatie's optimizer chain.
  • Resizes images proportionally by width, by height, or by both through resize().
  • Prevents upsizing by only resizing when the original image is larger than the requested dimension.
  • Saves transformations back to the original file path.

Usage

Create a transformer from an uploaded file:

use LaravelEnso\ImageTransformer\Services\ImageTransformer;

$transformer = new ImageTransformer($file);

Optimize the original file:

$transformer->optimize();

Resize proportionally to a maximum width:

$transformer->width(512);

Resize proportionally to a maximum height:

$transformer->height(512);

Apply both constraints in sequence:

$transformer->resize(1024, 768);

Tip

The transformer edits the original file in place.

If you need to preserve the original upload, copy or move it before calling optimize(), width(), height(), or resize().

API

Service

LaravelEnso\ImageTransformer\Services\ImageTransformer

Constructor:

  • __construct(File $file)

Public methods:

  • optimize(): self
  • resize(int $width, int $height): self
  • width(int $width): self
  • height(int $height): self

Supported Mime Types

The service accepts:

  • image/png
  • image/jpeg
  • image/gif
  • image/webp

Exceptions

The package exposes:

  • LaravelEnso\ImageTransformer\Exceptions\File
  • LaravelEnso\ImageTransformer\Exceptions\Dependency

Scenarios covered:

  • invalid uploaded file
  • unsupported mime type
  • missing gd / imagick extension when resizing

Depends On

Required Enso packages:

  • laravel-enso/helpers ↗

External dependencies:

  • intervention/image-laravel ↗
  • spatie/laravel-image-optimizer ↗

Contributions

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

Edit this page on GitHub
Last Updated: 4/19/2026, 10:23:57 PM
Prev
How-to
Next
Impersonate