简体   繁体   中英

Using Image Intervention with Laravel Nova

I have Image Intervention working and pushing to S3, but I can't save the filename to the table.

this is what I have so far:

// Use AS because Image is already a Nova facade
use Intervention\Image\Facades\Image as Cropper;

- - -

Avatar::make('Image Large')
    ->store(function (Request $request, $model) {

        // Create a UUID filename
        $fileName = $this->uuid() . '.jpg';

        // Crop with Image Intervention
        $cropped = Cropper::make($request->image_large)->fit(100, 50, function ($c) {
            $c->upsize();
        })->encode('jpg', 80);

        // Store on S3
        Storage::disk('s3_image')->put($fileName, (string) $cropped);

        // Save filename in DB
        $model->update([
            'image_large' => $fileName,
        ]);
    })
    ->rules('required')
    ->prunable(),

All working except the last part, saving the filename.

I figured it out

Avatar::make('Image', 'image_large')
    ->store(function (Request $request, $model) {

        // Create a UUID filename
        $fileSmall = $this->uuid() . '.jpg';
        $fileLarge = $this->uuid() . '.jpg';

        // Crop with Image Intervention
        $imageSmall = Cropper::make($request->image)->fit(200, 100, function ($c) {
            $c->upsize();
        })->encode('jpg', 90);
        $imageLarge = Cropper::make($request->image)->fit(500, 300, function ($c) {
            $c->upsize();
        })->encode('jpg', 90);

        // Store on S3
        Storage::disk('s3_image')->put($fileSmall, (string) $imageSmall);
        Storage::disk('s3_image')->put($fileLarge, (string) $imageLarge);

        return [
            'image_small' => $fileSmall,
            'image_large' => $fileLarge,
        ];
    })
    ->rules('required')
    ->disk('s3_image')
    ->hideFromIndex()
    ->prunable(),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM