我有一个表单,用户可以使用Dropzone.js上传多个图像,然后将这些图像存储在数据库和公共/图像文件夹中。
但我需要的是在将这些图像保存到公共/图像目录之前为所有这些图像添加水印,因为这些图像将在前端显示为“预览”图像。
我找到了有关如何使用干预图像http://image-net.org/challenges/LSVRC/2014/browse-synsets添加水印的文档。
但是我无法弄清楚如何在我当前的设置中添加它。
这是我的脚本表格:
<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
答案 0 :(得分: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
]);