Skip to content

Commit 2733074

Browse files
committed
worked version
1 parent c50ecb3 commit 2733074

27 files changed

+685
-21
lines changed

Imagable.php

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,88 @@
22

33
namespace bl\imagable;
44

5+
use bl\imagable\helpers\DirectoryHelper;
56
use yii\base\Component;
67

78
/**
89
* Description of Imagable
910
*
11+
* @method create($category, $path)
12+
*
13+
* @method get($category, $type, $name)
14+
* @method getThumb($category, $name)
15+
* @method getSmall($category, $name)
16+
* @method getBig($category, $name)
17+
* @method getOriginal($category, $name)
18+
*
1019
* @author RuslanSaiko
1120
*/
1221
class Imagable extends Component
1322
{
14-
15-
public $imagableClass = 'Imagine\Imagick\Imagine';
16-
public $categories = [];
23+
public $imageClass;
24+
public $categories;
1725
public $imagesPath = '@webroot/images';
1826
public $baseTemplate = [];
27+
public $nameClass = 'bl\imagable\name\OriginName';
28+
29+
public function behaviors()
30+
{
31+
return [
32+
'getImage' => [
33+
'class' => 'bl\imagable\behaviors\GetImageBehavior'
34+
],
35+
'createImage' => [
36+
'class' => 'bl\imagable\behaviors\CreateImageBehavior'
37+
]
38+
];
39+
}
1940

2041
public function init()
2142
{
43+
DirectoryHelper::create(\Yii::getAlias($this->imagesPath), true);
44+
$this->imagesPath = \Yii::getAlias($this->imagesPath);
45+
$this->registerDependencies();
46+
$this->initBaseTemplate();
2247
return parent::init();
2348
}
2449

25-
function createDirerctory($pathToDirectory, $chmod = 0777, $recursive = true)
50+
private function initBaseTemplate()
2651
{
27-
if (!is_dir($pathToDirectory)) {
28-
mkdir($pathToDirectory, $chmod, $recursive);
29-
return true;
30-
}
31-
return false;
52+
$this->baseTemplate = [
53+
'big' => [
54+
'width' => 500,
55+
'height' => 500
56+
],
57+
'small' => [
58+
'width' => 250,
59+
'height' => 250
60+
],
61+
'thumb' => [
62+
'width' => 100,
63+
'height' => 100
64+
],
65+
];
3266
}
3367

68+
private function registerDependencies()
69+
{
70+
\Yii::$container->set('bl\imagable\name\BaseName',
71+
$this->nameClass);
72+
73+
\Yii::$container->set('name', 'bl\imagable\name\BaseName');
74+
75+
\Yii::$container->set('bl\imagable\interfaces\CreateImageInterface',
76+
$this->imageClass);
77+
}
78+
79+
public function getTemplateSizeByCategory($template, $category)
80+
{
81+
if (key_exists($category, $this->categories)) {
82+
return $this->categories[$category][$template];
83+
} elseif (key_exists($category, $this->baseTemplate)) {
84+
return $this->baseTemplate[$category][$template];
85+
} else {
86+
throw new \Exception("Category with name '$category' does not exist");
87+
}
88+
}
3489
}

