wall-e/laravel-imageresize

A Laravel package to upload and resize images easily with automatic aspect ratio.

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/wall-e/laravel-imageresize

1.0.1 2025-09-05 15:14 UTC

This package is auto-updated.

Last update: 2026-01-09 09:41:43 UTC


README

A simple and reusable Laravel package for uploading and resizing images with automatic aspect ratio handling. You can define multiple sizes dynamically (small, thumb, custom), and it supports optional width and height. Perfect for books, sliders, profiles, banners, and any image resizing needs in Laravel.

Requirements

  • PHP >= 8.0

  • Laravel 9.x or 10.x

๐ŸŒ Features

  • Resize images to multiple sizes in one function call.

  • Automatic aspect ratio calculation if height is not provided.

  • Supports JPEG, PNG, GIF, and WebP formats.

  • Returns all resized image paths for easy database storage.

  • Configurable image quality via config/imageresizer.php.

  • Works with GdImage objects or uploaded files.

  • Can be used in any Laravel application via Composer.

๐Ÿ“ฆ Installation

Require the package via Composer:

composer require wall-e/laravel-imageresize

โš™๏ธ Configuration

If your Laravel version is auto-discoverable, the service provider is loaded automatically. Otherwise, add the provider manually in config/app.php:

'providers' => [
    // Other Service Providers
    WallE\LaravelImageresize\ImageResizerServiceProvider::class,
],

Publish the config file (optional):

php artisan vendor:publish --provider="WallE\LaravelImageresize\ImageResizerServiceProvider" --tag=config

This will create config/imageresizer.php:

return [
    'quality' => 90, // Default image quality (JPEG/WebP)
];

๐Ÿ› ๏ธ Usage

Import the class:

use WallE\LaravelImageresize\ImageFunctions;

Basic Example:

$imagePaths = ImageFunctions::upload(
    $request->file('image_path'), // Uploaded file
    'media/uploads/books', // Storage path
    [
        'small' => ['width' => 158],
        'thumb' => ['width' => 72],
    ]
);

Returns an array with resized image paths:

[
    'original' => 'media/uploads/book/filename-original.webp',
    'small'    => 'media/uploads/book/filename-small.webp',
    'thumb'    => 'media/uploads/book/filename-thumb.webp',
]

Save in database:

$book->image_path       = $imagePaths['original'];
$book->image_sm_path    = $imagePaths['small'];
$book->image_thumb_path = $imagePaths['thumb'];
$book->save();

Custom Sizes with Optional Height:

$imagePaths = ImageFunctions::upload(
    $request->file('image_path'),
    'media/uploads/sliders',
    [
        'large' => ['width' => 1920, 'height' => 600], // exact dimensions
        'medium' => ['width' => 1280],                // height calculated automatically
        'thumb' => ['width' => 320],
    ]
);

Using GdImage Objects:

$gdImage = imagecreatefromjpeg('example.jpg');

$imagePaths = ImageFunctions::upload(
    $gdImage,
    'media/uploads/custom',
    [
        'small' => ['width' => 200],
        'thumb' => ['width' => 100],
    ]
);

Advanced Usage:

You can define any number of sizes dynamically:

$sizes = [
    'small'  => ['width' => 150],
    'medium' => ['width' => 300, 'height' => 300],
    'large'  => ['width' => 600],
    'custom' => ['width' => 1024, 'height' => 768],
];

$imagePaths = ImageFunctions::upload($request->file('image_path'), 'uploads/gallery', $sizes);

If height is omitted, it is automatically calculated from the original aspect ratio.

Configuration

You can configure default image quality in config/imageresizer.php:

return [
    'quality' => 90, // Range 0 - 100
];

Supported Formats:

  • JPEG (.jpg / .jpeg)

  • PNG (.png)

  • GIF (.gif)

  • WebP (.webp)

All images are converted to WebP by default except GIFs.

Notes:

  • Temporary images are created in storage/app/temp-images during processing.

  • The package is fully compatible with Laravelโ€™s storage disks. You can modify it to save directly to S3 or other disks.

  • Returns array of paths, so you can save multiple sizes to DB.

Example in Controller:

public function store(Request $request)
{
    $request->validate(['image_path' => 'required|image']);

    $imagePaths = ImageFunctions::upload(
        $request->file('image_path'),
        'media/uploads/books',
        [
            'small' => ['width' => 158],
            'thumb' => ['width' => 72],
        ]
    );

    $book = Book::create([
        'title'        => $request->title,
        'image_path'     => $imagePaths['original'],
        'image_sm_path'  => $imagePaths['small'],
        'image_thumb_path' => $imagePaths['thumb'],
    ]);

    return response()->json([
        'message' => 'Book created successfully',
        'data'    => $book
    ]);
}

๐Ÿ“ Contributing

Fork the repository and submit pull requests.

Bug reports, feature requests, and improvements are welcome.

Make sure to follow semantic versioning for updates.

๐Ÿ“œ License

MIT License. See the LICENSE file for details.

๐Ÿ”— Links

Packagist: https://packagist.org/packages/wall-e/laravel-imageresize

GitHub Repository: https://github.com/Rahman-Shaikat/laravel-imageresize.git

โœ… This README explains:

  • Installation

  • Configuration

  • Multiple usage examples

  • Handling aspect ratio

  • Saving to database