Skip to content

Commit 71aa12a

Browse files
committed
Fix create image by category. Fix origin save. Add path to category in
BaseName
1 parent 873812b commit 71aa12a

File tree

12 files changed

+58
-44
lines changed

12 files changed

+58
-44
lines changed

BaseImagable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @method getOriginal($category, $name, &$imageContent = null)
1818
* @method delete($category, $name, &$imageContent = null)
1919
*
20-
* @author RuslanSaiko
20+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
2121
*/
2222
class BaseImagable extends Component
2323
{

Imagable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace bl\imagable;
33

44
/**
5-
*
5+
* @inheritdoc
66
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
77
*/
88
class Imagable extends BaseImagable

behaviors/CreateImageBehavior.php

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Description of CreateImageBehavior
1515
*
16-
* @author RuslanSaiko
16+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1717
*/
1818
class CreateImageBehavior extends Behavior
1919
{
@@ -57,46 +57,45 @@ public function attach($owner)
5757
*/
5858
public function create($category, $path)
5959
{
60-
//path to image
60+
// Path to image
6161
$saveImagePath = $this->owner->imagesPath;
62-
$categoriesParam = $this->owner->categories;
62+
63+
$categoryOptions = $this->owner->categories;
64+
6365
$defaultCategoriesSize = $this->owner->baseTemplate;
64-
if (!array_key_exists($category, $categoriesParam['category'])) {
65-
throw new \UnexpectedValueException("$category not declare");
66+
if (!array_key_exists($category, $categoryOptions['category'])) {
67+
throw new \UnexpectedValueException(" Category with name $category not specified.");
6668
}
67-
//new path to image
69+
// New path to image
6870
$newPath = implode(DIRECTORY_SEPARATOR, [$saveImagePath, $category]);
6971

70-
//new image name created with class BaseName
72+
// Specifies the full path to the category, for the derived class from BaseName
73+
$this->name->pathToCatory = $newPath;
74+
75+
// New image name created with class BaseName
7176
$imageName = $this->name->getName(FileHelper::getFileName($path));
7277
DirectoryHelper::create($newPath, true);
7378
$image = '';
74-
//TODO:refactor here!
75-
foreach ($categoriesParam['category'] as $category) {
76-
if (isset($category['origin']) && $category['origin']) {
77-
$image = $imageName . "-origin." . FileHelper::getFileType($path);
78-
list($width, $height, $type, $attr) = getimagesize($path);
79-
80-
$this->imageCreator->thumbnail($path, $width, $height);
81-
$this->imageCreator->save(implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
82-
83-
84-
} elseif (isset($categoriesParam['origin']) && $categoriesParam['origin']) {
85-
$image = $imageName . "-origin." . FileHelper::getFileType($path);
86-
87-
list($width, $height, $type, $attr) = getimagesize($path);
88-
89-
$this->imageCreator->thumbnail($path, $width, $height);
90-
$this->imageCreator->save(implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
91-
92-
}
79+
$categoryOption = $categoryOptions['category'][$category];
80+
81+
$categorySizes = $categoryOption['size'];
82+
if(empty($categorySizes)) {
83+
$categorySizes = $defaultCategoriesSize;
84+
}
85+
if ((isset($categoryOption['origin']) && $categoryOption['origin'])
86+
|| (isset($categoryOptions['origin']) && $categoryOptions['origin'])
87+
) {
88+
list($width, $height) = getimagesize($path);
89+
$categorySizes['origin'] = [
90+
'width' => $width,
91+
'height' => $height,
92+
];
93+
}
9394

94-
$arr = isset($category['size']) ? $category['size'] : $defaultCategoriesSize;
95-
foreach ($arr as $sizeName => $size) {
96-
$image = "$imageName-$sizeName." . FileHelper::getFileType($path);
97-
$this->imageCreator->thumbnail($path, $size['width'], $size['height']);
98-
$this->imageCreator->save(implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
99-
}
95+
foreach ($categorySizes as $sizeName => $size) {
96+
$image = "$imageName-$sizeName." . FileHelper::getFileType($path);
97+
$this->imageCreator->thumbnail($path, $size['width'], $size['height']);
98+
$this->imageCreator->save(implode(DIRECTORY_SEPARATOR, [$newPath, $image]));
10099
}
101100

102101
return $imageName;

behaviors/GetImageBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Description of GetImageBehavior
1414
*
15-
* @author RuslanSaiko
15+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1616
*/
1717
class GetImageBehavior extends Behavior
1818
{

chainImageThumb/ImageSizeChain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Description of ImageMakeThumb
1010
*
11-
* @author RuslanSaiko
11+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1212
*/
1313
abstract class ImageSizeChain extends Object
1414
{

helpers/FileHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Description of ImageHelper
1010
*
11-
* @author Ruslan
11+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1212
*/
1313
class FileHelper extends BaseFileHelper {
1414

helpers/base/BaseFileHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Description of BaseImageHelper
99
*
10-
* @author Ruslan
10+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1111
*/
1212
class BaseFileHelper extends \yii\helpers\BaseFileHelper {
1313
public static function fileInfo($filePath) {

instances/CreateImageImagine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* Description of CreateImageImagine
12-
* @author RuslanSaiko
12+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1313
*/
1414
class CreateImageImagine extends Object implements CreateImageInterface
1515
{

interfaces/CreateImageInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/**
77
* Interface CreateImageInterface
88
* @package bl\imagable\interfaces
9+
*
10+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
911
*/
1012
interface CreateImageInterface
1113
{

name/BaseName.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@
33
namespace bl\imagable\name;
44

55
/**
6-
* Description of BaseNameSignature
6+
* Base class for name generator
77
*
8-
* @author RuslanSaiko
8+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
99
*/
1010
abstract class BaseName extends \yii\base\Object
1111
{
12+
/**
13+
* @param $baseName string
14+
* @return string
15+
*/
1216
abstract public function generate($baseName);
13-
17+
18+
/**
19+
* @var string Full path to the category
20+
*/
21+
public $pathToCatory;
22+
23+
/**
24+
* @param $name string
25+
* @return string
26+
*/
1427
public function getName($name)
1528
{
1629
return $this->generate($name);

name/CRC32Name.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Description of CRC32Name
77
*
8-
* @author RuslanSaiko
8+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
99
*/
1010
class CRC32Name extends BaseName
1111
{

name/OriginName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Description of OriginName
1111
*
12-
* @author RuslanSaiko
12+
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
1313
*/
1414
class OriginName extends BaseName
1515
{

0 commit comments

Comments
 (0)