PHP code example of webplusm / gallery-json-media

1. Go to this page and download the library: Download webplusm/gallery-json-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/ */

    

webplusm / gallery-json-media example snippets


use Spatie\Image\Enums\ImageDriver;
return [
    ...
    'images' => [
        'driver' => ImageDriver::Gd, // or ImageDriver::Imagick
    ],
    ...
]

return [
    'disk' => 's3',
    'visibility' => 'private', // 'public' by default
    ...
]

JsonMediaGallery::make('images')
    ->disk('s3')
    ->visibility('private')

use GalleryJsonMedia\JsonMedia\Concerns\InteractWithMedia;
use GalleryJsonMedia\JsonMedia\Contracts\HasMedia;

class Page extends Model implements HasMedia
{
    use HasFactory;
    use InteractWithMedia;
    
    protected $casts =[
        'images' => 'array',
        'documents' => 'array',
    ];
    
    // for auto-delete media thumbnails
    protected function getFieldsToDeleteMedia(): array {
        return ['images','documents'];
    }
    ...
    
}

use GalleryJsonMedia\JsonMedia\Casts\AsJsonMedia;

protected $casts = [
    'images' => AsJsonMedia::class,
    'documents' => AsJsonMedia::class,
];

$page->images; // Collection<Media|Document>
$page->images->first()->getCropUrl(400, 300);
$page->images->first()->getOriginalDimensions()?->width; // the size of the image, null when it is not recorded

use GalleryJsonMedia\Form\JsonMediaGallery;
JsonMediaGallery::make('images')
    ->directory('page')
    ->reorderable() 
    ->acceptedFileTypes()
    ->disk('s3') // any Laravel disk, the `gallery-json-media.disk` config by default
    ->visibility('private') // 'public' or 'private', see "Remote and private storage"
    ->maxSize(4 * 1024)
    ->minSize()
    ->maxFiles(2)
    ->minFiles(1)
    ->replaceTitleByAlt() // If you want to show alt customProperties  against file name
    ->image() // only images by default , u need to choose one method (image or document)
    ->document() // only documents (eg: pdf, doc, xls,...)
    ->downloadable()
    ->deletable()
    ->withCustomProperties(
       customPropertiesSchema: [                                     
            ...some form fields here
        ],
       editCustomPropertiesOnSlideOver: true,
       editCustomPropertiesTitle: "Edit customs properties"
    )
    ->editableCustomProperties(bool|Closure) // if you want to enable/disable the custom properties edition ;

use GalleryJsonMedia\Tables\Columns\JsonMediaColumn;
JsonMediaGallery::make('images')
    ...
    ->displayOnGrid()

use GalleryJsonMedia\Tables\Columns\JsonMediaColumn;
JsonMediaColumn::make('images')
    ->avatars(bool|Closure)

use GalleryJsonMedia\Infolists\JsonMediaEntry;
use GalleryJsonMedia\Infolists\JsonDocumentsEntry;
JsonMediaEntry::make('images')
    ->avatars()
    ->thumbHeight(100)
    ->thumbWidth(100)
    ->visible(static fn(array|null $state)=> filled($state))


// or for Documents, you can download them here 
GalleryJsonMedia\Infolists\JsonDocumentsEntry::make('documents')
    ->columns(4)
    ->columnSpanFull()

// config/gallery-json-media.php
'maintenance' => [
    'models' => [
        App\Models\Post::class => ['images', 'documents'],
    ],
],
bash
php artisan vendor:publish --tag="gallery-json-media-config"
bash
php artisan vendor:publish --tag="gallery-json-media-translations"
bash
php artisan vendor:publish --tag="gallery-json-media-views"
html
@foreach($page->getMedias('images') as $media)
    <div style="display: flex;gap: .5rem">
        {{ $media->withImageProperties( width : 200,height: 180)->withView('page.json-media') }}
    </div>
 @endforeach


<!-- the json-media.blade.php -->
@php
   use GalleryJsonMedia\JsonMedia\Media;
   /** @var Media $media*/
   $media
@endphp
<figure class="" style="width: {{ $media->width }}px">
    <img class="object-cover w-full aspect-video" loading="lazy"
         src="{{ $media->getCropUrl(width: $media->width,height: $media->height) }}"
         alt="{{ $media->getCustomProperty('alt') }}"
         width="{{ $media->width }}"
         height="{{ $media->height }}"
    >
</figure>
bash
php artisan gallery-json-media:clean --dry-run   # list the files to delete
php artisan gallery-json-media:clean             # delete them, after confirmation
php artisan gallery-json-media:clean --force     # without confirmation (cron, deployment)