1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.components.image
|
8
|
|
|
*
|
9
|
|
|
* Класс используется для работы с одним изображением модели и его миниатюр
|
10
|
|
|
*
|
11
|
|
|
* Examples:
|
12
|
|
|
*
|
13
|
|
|
* $image = new FSingleImage($model->img, 'product', array('pre'));
|
14
|
|
|
* echo $image;
|
15
|
|
|
* echo $image->pre;
|
16
|
|
|
*/
|
17
|
|
|
class FSingleImage implements ImageInterface
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* Изображение, отображаемое при отсутствии файла
|
21
|
|
|
*
|
22
|
|
|
* @var string
|
23
|
|
|
*/
|
24
|
|
|
protected $defaultImage = '/i/sp.gif';
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* Массив доступных префиксов для файла
|
28
|
|
|
*
|
29
|
|
|
* @var array
|
30
|
|
|
*/
|
31
|
|
|
protected $availableTypes = array();
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* Оригинальное имя файла
|
35
|
|
|
*
|
36
|
|
|
* @var string
|
37
|
|
|
*/
|
38
|
|
|
protected $name;
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Название директории в папке с изображениями
|
42
|
|
|
*
|
43
|
|
|
* @var string
|
44
|
|
|
*/
|
45
|
|
|
protected $path;
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* Путь к папке с изображениями относительно корня проекта
|
49
|
|
|
*
|
50
|
|
|
* @var string
|
51
|
|
|
*/
|
52
|
|
|
protected $imageDir = 'f/';
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
*
|
56
|
|
|
* @param string $name
|
57
|
|
|
* @param string $path
|
58
|
|
|
* @param array $types
|
59
|
|
|
* @param string $defaultImage
|
60
|
|
|
*/
|
61
|
6 |
|
public function __construct($name, $path, array $types = array(), $defaultImage = '/i/sp.gif')
|
62
|
|
|
{
|
63
|
6 |
|
$this->name = $name;
|
64
|
6 |
|
$this->path = $path;
|
65
|
6 |
|
$this->availableTypes = $types;
|
66
|
6 |
|
$this->defaultImage = $defaultImage;
|
67
|
6 |
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* Получение неоригинального изображения
|
71
|
|
|
* Допустимые типы находятся в $this->availableTypes
|
72
|
|
|
* Если файл не совпадает с доступными типами, вызывается магия Yii
|
73
|
|
|
* Если файл не существует, отдается defaultImage
|
74
|
|
|
*
|
75
|
|
|
* @param string $name
|
76
|
|
|
*
|
77
|
|
|
* @return string
|
78
|
|
|
* @throws CException
|
79
|
|
|
*/
|
80
|
3 |
View Code Duplication |
public function __get($name)
|
|
|
|
|
81
|
|
|
{
|
82
|
3 |
|
if( in_array($name, $this->availableTypes) )
|
83
|
3 |
|
{
|
84
|
2 |
|
if( file_exists($this->getFullPath($name)) )
|
85
|
2 |
|
{
|
86
|
1 |
|
return '/' . $this->getFullPath($name);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
1 |
|
return $this->defaultImage;
|
90
|
|
|
}
|
91
|
|
|
else
|
92
|
|
|
{
|
93
|
1 |
|
throw new CException('Запрашиваемое изображение не существует');
|
94
|
|
|
}
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* По умолчанию отдаётся оригинальный файл
|
99
|
|
|
*
|
100
|
|
|
* @return string
|
101
|
|
|
*/
|
102
|
2 |
View Code Duplication |
public function __toString()
|
|
|
|
|
103
|
|
|
{
|
104
|
2 |
|
if( !empty($this->name) && file_exists($this->getFullPath()) )
|
105
|
2 |
|
{
|
106
|
1 |
|
return '/' . $this->getFullPath();
|
107
|
|
|
}
|
108
|
|
|
else
|
109
|
|
|
{
|
110
|
1 |
|
return $this->defaultImage;
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
|
114
|
|
|
/**
|
115
|
|
|
* Создание пути для файла
|
116
|
|
|
*
|
117
|
|
|
* @return string
|
118
|
|
|
*/
|
119
|
2 |
|
public function getPath()
|
120
|
|
|
{
|
121
|
2 |
|
return $this->imageDir . $this->path . '/';
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
/**
|
125
|
|
|
* @param $imageDir
|
126
|
|
|
*/
|
127
|
1 |
|
public function setImageDir($imageDir)
|
128
|
|
|
{
|
129
|
1 |
|
$this->imageDir = $imageDir;
|
130
|
1 |
|
}
|
131
|
|
|
|
132
|
|
|
public function isNotEmpty()
|
133
|
|
|
{
|
134
|
|
|
return !empty($this->name);
|
135
|
|
|
}
|
136
|
|
|
|
137
|
|
|
/**
|
138
|
|
|
* Создание полного пути до файла
|
139
|
|
|
*
|
140
|
|
|
* @param string $name
|
141
|
|
|
*
|
142
|
|
|
* @return string
|
143
|
|
|
*/
|
144
|
2 |
View Code Duplication |
protected function getFullPath($name = null)
|
|
|
|
|
145
|
|
|
{
|
146
|
2 |
|
if( empty($name) )
|
147
|
2 |
|
{
|
148
|
2 |
|
return $this->getPath() . $this->name;
|
149
|
|
|
}
|
150
|
|
|
else
|
151
|
|
|
{
|
152
|
2 |
|
return $this->getPath() . $name . '_' . $this->name;
|
153
|
|
|
}
|
154
|
|
|
}
|
155
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.