1. Go to this page and download the library: Download jegex/laravel-media library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jegex / laravel-media example snippets
use Jegex\Media\MediaCollections\Concerns\HasMedia;
class Post extends Model
{
use HasMedia;
// ...
}
use Jegex\Media\MediaCollections\Models\Media;
// From a file path
$media = Media::createFromFile('/path/to/image.jpg');
// From string content
$media = Media::createFromString($imageContent, 'photo.jpg');
// From base64
$media = Media::createFromBase64($base64String, 'avatar.png');
// From a URL
$media = Media::createFromUrl('https://example.com/image.jpg');
// With custom options
$media = Media::createFromFile('/path/to/image.jpg', [
'collection_name' => 'uploads',
'name' => 'Custom Name',
'disk' => 's3',
'custom_properties' => ['source' => 'api'],
]);
// Access the media
echo $media->getUrl();
echo $media->toHtml();
// Get all media in a collection
$media = $model->getMedia('photos');
// Get first media
$media = $model->getFirstMedia('photos');
// Get media URL
$url = $media->getUrl();
// Get conversion URL
$url = $media->getUrl('thumb');
// Get temporary URL (S3)
$url = $media->getTemporaryUrl(now()->addMinutes(10));
// Convert to HTML string
echo $media->toHtml();
// <img src="..." alt="..." loading="lazy">
// Blade component
<x-media :media="$media" class="w-full" :loading="'lazy'" />
// With conversion
<x-media :media="$media" conversion="thumb" class="thumbnail" />
use Jegex\Media\MediaCollections\Concerns\HasMedia;
class Post extends Model
{
use HasMedia;
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('thumb')
->width(200)
->height(200)
->optimize()
->queued();
$this->addMediaConversion('preview')
->width(800)
->height(600)
->optimize();
}
}
$this->addMediaConversion('thumb')
->width(200)
->queued(); // Process on queue
$this->addMediaConversion('preview')
->width(800)
->nonQueued(); // Process immediately
// Stream ZIP directly to browser
return $post->getMediaCollectionZip('photos')->download('photos.zip');
// Save ZIP to a disk
$zipPath = $post->getMediaCollectionZip('photos')->saveToDisk('public', 'exports/photos.zip');
use Jegex\Media\MediaCollections\Models\Media;
// Get ZIP for a single media item
$zip = $media->getZip('archive.zip');
// Or retrieve media by ID first
$media = Media::find(1);
$zip = $media->getZip('archive.zip');
// Only $post->getMediaCollectionZip('photos', function ($zip, $media) {
$zip->add($media, 'thumb');
})->download('thumbs.zip');
use Jegex\Media\Facades\LaravelMedia;
// Create media from file
$media = LaravelMedia::createFromFile('/path/to/image.jpg');
// Create media from string content
$media = LaravelMedia::createFromString($imageContent, 'photo.jpg');
// Create media from base64
$media = LaravelMedia::createFromBase64($base64String, 'avatar.png');
// Create media from URL
$media = LaravelMedia::createFromUrl('https://example.com/image.jpg');
// Retrieve media
$media = LaravelMedia::getMediaById(1);
$media = LaravelMedia::getMediaByIds([1, 2, 3]);
$collection = LaravelMedia::getMediaByCollection('avatars');
// Delete media
LaravelMedia::deleteMedia(1);
// Get package info
$maxSize = LaravelMedia::getMaxFileSize(); // 10485760 (10MB)
$defaultDisk = LaravelMedia::getDefaultDisk(); // 'public'
namespace App\Support;
use Jegex\Media\Support\PathGenerator\DefaultPathGenerator;
use Jegex\Media\MediaCollections\Models\Media;
class CustomPathGenerator extends DefaultPathGenerator
{
public function getPath(Media $media): string
{
return $media->model_type.'/'.date('Y/m/d').'/'.$media->id;
}
}