Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: CostaRico/yii2-images
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0.0
Choose a base ref
...
head repository: CostaRico/yii2-images
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
39 changes: 35 additions & 4 deletions Module.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
namespace rico\yii2images;


use rico\yii2images\models\PlaceHolder;
use yii;
use rico\yii2images\models\Image;

@@ -12,8 +13,18 @@ class Module extends \yii\base\Module

public $imagesCachePath = '@app/web/imgCache';

public $graphicsLibrary = 'GD';

public $controllerNamespace = 'rico\yii2images\controllers';

public $placeHolderPath;

public $waterMark = false;

public $className;

public $imageCompressionQuality = 85;


public function getImage($item, $dirtyAlias)
{
@@ -28,7 +39,13 @@ public function getImage($item, $dirtyAlias)


//Lets get image
$image = Image::find()
if(empty($this->className)) {
$imageQuery = Image::find();
} else {
$class = $this->className;
$imageQuery = $class::find();
}
$image = $imageQuery
->where([
'modelName' => $modelName,
'itemId' => $itemId,
@@ -41,12 +58,15 @@ public function getImage($item, $dirtyAlias)
':alias' => $alias
])*/
->one();
if(!$image){
return $this->getPlaceHolder();
}

return $image;
}

public function getStorePath()
{

return Yii::getAlias($this->imagesStorePath);
}

@@ -59,10 +79,12 @@ public function getCachePath()

public function getModelSubDir($model)
{
$modelName = $this->getShortClass($model);
$modelDir = $modelName . 's/' . $modelName . $model->id;

$modelName = $this->getShortClass($model);
$modelDir = \yii\helpers\Inflector::pluralize($modelName).'/'. $modelName . $model->getPrimaryKey();
return $modelDir;


}


@@ -157,4 +179,13 @@ public function init()
throw new \Exception('Setup imagesStorePath and imagesCachePath images module properties!!!');
// custom initialization code goes here
}

public function getPlaceHolder(){

if($this->placeHolderPath){
return new PlaceHolder();
}else{
return null;
}
}
}
6 changes: 6 additions & 0 deletions ModuleTrait.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
namespace rico\yii2images;


use yii\base\Exception;

trait ModuleTrait
{
/**
@@ -25,6 +27,10 @@ protected function getModule()
$this->_module = \Yii::$app->getModule('yii2images');
}

if(!$this->_module){
throw new Exception("\n\n\n\n\nYii2 images module not found, may be you didn't add it to your config?\n\n\n\n");
}

return $this->_module;
}
}
150 changes: 133 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
yii2-images
===========
Yii2-images is yii2 module that allow attach images to any your model.
Module requires Imagick library.
Guys, we definetly need to do something with this repo. I see several ways:
<ul>
<li>to write tests</li>
<li>to find active contributers</li>
<li>to find some alternative repo</li>
</ul>
What do you prefer? please let me know.

You can attach, remove, resize images,
set main image for model

For instance:

<pre>

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.

Module supports Imagick and GD libraries, you can set up it in module settings.


Usage instance:
-------------

```php
$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');
$model->attachImage('../../image2.png');

//to get all images
//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
@@ -25,17 +39,119 @@ foreach($images as $img){
//return url to proportionally resized image by height
echo $img->getUrl('x300');

//return url to resized and cropped image by width and height
//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);
}
</pre>

```

Details
-------------
1. Get images
```php
$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)

```
2. Remove image/images
```php
$model->removeImage($image); //you must to pass image (object)

$model->removeImages(); //will remove all images of this model
```

3. Set main image
```php
$model->attachImage($absolutePathToImage, true); //will attach image and make it main

foreach($model->getImages() as $img){
if($img->getPrimaryKey() == $ourId){
$model->setMainImage($img);//will set current image main
}
}
```

4. Get image sizes
```php
$image = $model->getImage();
$sizes = $image->getSizes(); // Array. Original image sizes
$sizes = $image->getSizesWhen('x500');
echo '&lt;img width="'.$sizes['width'].'" height="'.$sizes['height'].'" src="'.$image->getUrl('x500').'" />';
```

5. Get original image
```php
$img = $model->getImage();
echo $img->getPathToOrigin();
```


Installation
-------------
1. Add Yii2-images to the require section of your composer.json file:
<pre>
{
"require": {
"costa-rico/yii2-images": "dev-master"
}
}
</pre>
2. run
<pre>
php composer.phar update
</pre>

3. run migrate
<pre>
php yii migrate/up --migrationPath=@vendor/costa-rico/yii2-images/migrations
</pre>

4. setup module
```php
'modules' => [
'yii2images' => [
'class' => 'rico\yii2images\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' => 'images/store', //path to origin images
'imagesCachePath' => 'images/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.
],
],
```

5. attach behaviour to your model (be sure that your model has "id" property)
```php
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
```

Thats all!


Installation:
1) install from json
2) run migration
3) setup module
4) setup behaviour
5) check permissions
Loading