1

我有图像干预工作并推送到 S3,但我无法将文件名保存到表中。

这就是我到目前为止所拥有的:

// 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(),

除了最后一部分,所有工作,保存文件名。

4

1 回答 1

4

我想到了

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(),
于 2018-11-22T13:46:22.327 回答