PHP code example of quansitech / cmf-module-media

1. Go to this page and download the library: Download quansitech/cmf-module-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/ */

    

quansitech / cmf-module-media example snippets


use Illuminate\Database\Eloquent\Model;
use Quansitech\Cmf\Media\Concerns\HasMedia;

class Post extends Model
{
    use HasMedia;
}

$post->syncMedia([1, 2, 3], 'gallery'); // 以 id 列表覆盖某字段的引用(差集增删)
$post->attachMedia(1, 'cover');         // 引用单个媒体
$post->detachMedia(1, 'cover');         // 解除引用

use Filament\Schemas\Schema;
use Quansitech\Cmf\Media\Filament\Forms\Components\MediaPicker;

class PostResource extends Resource
{
    public static function form(Schema $form): Schema
    {
        return $form->components([
            MediaPicker::make('cover'),               // 单选,state 为 media id(int|null)
            MediaPicker::make('gallery')->multiple(), // 多选,state 为 id 数组
        ]);
    }
}

// CreatePost 页面
class CreatePost extends CreateRecord
{
    protected static string $resource = PostResource::class;

    protected function afterCreate(): void
    {
        $this->record->syncMedia(array_filter((array) $this->data['cover']), 'cover');
        $this->record->syncMedia($this->data['gallery'] ?? [], 'gallery');
    }
}

// EditPost 页面:在 afterSave() 中写同样的两行

// config/cmf-media.php
'picker_library' => true,

MediaPicker::make('cover')
    ->cropAspectRatio('4:3')   // '4:3' / '4/3' / '1.5' / 1.5,或闭包按 $record 动态取值
    ->cropMaxWidth(1600);      // 可选,裁剪产物像素宽上限,超出等比缩小

use Quansitech\Cmf\Media\Models\Media;

// 取某字段引用的媒体
$coverId = $post->mediaUsages()->where('field', 'cover')->value('media_id');
$cover = $coverId ? Media::find($coverId) : null;

$cover?->url();       // 访问 URL(公开 URL,无公开 URL 时回退临时签名 URL)
$cover?->thumbUrl();  // 图片缩略图(云厂商图片处理参数;local 为原图)
$cover?->isImage();

// config/cmf-media.php
'rules' => [
    'cert-photo'   => ['mimes' => ['image/*'], 'max_size' => 10 * 1024 * 1024],
    'doc-preview'  => ['mimes' => ['application/pdf'], 'disposition' => 'inline', 'cache_seconds' => 86400],
    'export'       => ['mimes' => ['application/zip'], 'disposition' => 'attachment'],
],

MediaPicker::make('cert_photos')->uploadRule('cert-photo')->multiple()

$media->urlForEntry('doc-preview'); // 预览入口
$media->urlForEntry('export');      // 下载入口(attachment + 原始文件名)

use Filament\Forms\Components\RichEditor\Models\Contracts\HasRichContent;
use Illuminate\Database\Eloquent\Model;
use Quansitech\Cmf\Media\Concerns\HasMedia;
use Quansitech\Cmf\Media\Concerns\HasMediaRichContent;

class Post extends Model implements HasRichContent
{
    use HasMedia;
    use HasMediaRichContent;

    protected array $mediaRichContentAttributes = ['content']; // 可配多个字段;省略时默认 ['content']
}
bash
php artisan queue:work            # 开发/小规模
# 生产环境建议 Supervisor / Horizon 常驻守护
bash
php artisan cmf-media:prune-orphans
bash
php artisan cmf-media:smoke --disk=tos    # put → exists → get → delete 全流程
php artisan cmf-media:smoke --disk=local  # local 驱动无需凭证,随时可跑