代码之家  ›  专栏  ›  技术社区  ›  David

使用Dropzone向多个图像添加水印。js和Laravel 5.5

  •  0
  • David  · 技术社区  · 7 年前

    我有一个表单,用户可以使用Dropzone上传多个图像。然后我将这些图像存储在数据库和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>
    

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

    以下是我的存储方法:

    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

    1 回复  |  直到 7 年前
        1
  •  0
  •   David    7 年前

    我想出来了。这就是我必须做的:

    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”方法中,也更改了“filename”:

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