We now want user to be able to upload a custom thumbnail for their videos.
1) Get the uploaded file, save ad redirect to the Update page.
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
public function actionUpdate($video_id)
{
$model = $this->findModel($video_id);
$model->thumbnail = UploadedFile::getInstanceByName('thumbnail');
if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
return $this->redirect(['update', 'video_id' => $model->video_id]);
}
return $this->render('update', [
'model' => $model,
]);
}
|
backend > controllers > VideoController.php
2) Save the thumbnail to storage, if there's any & add new label for form.
26
27
..
39
..
94
95
96
..
127
128
129
130
131
..
144
145
146
147
148
149
150
151
152
153
..
161
162
163
164
165
166
|
class Video extends \yii\db\ActiveRecord
{
...
public $thumbnail;
...
'updated_at' => 'Updated At',
'created_by' => 'Created By',
'thumbnail' => 'Thumbnail',
...
if ($this->thumbnail) {
$this->has_thumbnail = 1;
}
$saved = parent::save($runValidation, $attributeNames);
...
if ($this->thumbnail) {
$thumbnailPath = Yii::getAlias('@frontend/web/storage/thumbs/' . $this->video_id . '.jpg');
if (!is_dir(dirname($thumbnailPath))) {
FileHelper::createDirectory(dirname($thumbnailPath));
}
$this->thumbnail->saveAs($thumbnailPath);
}
return true;
}
...
// Return the video thumbnail link
public function getThumbnailLink()
{
return $this->has_thumbnail ? Yii::$app->params['frontendUrl'] . 'storage/thumbs/' . $this->video_id . '.jpg' : '';
}
}
|
common > models > Video.php
3) Create new input field and pull the thumbnail image from storage
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<?php $form = ActiveForm::begin([
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<div class="row mr-2">
<div class="col-sm-8">
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<div class="form-group">
<label><?php echo $model->getAttributeLabel('thumbnail') ?></label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="thumbnail" name="thumbnail">
<label class="custom-file-label" for="thumbnail">Choose file</label>
</div>
</div>
<?= $form->field($model, 'tags')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
</div>
<div class="col-sm-4 bg-light p-0">
<div class=>
<video class="embed-responsive" poster="<?php echo $model->getThumbnailLink() ?>"
src="<?php echo $model->getVideoLink() ?>" controls></video>
<div class="m-3">
<div class="text-muted ts-2">Video link</div>
<a href="<?php echo $model->getVideoLink() ?>">Open Video</a>
</div>
</div>
<div class="m-3">
<div class="text-muted">Filename</div>
<?php echo $model->video_name ?>
<?= $form->field($model, 'status')->textInput() ?>
</div>
</div>
<?php ActiveForm::end(); ?>
|
backend > views > video > _form.php
4) Displaying error message
We need to display an error message if user uploaded a file that is not an
image or if the image resolution is too small.
77
78
|
['status', 'default', 'value' => self::STATUS_UNLISTED], // set default status value
['thumbnail', 'image', 'minWidth' => 1280],
|
common > models > Video.php
19
20
21
|
<?php echo $form->errorSummary($model) ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
|
backend > views > video > _form.php
5) Resize the image
If user uploaded an image too large, resize it to 1280x720 or minimum width of
1280 (according to aspect ratio)
3
4
5
6
7
8
9
10
..
161
162
163
164
165
166 | namespace common\models;
use Imagine\Image\Box;
use Yii;
use yii\imagine\Image;
use yii\helpers\FileHelper;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
...
$this->thumbnail->saveAs($thumbnailPath);
Image::getImagine()
->open($thumbnailPath)
->thumbnail(new Box(1280, 720))
->save();
|
common > models > Video.php
No comments:
Post a Comment