使用Dropzone.js和Laravel 5.5为多个图像添加水印

问题描述 投票:0回答:1

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

但我需要的是在将这些图像保存到公共/图像目录之前为所有这些图像添加水印,因为这些图像将在前端显示为“预览”图像。

我找到了有关如何使用Intervention Image 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个回答
0
投票

我想到了。这就是我必须做的事情:

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
]);
© www.soinside.com 2019 - 2024. All rights reserved.