{"id":23346155,"url":"https://github.com/tpmanc/yii2-file-behavior","last_synced_at":"2025-04-10T03:43:31.307Z","repository":{"id":35637359,"uuid":"39911651","full_name":"tpmanc/yii2-file-behavior","owner":"tpmanc","description":"Yii 2 file uploading","archived":false,"fork":false,"pushed_at":"2021-09-23T19:37:48.000Z","size":42,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T05:01:49.977Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tpmanc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-29T19:16:24.000Z","updated_at":"2021-09-23T19:35:33.000Z","dependencies_parsed_at":"2022-09-18T06:55:47.681Z","dependency_job_id":null,"html_url":"https://github.com/tpmanc/yii2-file-behavior","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpmanc%2Fyii2-file-behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpmanc%2Fyii2-file-behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpmanc%2Fyii2-file-behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpmanc%2Fyii2-file-behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tpmanc","download_url":"https://codeload.github.com/tpmanc/yii2-file-behavior/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248154965,"owners_count":21056541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-21T07:12:57.172Z","updated_at":"2025-04-10T03:43:31.281Z","avatar_url":"https://github.com/tpmanc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# yii2-file-behavior\nYii 2 file uploading\n\n## Install via Composer\n\nRun the following command\n\n```bash\n$ composer require tpmanc/yii2-file-behavior \"*\"\n```\n\nor add\n\n```bash\n$ \"tpmanc/yii2-file-behavior\": \"*\"\n```\n\nto the require section of your `composer.json` file.\n\n## Migrations\n\nCreate migration by following command\n\n```bash\n$ yii migrate/create images\n```\n\nOpen the `/path/to/migrations/m_xxxxxx_xxxxxx_images.php` file \nand add following code to `up()` method\n\n\n```php\n        $this-\u003ecreateTable('image', [\n            'id' =\u003e Schema::TYPE_PK,\n            'itemId' =\u003e Schema::TYPE_INTEGER . ' NOT NULL',\n            'order' =\u003e Schema::TYPE_INTEGER . ' NOT NULL',\n            'extension' =\u003e Schema::TYPE_STRING . '(10) NOT NULL',\n            'hash' =\u003e Schema::TYPE_STRING . '(32) NOT NULL',\n        ]);\n\n        $this-\u003ecreateTable('imageSize', [\n            'id' =\u003e Schema::TYPE_PK,\n            'imageId' =\u003e Schema::TYPE_INTEGER . ' NOT NULL',\n            'path' =\u003e Schema::TYPE_STRING . '(255) NOT NULL',\n            'size' =\u003e Schema::TYPE_STRING . '(255) NOT NULL',\n        ]);\n```\n\n## Create model\n\nGenerate Active Record model for new `image` and `imageSize` tables\n\n## Configuring\n\nAttach the behavior to your model class:\n\n```php\nuse tpmanc\\filebehavior\\ImageBehavior;\n\n\\\\ ...\n\n    public $file;\n\n    public function behaviors()\n    {\n        return [\n            'ImageBehavior' =\u003e [\n                'class' =\u003e ImageBehavior::className(),\n                'imageModel' =\u003e 'models\\Image',\n                'imageSizeModel' =\u003e 'models\\ImageSize',\n                'imageVariable' =\u003e 'file',\n                'imageFolder' =\u003e '@upload',\n                'webImageFolder' =\u003e '@webupload',\n                'noImagePath' =\u003e '@webupload/no-image.png',\n            ],\n        ];\n    }\n\n    public function rules()\n    {\n        ['file', 'file', 'extensions' =\u003e ['png', 'jpg'], 'maxSize' =\u003e 1024*1024*1024, 'maxFiles' =\u003e 4],\n    }\n\n```\n\nIf file hash will be like \"6e3c797abee0ff2803ef1f952f187d2f\" \nthe file will be located in `@upload/images/6e/3c/{id from image table}.jpg`\n\nTo save several sizes of image add:\n\n```php\n    public $file;\n\n    public function behaviors()\n    {\n        return [\n            'ImageBehavior' =\u003e [\n                'class' =\u003e ImageBehavior::className(),\n                'fileModel' =\u003e 'models\\Image',\n                'fileVar' =\u003e 'file',\n                'fileFolder' =\u003e '@upload/images',\n                'imageSizes' =\u003e [\n                    'original' =\u003e [\n                        'folder' =\u003e 'original',\n                    ],\n                    'big' =\u003e [\n                        'width' =\u003e 800,\n                        'height' =\u003e 600,\n                        'folder' =\u003e 'big',\n                    ],\n                    'small' =\u003e [\n                        'width' =\u003e 64,\n                        'height' =\u003e 64,\n                        'folder' =\u003e 'small',\n                    ],\n                ],\n            ],\n        ];\n    }\n\n    public function rules()\n    {\n        ['file', 'file', 'extensions' =\u003e ['png', 'jpg'], 'maxSize' =\u003e 1024*1024*1024, 'maxFiles' =\u003e 1],\n    }\n```\n\nIf file hash will be like \"6e3c797abee0ff2803ef1f952f187d2f\" - result 3 images:\n\n- `@upload/images/original/6e/3c/{id from image table}.jpg`\n\n- `@upload/images/big/6e/3c/{id from image table}.jpg`\n\n- `@upload/images/small/6e/3c/{id from image table}.jpg`\n\n## View file\n\nExample of view file\n\n```html\n\u003c?php $form = ActiveForm::begin(['options' =\u003e ['enctype' =\u003e 'multipart/form-data']]) ?\u003e\n    \u003c?= $form-\u003efield($model, 'file[]')-\u003efileInput(['multiple' =\u003e true, 'accept' =\u003e 'image/*']) ?\u003e\n    \u003cdiv class=\"form-group\"\u003e\n        \u003c?= Html::submitButton($model-\u003eisNewRecord ? 'Create' : 'Update', ['class' =\u003e $model-\u003eisNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?\u003e\n    \u003c/div\u003e\n\u003c?php ActiveForm::end(); ?\u003e\n```  \n\n## Geting images\n\nGet single image:\n\n```html\n\u003cimg src=\"\u003c?= $model-\u003egetImage('original') ?\u003e\" alt=\"\"\u003e\n\u003cimg src=\"\u003c?= $model-\u003egetImage('big') ?\u003e\" alt=\"\"\u003e\n\u003cimg src=\"\u003c?= $model-\u003egetImage('small') ?\u003e\" alt=\"\"\u003e\n```\n\nGet all images:\n\n```html\n\u003c?php foreach ($model-\u003egetImages('original') as $image) { ?\u003e\n    \u003cimg src=\"\u003c?= $image ?\u003e\" alt=\"\"\u003e\n\u003c?php } ?\u003e\n```\n\nGet `count` images:\n\n```html\n\u003c?php $count = 5;?\u003e\n...\n\u003c?php foreach ($model-\u003egetImages('original', $count) as $image) { ?\u003e\n    \u003cimg src=\"\u003c?= $image ?\u003e\" alt=\"\"\u003e\n\u003c?php } ?\u003e\n```\n\nReturn `false` when image does not exist:\n\nFor single image:\n\n```php\nif ($model-\u003egetImage('original', true) === false) {\n    ...\n}\n```\n\nFor all images:\n\n```php\n$count = false;\nforeach ($model-\u003egetImages('original', $count, true) as $image) {\n    if ($image === false) {\n        ...\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpmanc%2Fyii2-file-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftpmanc%2Fyii2-file-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpmanc%2Fyii2-file-behavior/lists"}