Image module for my dockerized Yii2 application.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist acid23m/yii2-image "~1.0"
or add
"acid23m/yii2-image": "~1.0"
to the require section of your composer.json
file.
Once the extension is installed, do next:
-
Storage path is not editable -
@root/userdata/images
. Adduserdata/
to .gitignore. -
Add module (recommended id is imagetool) in
backend/config/main.php
andfrontend/config/main.php
.
'modules' => [
'imagetool' => [
'class' => \imagetool\Module::class,
'controllerNamespace' => 'imagetool\controllers\web',
//'browser_cache_time' => 60,
//'etag' => true,
//'unset_cookie' => true,
]
]
// get full path to image
// output: /var/www/site/app/userdata/images/ad/c1/abc123cde456.jpg
echo imagetool\helpers\File::getPath('abc123cde456.jpg');
// get url to image
// output: https://site.com/image-data/abc123cde456.jpg
echo imagetool\helpers\File::getUrl('abc123cde456.jpg');
// get mime type of image in data-uri format
// output: image/png
echo imagetool\helpers\File::getMimeOfDataUri('data:image/png;base64,iVBORw0KG');
// get extension of image in data-uri format
// output: png
echo imagetool\helpers\File::getExtensionOfDataUri('data:image/png;base64,iVBORw0KG');
// delete image
// unlink abc123cde456.jpg, [email protected] and [email protected]
imagetool\helpers\File::delete('abc123cde456.jpg');
// output: <img />
echo imagetool\helpers\Html::img('abc123cde456.jpg', [
'class' => 'img-responsive',
'alt' => 'Company logo'
]);