Download the PHP package dialloibrahima/laravel-model-media without Composer

On this page you can find all versions of the php package dialloibrahima/laravel-model-media. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-media

Latest Version on Packagist GitHub Tests Action Status Total Downloads PHP Version Laravel Version

A lightweight, zero-boilerplate media management trait for Laravel Eloquent models. Attach files directly to your existing model attributes without adding any extra database tables or complex relationships.

[!TIP] Includes a built-in Glide integration for on-the-fly image manipulation: resize, crop, convert formats, and generate responsive images.


Table of Contents


The Problem

Most media management packages for Laravel (like Spatie MediaLibrary) are powerful but "heavy". They often require:

The Solution

Laravel Model Media keeps it simple. It uses your existing database columns to store file names.


Comparison: Spatie MediaLibrary vs. Laravel Model Media

Feature Spatie MediaLibrary Laravel Model Media
Philosophy "One table for everything" "Keep it on the model"
New Tables media (Polymorphic) None
Complexity High (Conversions, Collections) Low (Simple & Fast)
Performance Extra Join/Query for each model Zero extra queries
Setup Migrations + Trait + Interface Trait only
Image Manipulation Built-in conversions Glide (included)
Ideal for Complex CMS, Multiple galleries Profile pics, Single documents, Simple uploads

Installation

This installs league/glide automatically as a dependency, giving you image manipulation out of the box.


Quick Start

1. Prepare your Model

Add the HasMedia trait and register which column should handle media.

[!NOTE] The fileName parameter can be a model attribute name (like 'slug') or a Closure for more complex naming logic.

2. Handle Uploads

Call attachMedia() in your controller.

[!IMPORTANT] attachMedia automatically calls $this->save(). You don't need to manually save the model unless you perform other modifications.

3. Retrieve URLs

4. Delete Media


API Reference

HasMedia Trait

The core trait that provides media management capabilities.

registerMediaForColumn()

Register a media mapping for a model column.

attachMedia()

Attach an uploaded file to a column.

Returns: true if storage was successful, false otherwise.

Behavior:

detachMedia()

Remove media from a column and delete the file.

Returns: true when complete.

Behavior:

getMediaUrl()

Get the public URL for a media file.

Returns: The full URL to the file, or null if no file is attached.

getMediaMappings()

Get all registered media mappings for the model.

[!NOTE] Media mappings are scoped per model class. Each model maintains its own independent set of column mappings, so you can safely use HasMedia on multiple models without conflicts.


HasGlideUrls Trait

Adds Glide image manipulation capabilities. Requires the HasMedia trait on the same model.

[!CAUTION] This trait only works with image files. It validates MIME types and returns null (or throws InvalidMediaTypeException when throwOnError is true) for non-image files.

getGlideUrl()

Generate a URL for a transformed image.

Returns: URL to the transformed image, or null if unavailable.

getGlidePresetUrl()

Generate a URL using a predefined preset.

Returns: URL with preset parameters applied, or null if the preset doesn't exist or the file is not a valid image.

getGlideSrcset()

Generate a srcset attribute for responsive images.

Usage in Blade:

hasImageMedia()

Check if a column contains a valid image file.


Advanced Usage

Dynamic Filenames via Closure

If you need complex naming logic, use a Closure:

The Closure receives:

Multiple Storage Disks

Automatic Cleanup

You don't need to do anything! Laravel Model Media automatically:


Image Manipulation with Glide

The package includes a built-in Glide integration for on-the-fly image manipulation.

Add the HasGlideUrls trait to your model alongside HasMedia:

Publish the Glide configuration:

Glide Basic Usage

Once configured, the HasGlideUrls trait provides convenient methods to generate URLs directly from your models:

Transformation Parameters

Parameter Description Example
w Width in pixels ['w' => 200]
h Height in pixels ['h' => 200]
fit Fit mode: contain, max, fill, stretch, crop ['fit' => 'crop']
fm Format: jpg, png, gif, webp ['fm' => 'webp']
q Quality (0-100) ['q' => 85]
blur Blur (0-100) ['blur' => 5]
bri Brightness (-100 to 100) ['bri' => 10]
con Contrast (-100 to 100) ['con' => 10]
gam Gamma (0.1 to 9.99) ['gam' => 1.5]
sharp Sharpen (0-100) ['sharp' => 10]
flip Flip: v (vertical), h (horizontal), both ['flip' => 'h']
or Orientation: auto, 0, 90, 180, 270 ['or' => 'auto']
border Border in format width,color,method ['border' => '5,FFFFFF,overlay']

See the Glide documentation for all available parameters.

Using Presets

Presets allow you to define reusable transformation sets:

Then use them in your code:

[!NOTE] getGlidePresetUrl() returns null if the preset name doesn't exist in the configuration.

Responsive Images (srcset)

Generate responsive image sets automatically:

Standalone Usage (Facade)

You can generate Glide URLs anywhere (controllers, services, Blade) using the Glide facade, even without an Eloquent model:

[!NOTE] Glide::url() automatically validates that the file exists and is a valid image before generating the URL. If validation fails, it returns null. Glide::preset() also returns null if the preset name doesn't exist.

Glide Configuration

Publish the configuration file:

[!TIP] The package uses Laravel disk names instead of hardcoded paths. This means it works seamlessly with S3, Cloudflare R2, or any Flysystem driver. Just set GLIDE_SOURCE_DISK=s3 in your .env file.

[!IMPORTANT] imagick is strongly recommended over gd as the image driver. Imagick uses Lanczos resampling which produces sharper, cleaner results compared to gd's bilinear interpolation. Verify it's installed with php -m | grep imagick.

Optimization Support

The package is fully compatible with Laravel's optimization system:

This will cache your configuration and routes while maintaining full functionality, including secure image signatures.

Security

Enable URL signing to prevent unauthorized image manipulation (DoS protection):

Generate a secure key:

When secure mode is enabled, all Glide URLs will be cryptographically signed. Requests with invalid or missing signatures receive a 403 response.

Automatic Cache Cleanup

The HasGlideUrls trait automatically registers a GlideCacheObserver that cleans up cached images when:

This happens automatically - you don't need to configure anything. The observer:

  1. Detects when a media column changes
  2. Finds cached transformations for the old image
  3. Deletes them from the Glide cache directory

This prevents stale cached images from being served and saves disk space.

Manual Cache Clearing

If you need to manually clear the cache for a specific image:


Configuration

You can publish the configuration files to customize the package behavior:


How It Works


Testing

The package includes a robust test suite. You can run it via composer:

We test for:


Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

Credits


All versions of laravel-model-media with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
ext-fileinfo Version *
illuminate/contracts Version ^11.0||^12.0||^13.0
league/glide Version ^3.1
spatie/laravel-package-tools Version ^1.16
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dialloibrahima/laravel-model-media contains the following files

Loading the files please wait ...