Intercept uploaded file #791
-
Hi, I need to intercept an uploaded attachment file in the Backend article edit page, so that I can optimize it if it is a PDF, before it is saved. Where is the best place in the code to apply this logic? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
<?php echo $form->field($model, 'attachments')->widget(
Upload::class,
[
'url' => ['/file/storage/upload'],
'sortable' => true,
'maxFileSize' => 10000000, // 10 MiB
'maxNumberOfFiles' => 10,
]
) ?> This is what appears in the That endpoint is a controller using the yii2-starter-kit/yii2-file-kit actions. So, in the file This way, you can add watermarks or modify files just after they are stored in the server. For example: public function actions(){
return [
'upload'=>[
'class'=>'trntv\filekit\actions\UploadAction',
//...
'on afterSave' => function($event) {
/* @var $file \League\Flysystem\File */
$file = $event->file;
// create new Intervention Image
$img = Intervention\Image\ImageManager::make($file->read());
// insert watermark at bottom-right corner with 10px offset
$img->insert('public/watermark.png', 'bottom-right', 10, 10);
// save image
$file->put($img->encode());
}
//...
]
];
} `` |
Beta Was this translation helpful? Give feedback.
This is what appears in the
_form.php
file for the articles module. You see that theattachmets
field has a configuration that send data to/file/storage/upload
endpoint.That endpoint is a controller using the yii2-starter-kit/yii2-file-kit actions.
So, in the file
backend/modules/file/controllers/StorageController.php
, in theupload
action configuration parameters, you can add theon afterSave
function signature.This way, you can add watermarks or …