PHP code example of besnovatyj / yii2-cms-upload
1. Go to this page and download the library: Download besnovatyj/yii2-cms-upload library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
besnovatyj / yii2-cms-upload example snippets
use common\components\upload\behaviors\ImageUploadBehavior;
public function behaviors(): array
{
return [
[
'class' => ImageUploadBehavior::class,
'attribute' => 'photo',
'filePath' => '@static/origin/users/[[attribute_user_id]]/[[id]].[[extension]]',
'fileUrl' => '@staticHostName/origin/users/[[attribute_user_id]]/[[id]].[[extension]]',
'thumbPath' => '@static/cache/users/[[attribute_user_id]]/[[profile]]_[[id]].[[extension]]',
'thumbUrl' => '@staticHostName/cache/users/[[attribute_user_id]]/[[profile]]_[[id]].[[extension]]',
'thumbs' => [
'admin' => ['width' => 100, 'height' => 70],
'thumb' => ['width' => 370, 'height' => 370],
],
],
...parent::behaviors(),
];
}
use Besnovatyj\Upload\heap\UploadBehavior;
use Besnovatyj\Upload\heap\ThumbnailProfile;
public function behaviors(): array
{
return [
'photoUpload' => [
'class' => UploadBehavior::class,
'attribute' => 'photo',
'pathTemplate' => 'origin/users/{attr.user_id}/{pk}.{extension}',
'thumbnails' => [
new ThumbnailProfile('admin', width: 100, height: 70),
new ThumbnailProfile('thumb', width: 370, height: 370),
],
'thumbPathTemplate' => 'cache/users/{attr.user_id}/{profile}_{pk}.{extension}',
],
...parent::behaviors(),
];
}
// Было:
$model->getImageFileUrl('photo');
$model->getThumbFileUrl('photo', 'admin');
$model->getUploadedFileUrl('photo');
$model->getUploadedFilePath('photo');
// Стало:
$model->getUploadUrl('photo');
$model->getThumbUrl('photo', 'admin');
$model->getUploadPath('photo');
$model->getThumbPath('photo', 'admin');
$model->getUploadUrl('photo', '/images/no-photo.png');
$model->getThumbUrl('photo', 'admin', '/images/no-photo-sm.png');
// Файл документа
'pathTemplate' => 'documents/{attr.user_id}/{pk}.{extension}'
// → documents/5/42.pdf
// Изображение с превью
'pathTemplate' => 'origin/gallery/{attr.gallery_id}/{pk}.{extension}'
'thumbPathTemplate' => 'cache/gallery/{attr.gallery_id}/{profile}_{pk}.{extension}'
// → origin/gallery/10/42.jpg
// → cache/gallery/10/admin_42.jpg
[
'class' => UploadBehavior::class,
'attribute' => 'document',
'pathTemplate' => 'documents/{attr.user_id}/{pk}.{extension}',
]
use Besnovatyj\Upload\heap\ThumbnailProfile;
use Besnovatyj\Upload\heap\ThumbnailMode;
[
'class' => UploadBehavior::class,
'attribute' => 'photo',
'pathTemplate' => 'origin/users/{attr.user_id}/{pk}.{extension}',
'generateName' => true, // уникальные имена
'thumbnails' => [
new ThumbnailProfile('small', width: 100, height: 100, mode: ThumbnailMode::Crop),
new ThumbnailProfile('medium', width: 400, height: 300),
new ThumbnailProfile('large', width: 800, height: 600, quality: 90),
],
'thumbPathTemplate' => 'cache/users/{attr.user_id}/{profile}_{pk}.{extension}',
]
'thumbnails' => [
// формат оригинала (как раньше)
new ThumbnailProfile('admin', width: 100, height: 70),
// современный формат отдачи: энкодер + {extension} в пути превью станут webp
new ThumbnailProfile('thumb', width: 370, height: 370, format: 'webp'),
new ThumbnailProfile('hero', width: 1600, height: 900, format: 'avif', quality: 60),
],
[
'class' => UploadBehavior::class,
'attribute' => 'avatar',
'pathTemplate' => 'avatars/{pk}.{extension}',
'generateName' => fn(\yii\web\UploadedFile $file) => 'avatar_' . time(),
'deleteOnEmpty' => true, // удалять файл если атрибут очищен
]
use Besnovatyj\Upload\heap\UrlDownloader;
$downloader = new UrlDownloader(timeout: 60);
$result = $downloader->download(
url: 'https://example.com/photo.jpg',
allowedExtensions: ['jpg', 'png', 'webp'],
maxSize: 5 * 1024 * 1024, // 5 MB
);
// Использование результата в модели
$model->photo = /* создать UploadedFile из $result->tempPath */;
$model->save();
$result->cleanup(); // удалить временный файл
bash
grep -r "common\\\\components\\\\upload\\\\behaviors" app/ --="*.php"
grep -r "getImageFileUrl\|getThumbFileUrl\|getUploadedFileUrl\|getUploadedFilePath" app/ --