1. Go to this page and download the library: Download whitecube/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/ */
whitecube / laravel-media example snippets
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Whitecube\Media\Attributes\Image;
class User extends Model
{
protected function avatar(): Attribute
{
return Image::attribute($this, 'avatar')
->variants([
\App\Media\Images\SquareIcon::class,
\App\Media\Images\SquareRegular::class,
\App\Media\Images\ProfileCover::class,
\App\Media\Images\SocialShare::class,
])
->default('square-regular')
->disk('public')
->path('users');
}
}
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Whitecube\Media\Attributes\Image;
class Post extends Model
{
protected function img(): Attribute
{
return Image::attribute($this, 'img');
}
}
protected function img(): Attribute
{
return Image::attribute($this, 'img')
->repository(\App\Media\DatabaseMediaRepository::class);
}
namespace App\Media\Images;
use Whitecube\Media\MediaFile;
use Whitecube\Media\Generators\Variant;
use Whitecube\Media\Generators\Output;
use Whitecube\Media\Generators\Enums\Format;
use Intervention\Image\Laravel\Facades\Image;
class SquareRegular implements Variant
{
public function key(): string
{
return 'square-regular';
}
public function output(): Output
{
return Output::make(Format::Webp)
->suffix($this->key())
->fit(width: 512, height: 512);
}
public function generate(Output $output): MediaFile
{
$image = $output->resize->apply(
image: Image::decode($output->original->fullPath())
);
return $output->store($image, quality: 80);
}
}