【问题标题】:Add watermarks to multiple images using Dropzone.js and Laravel 5.5使用 Dropzone.js 和 Laravel 5.5 为多个图像添加水印
【发布时间】:2018-01-15 02:15:53
【问题描述】:

我有一个表单,用户可以使用 Dropzone.js 上传多个图像,然后我将这些图像存储在数据库和 public/images 文件夹中。

但我需要为所有这些图像添加水印,然后再将它们保存在 public/images 目录中,因为这些图像将在前端显示为“预览”图像。

我找到了有关如何使用干预图像 here 添加水印的文档。

但我只是不知道如何在我当前的设置中添加它。

这是我的脚本表单:

 <div id="file-preview_images" class="dropzone"></div>

 <script>
    let dropPreview = new Dropzone('#file-preview_images', {
        url: '{{ route('upload.preview.store', $file) }}',
        headers: {
            'X-CSRF-TOKEN': document.head.querySelector('meta[name="csrf-token"]').content
        }
    });

    dropPreview.on('success', function(file, response) {
        file.id = response.id;
    });
</script>

$file 变量是当用户单击创建新文件时,它会在保存之前创建一个具有唯一标识符的新文件。一个文件可以有多次上传。

这是我的存储方法:

public function store(File $file, Request $request) {

        // Make sure the user owns the file before we store it in database.
        $this->authorize('touch', $file);

        // Get the file(s)
        $uploadedFile = $request->file('file');

        $upload = $this->storeUpload($file, $uploadedFile);

        $request->file( 'file' )->move(
            base_path() . '/public/images/previews/', $upload->filename
        );

        return response()->json([
            'id' => $upload->id
        ]);
}



protected function storeUpload(File $file, UploadedFile $uploadedFile) {

        // Make a new Upload model
        $upload = new Upload;

        // Fill the fields in the uploads table
        $upload->fill([
            'filename' => $uploadedFile->getClientOriginalName(),
            'size' => $uploadedFile->getSize(),
            'preview' => 1
        ]);

        // Associate this upload with a file.
        $upload->file()->associate($file);

        // Associate this upload with a user
        $upload->user()->associate(auth()->user());

        // Save the file
        $upload->save();

        return $upload;
}

所有这些都按预期工作,我只需要为这些图像中的每一个添加水印,但我遇到了麻烦。

我已经在public/images/shutterstock.png中保存了一张水印图片

【问题讨论】:

    标签: php dropzone.js laravel-5.5 watermark intervention


    【解决方案1】:

    我想通了。这是我必须做的:

    public function store(File $file, Request $request) {
    
            // Make sure the user owns the file before we store it in database.
            $this->authorize('touch', $file);
    
            // Get the file(s)
            $uploadedFile = $request->file('file');
    
            $upload = $this->storeUpload($file, $uploadedFile);
    
            // Get the image, and make it using Image Intervention
            $img = Image::make($request->file('file'));
    
            // Insert the image above with the watermarked image, and center the watermark
            $img->insert('images/home/shutterstock.png', 'center');
    
            // Save the image in the 'public/images/previews' directory
            $img->save(base_path() . '/public/images/gallery/pre/'.$upload->filename);      
    
            return response()->json([
                'id' => $upload->id
            ]);
        }
    

    在“storeUpload”方法上,也更改了“文件名”:

    $upload->fill([
        'filename' => $file->identifier.'-'.uniqid(10).$uploadedFile->getClientOriginalName(),
        'size' => $uploadedFile->getSize(),
        'preview' => 1
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2016-11-15
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多