dev-master
9999999-devCORNER CMS module images
BSD-3-Clause
The Requires
The Development Requires
- yiisoft/yii2-codeception *
- codeception/specify *
- codeception/verify *
- mikey179/vfsstream 1.3.*@dev
corner-cms
CORNER CMS module images
Guys, we definetly need to do something with this repo. I see several ways:, (*1)
What do you prefer? please let me know., (*2)
Yii2-images is yii2 module that allows to attach images to any of your models, next you can get images in any sizes, also you can set main image of images set., (*3)
Module supports Imagick and GD libraries, you can set up it in module settings., (*4)
$model = Model::findOne(12); //Model must have id //If an image is first it will be main image for this model $model->attachImage('../../image.png'); //But if you need set another image as main, use second arg $model->attachImage('../../image2.png', true); //get all images $images = $model->getImages(); foreach($images as $img){ //retun url to full image echo $img->getUrl(); //return url to proportionally resized image by width echo $img->getUrl('300x'); //return url to proportionally resized image by height echo $img->getUrl('x300'); //return url to resized and cropped (center) image by width and height echo $img->getUrl('200x300'); } //Returns main model image $image = $model->getImage(); if($image){ //get path to resized image echo $image->getPath('400x300'); //path to original image $image->getPathToOrigin(); //will remove this image and all cache files $model->removeImage($image); }
Get images, (*5)
$model->getImage(); //returns main image for model (first added image or setted as main) $model->getImages(); //returns array with images //If there is no images for model, above methods will return PlaceHolder images or null //If you want placeholder set up it in module configuration (see documentation)
Remove image/images, (*6)
$model->removeImage($image); //you must to pass image (object) $model->removeImages(); //will remove all images of this model
Set main image, (*7)
$model->attachImage($absolutePathToImage, true); //will attach image and make it main foreach($model->getImages() as $img){ if($img->id == $ourId){ $model->setMainImage($img);//will set current image main } }
Get image sizes, (*8)
$image = $model->getImage(); $sizes = $image->getSizes(); // Array. Original image sizes $sizes = $image->getSizesWhen('x500'); echo '<img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500').'" />';
Get original image, (*9)
$img = $model->getImage(); echo $img->getPathToOrigin();
{ "require": { "panix/mod-images": "dev-master" } }
php composer update
php yii migrate/up --migrationPath=@vendor/panix/mod-images/migrations
setup module, (*10)
'modules' => [ 'images' => [ 'class' => 'panix\mod\images\Module', //be sure, that permissions ok //if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions 'imagesStorePath' => 'uploads/store', //path to origin images 'imagesCachePath' => 'uploads/cache', //path to resized copies 'graphicsLibrary' => 'GD', //but really its better to use 'Imagick' 'placeHolderPath' => '@webroot/images/placeHolder.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias 'imageCompressionQuality' => 100, // Optional. Default value is 85. 'waterMark' => '@app/web/uploads/watermark.png' ], ],
attach behaviour to your model (be sure that your model has "id" property), (*11)
public function behaviors() { return [ 'image' => [ 'class' => 'panix\mod\images\behaviors\ImageBehavior', ] ]; }
Thats all!, (*12)
CORNER CMS module images
BSD-3-Clause
corner-cms