behaviors/CreateImageBehavior.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace bl\imagable\behaviors;
4+
5+
use bl\imagable\helpers\FileHelper;
6+
use bl\imagable\name\BaseName;
7+
use bl\imagable\helpers\DirectoryHelper;
8+
use bl\imagable\interfaces\CreateImageInterface;
9+
use yii\base\Behavior;
10+
11+
/**
12+
* Description of CreateImageBehavior
13+
*
14+
* @author RuslanSaiko
15+
*/
16+
class CreateImageBehavior extends Behavior
17+
{
18+
19+
/**
20+
* @var BaseHashName
21+
*/
22+
private $name = null;
23+
24+
/**
25+
* @var CreateImageInterface
26+
*/
27+
private $imageCreator = null;
28+
29+
/**
30+
* CreateImageBehavior constructor.
31+
* @param BaseName $name name generator
32+
* @param CreateImageInterface $imageCreator image resizer
33+
* @param array $config
34+
*/
35+
public function __construct(BaseName $name,
36+
CreateImageInterface $imageCreator, $config = array())
37+
{
38+
parent::__construct($config);
39+
40+
$this->name = $name;
41+
$this->imageCreator = $imageCreator;
42+
}
43+
44+
/**
45+
* @param $category string category name
46+
* @param $path string path to image
47+
* @return string return image name
48+
*/
49+
public function create($category, $path)
50+
{
51+
//path to image
52+
$saveImagePath = $this->owner->imagesPath;
53+
$categoriesParam = $this->owner->categories;
54+
$defaultCategoriesSize = $this->owner->baseTemplate;
55+
if (!array_key_exists($category, $categoriesParam['category'])) {
56+
throw new \UnexpectedValueException("$category not declare");
57+
}
58+
//new path to image
59+
$newPath = implode(DIRECTORY_SEPARATOR, [$saveImagePath, $category]);
60+
61+
//new image name created with class BaseName
62+
$imageName = $this->name->getName(FileHelper::getFileName($path));
63+
DirectoryHelper::create($newPath, true);
64+
//TODO:make size creator
65+
$image = '';
66+
67+
foreach ($categoriesParam['category'] as $category) {
68+
if (isset($category['origin']) && $category['origin']) {
69+
$image = $imageName . "-origin." . FileHelper::getFileType($path);
70+
copy($path, implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
71+
} elseif (isset($categoriesParam['origin']) && $categoriesParam['origin']) {
72+
$image = $imageName . "-origin." . FileHelper::getFileType($path);
73+
copy($path, implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
74+
}
75+
$arr = isset($category['size']) ? array_merge($defaultCategoriesSize, $category['size']) : $defaultCategoriesSize;
76+
foreach ($arr as $sizeName => $size) {
77+
$image = $imageName . "-$sizeName." . FileHelper::getFileType($path);
78+
$this->imageCreator->thumbnail($path, $size['width'], $size['height']);
79+
$this->imageCreator->save(implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
80+
// copy($path, implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
81+
}
82+
}
83+
84+
return $imageName;
85+
}
86+
87+
}

behaviors/GetImageBehavior.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace bl\imagable\behaviors;
4+
5+
use bl\imagable\helpers\FileHelper;
6+
use bl\imagable\Imagable;
7+
use yii\base\Behavior;
8+
use yii\helpers\StringHelper;
9+
use yii\web\NotFoundHttpException;
10+
11+
/**
12+
* Description of GetImageBehavior
13+
*
14+
* @author RuslanSaiko
15+
*/
16+
class GetImageBehavior extends Behavior
17+
{
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
public function init()
23+
{
24+
return parent::init();
25+
}
26+
27+
/**
28+
* Get the image in a $type size.
29+
* @param $category string image category
30+
* @param $type string image type. Example 'thumb'
31+
* @param $name string image name
32+
* @param $imageContent string image contnent
33+
* @return bool|string return name or false if image not exist
34+
*/
35+
public function get($category, $type, $name, &$imageContent = null)
36+
{
37+
/* Get the path to the folder with the images. */
38+
$directory = \Yii::getAlias($this->owner->imagesPath);
39+
$imagePath = implode(DIRECTORY_SEPARATOR, [$directory, $category]);
40+
$images = glob($imagePath . DIRECTORY_SEPARATOR . "$name-$type.*");
41+
if ($images === false) {
42+
return false;
43+
}
44+
/* Get the first matched file. */
45+
$image = $images[0];
46+
if (!is_null($imageContent)) {
47+
$resource = fopen($image);
48+
$imageContent = stream_get_contents($resource);
49+
fclose($resource);
50+
}
51+
return FileHelper::normalizePath($image);
52+
}
53+
54+
/**
55+
* Get the image in a thumb size.
56+
* @param $category string image category
57+
* @param $name string image name
58+
* @param $imageContent string image contnent
59+
* @return bool|string return name or false if image not exist
60+
*/
61+
public function getThumb($category, $name, &$imageContent = null)
62+
{
63+
return $this->get($category, 'thumb', $name, $imageContent);
64+
}
65+
66+
/**
67+
* Get the image in a small size.
68+
* @param $category string image category
69+
* @param $name string image name
70+
* @param $imageContent string image contnent
71+
* @return bool|string return name or false if image not exist
72+
*/
73+
public function getSmall($category, $name, &$imageContent = null)
74+
{
75+
return $this->get($category, 'small', $name, $imageContent);
76+
}
77+
78+
/**
79+
* Get the image in a big size.
80+
* @param $category string image category
81+
* @param $name string image name
82+
* @param $imageContent string image contnent
83+
* @return bool|string return name or false if image not exist
84+
*/
85+
public function getBig($category, $name, &$imageContent = null)
86+
{
87+
return $this->get($category, 'big', $name, $imageContent);
88+
}
89+
90+
/**
91+
* Get the image in a original size.
92+
* @param $category string image category
93+
* @param $name string image name
94+
* @param $imageContent string image contnent
95+
* @return bool|string return name or false if image not exist
96+
*/
97+
public function getOriginal($category, $name, &$imageContent = null)
98+
{
99+
return $this->get($category, 'origin', $name, $imageContent);
100+
}
101+
102+
}

chainImageThumb/ImageSizeChain.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace bl\imagable\chainImageThumb;
4+
5+
use bl\imagable\interfaces\CreateImageInterface;
6+
use yii\base\Object;
7+
8+
/**
9+
* Description of ImageMakeThumb
10+
*
11+
* @author RuslanSaiko
12+
*/
13+
abstract class ImageSizeChain extends Object
14+
{
15+
16+
/**
17+
*
18+
* @var CreateImageInterface
19+
*/
20+
protected $imageCreator;
21+
22+
public function __construct(CreateImageInterface $imageCreator,
23+
$config = [])
24+
{
25+
$this->imageCreator = $imageCreator;
26+
parent::__construct($config);
27+
}
28+
29+
/**
30+
*
31+
* @var ImageMakeThumb
32+
*/
33+
protected $next;
34+
35+
public function setNext(ImageMakeThumb $thumb)
36+
{
37+
$this->next = $thumb;
38+
return $thumb;
39+
}
40+
41+
protected abstract function resize($imagePath);
42+
43+
public function save($imagePath) {
44+
if (!is_null($this->next)) {
45+
$this->next->resize($imagePath);
46+
}
47+
}
48+
49+
}

chainImageThumb/ImageThumb.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace bl\imagable\chainImageThumb;
4+
5+
use bl\imagable\interfaces\CreateImageInterface;
6+
use yii\base\Object;
7+
8+
/**
9+
* Description of ImageThunb
10+
*
11+
* @author RuslanSaiko
12+
*/
13+
class ImageThumb extends Object
14+
{
15+
16+
/**
17+
* @param $imagePath
18+
* @throws \yii\base\InvalidConfigException
19+
*/
20+
public function create($imagePath)
21+
{
22+
/* @var ImageSizeChain */
23+
$original = \Yii::createObject('bl\imagable\chainImageThumb\responsibilities\OriginalSize');
24+
25+
/* @var ImageSizeChain */
26+
$big = \Yii::createObject('bl\imagable\chainImageThumb\responsibilities\BigSize');
27+
28+
/* @var ImageSizeChain */
29+
$small = \Yii::createObject('bl\imagable\chainImageThumb\responsibilities\SmallSize');
30+
31+
/* @var ImageSizeChain */
32+
$thumb = \Yii::createObject('bl\imagable\chainImageThumb\responsibilities\ThumbSize');
33+
34+
$original->setNext($big)->setNext($small)->setNext($thumb);
35+
}
36+
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ruslan
5+
* Date: 31.05.2016
6+
* Time: 14:56
7+
*/
8+
9+
namespace bl\imagable\chainImageThumb\responsibilities;
10+
11+
12+
use bl\imagable\chainImageThumb\ImageSizeChain;
13+
14+
class BigSize extends ImageSizeChain
15+
{
16+
17+
protected function resize($imagePath)
18+
{
19+
// TODO: Implement resize() method.
20+
}
21+
}

0 commit comments

Comments
 (0)