You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nova-s3-multipart-upload Laravel Nova包上传至S3时,如何自动为视频或图片添加水印

Hey there! Let's figure out how to add automatic watermarks to your images and videos when using the nova-s3-multipart-upload package for Laravel Nova. Here's a practical, step-by-step approach that integrates smoothly into your workflow:

Step 1: Install Required Dependencies

First, we need tools to handle image and video processing:

  • For images: Use Intervention Image, a popular Laravel image manipulation library. Install it via Composer:

    composer require intervention/image
    

    Laravel 5.5+ auto-discovers the package, but if you're on an older version, add the service provider and facade to your config/app.php.

  • For videos: Use php-ffmpeg, which wraps FFmpeg for PHP. Install it with:

    composer require php-ffmpeg/php-ffmpeg
    

    Important: Ensure your server has FFmpeg installed (check with ffmpeg -v in the terminal) and note the path to the binaries for later configuration.

Step 2: Customize the Upload Logic in Your Nova Resource

The nova-s3-multipart-upload field allows you to override the default store method, which is where we'll inject our watermarking logic. In your Nova resource's fields method:

use Ahmedkandel\NovaS3MultipartUpload\S3MultipartUpload;
use Illuminate\Http\Request;

public function fields(Request $request)
{
    return [
        // Your other Nova fields...
        S3MultipartUpload::make('Media Files')
            ->disk('s3')
            ->acceptedTypes('image/*,video/*')
            ->store(function (Request $request, $model) {
                $uploadedPaths = [];
                $files = $request->file('media_files');

                foreach ($files as $file) {
                    // Process the file with watermark first
                    $processedFile = $this->addWatermark($file);
                    // Upload the processed file to S3
                    $path = $processedFile->store('uploads', 's3');
                    $uploadedPaths[] = $path;

                    // Optional: Clean up the temporary processed file
                    unlink($processedFile->getPathname());
                }

                return $uploadedPaths;
            }),
    ];
}
Step 3: Implement the Watermarking Method

Add a private method to your Nova resource to handle both image and video watermarking. This method takes the uploaded file, processes it, and returns the processed file for upload:

use Intervention\Image\Facades\Image;
use FFMpeg\FFMpeg;
use FFMpeg\Format\Video\X264;
use Illuminate\Http\File;

private function addWatermark($file)
{
    $mime = $file->getMimeType();
    $watermarkPath = storage_path('app/public/watermark.png'); // Update this to your watermark's path

    // Handle image files
    if (str_starts_with($mime, 'image/')) {
        $image = Image::make($file->getRealPath());
        $watermark = Image::make($watermarkPath);

        // Resize watermark to 15% of the image's width (adjust as needed)
        $watermark->resize($image->width() * 0.15, null, function ($constraint) {
            $constraint->aspectRatio();
        });

        // Place watermark in the bottom-right corner with 10px padding
        $image->insert($watermark, 'bottom-right', 10, 10);

        // Save processed image to a temporary file
        $tempFile = tempnam(sys_get_temp_dir(), 'watermarked_img_');
        $image->save($tempFile);

        return new File($tempFile);
    }

    // Handle video files
    if (str_starts_with($mime, 'video/')) {
        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => '/usr/bin/ffmpeg', // Update to your server's FFmpeg path
            'ffprobe.binaries' => '/usr/bin/ffprobe', // Update to your server's FFprobe path
        ]);

        $video = $ffmpeg->open($file->getRealPath());
        // Add watermark to bottom-right corner with 10px padding
        $video->filters()->watermark($watermarkPath, [
            'position' => 'relative',
            'bottom' => 10,
            'right' => 10,
        ]);

        // Save processed video to a temporary file
        $tempFile = tempnam(sys_get_temp_dir(), 'watermarked_vid_') . '.' . $file->getClientOriginalExtension();
        $video->save(new X264(), $tempFile);

        return new File($tempFile);
    }

    // Return original file if it's not an image/video
    return $file;
}
Key Notes to Keep in Mind
  • Watermark Preparation: Use a PNG watermark with transparency for best results, and place it in storage/app/public (run php artisan storage:link to make it accessible).
  • Server Configuration: For video processing, double-check FFmpeg paths and ensure your server has enough memory/CPU to handle large video files.
  • Upload Limits: Adjust PHP's upload_max_filesize, post_max_size, and memory_limit in your php.ini to accommodate large media files.
  • Temporary Files: The code above cleans up temporary processed files after upload, but you can also rely on your server's temp file cleanup mechanism if preferred.

内容的提问来源于stack exchange,提问作者Sargis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:12:49