-
Notifications
You must be signed in to change notification settings - Fork 2
/
SAImageDisplayer.php
278 lines (247 loc) · 7.83 KB
/
SAImageDisplayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
class SAImageDisplayer extends CWidget
{
const NONE = 1;
const AUTO = 2;
const HEIGHT = 3;
const WIDTH = 4;
/**
* The type of width and height we would like
* to display. The name and the sizes have to be
* defined in the config.
*/
public $size = "original";
/**
* Class of the href link
*/
public $class = "";
/**
* Id of the href link
*/
public $id = "";
/**
* To add some others attributes to the image
*/
public $othersAttributes = array();
/**
* Title of the href link
*/
public $title = null;
/**
* Alternative text of the href link
*/
public $alt = null;
/**
* Name of the image to display
*/
public $image = null;
/**
* Path to the dir where all the plugin will store
* and get the images.
* the root is "webroot"
*/
public $baseDir = "images";
/**
* Name of the folder keeping the originals images
*/
public $originalFolderName = "originals";
/**
* Name of the default image to display
* if the given image doesn't exist
*/
public $defaultImage = null;
/**
* All the possible sizes that the plugin can generate
* when not using groups
* Example:
* array(
* 'tiny' => array('width'=>40, 'height'=>30),
* 'big' => array('width'=>640, 'height'=>480),
* 'thumb' => array('width'=>400, 'height'=>300),
* ),
* will give the following structure:
* images/
* big/
* originals/
* thumb/
* tiny/
*
*/
public $sizes = array();
/**
* List of the groups of images. Each group will have its own sizes
* A folder will be created for each group containing all the different sizes
* Example:
* array(
* 'news' => array(
* 'tiny' => array('width'=>40, 'height'=>30),
* 'big' => array('width'=>640, 'height'=>480),
* ),
* 'reviews' => array(
* 'thumb' => array('width'=>400, 'height'=>300),
* ),
* ),
* will give the following structure:
* images/
* news/
* big/
* originals/
* tiny/
* reviews/
* originals/
* tiny/
*/
public $groups = array();
/**
* Name of the group.
* A group let the user separate images not belonging
* to the same type.
* When group is defined original images are stored in
* "baseDir/group/originalFolderName"
* For example if the group "news" is defined,
* The images will be stored in images/news/originals
*/
public $group = null;
/**
* Weither or not we need to display the image tag
* If false is selected then the image will be resized and
* all the other actions will be performed except displaying
* the image tag.
* @var boolean
*/
public $displayImage = true;
/**
* The master dimmension to resize the image.
* Options are : SAImageDisplayer::NONE, SAImageDisplayer::AUTO, SAImageDisplayer::HEIGHT, SAImageDisplayer::WIDTH
*/
public $resizeMode = SAImageDisplayer::AUTO;
protected $_originalFile;
protected $_src;
protected $_width;
protected $_height;
protected $_basePath;
protected $_imageFile;
public function init()
{
try {
$this->setBasePath();
if( $this->size !== 'original') {
$this->setWidthAndHeight();
$this->checkIfFolderExists();
$this->defineImageFile($this->size);
$this->createImagesIfNotExists();
$this->defineSrc($this->size);
} else {
$this->defineImageFile($this->originalFolderName);
$this->defineSrc($this->originalFolderName);
}
} catch(Exception $e) {
if (YII_DEBUG === false)
{
$this->displayImage = false;
}
}
}
public function run()
{
if($this->displayImage) {
$addAttributes = "";
foreach($this->othersAttributes as $name => $value) {
$addAttributes .= $name . '="' . $value . '" ';
}
echo '<img src="' . Yii::app()->baseUrl . '/' . $this->_src .
'" title="' . $this->getTitle() .
'" alt="' . $this->getAlt() .
'" id="' . $this->id .
'" width="' . $this->_width .
'" height="' . $this->_height .
'" class="' . $this->class . '" ' .
$addAttributes .
'/>';
}
}
/**
* Set the src to the file
*/
private function defineSrc($imageFolder)
{
$this->_src = $this->baseDir . '/'. $imageFolder . '/' . $this->image;
}
/**
* Define the image file that can be the one given to the widgets
* or the default one if the previous one doesn't exist.
* Throw an error if the image given doesn't exist and no default image is defined
*/
private function defineImageFile($imageFolder)
{
if (!$this->image) {
throw new Exception('Image cannot be null!');
}
$this->_originalFile = $this->_basePath . '/' . $this->originalFolderName . '/' . $this->image;
if(!file_exists($this->_originalFile) && $this->defaultImage !== null){
$this->image = $this->defaultImage;
$this->_originalFile = $this->_basePath . '/' . $this->originalFolderName . '/' . $this->image;
}
if (!file_exists($this->_originalFile)) {
throw new Exception($this->image . ' do not exists!');
}
$this->_imageFile = $this->_basePath . '/' . $imageFolder . '/' . $this->image;
}
/**
* Create the resized image if it doesn't exist
*/
private function createImagesIfNotExists()
{
if (!file_exists($this->_imageFile)) {
$image = Yii::app()->image->load($this->_originalFile);
$image->resize($this->_width, $this->_height, $this->resizeMode);
$image->save($this->_imageFile);
}
}
/**
* Check if the image is set and the folder exists or can be created
*/
private function checkIfFolderExists()
{
$path = $this->_basePath . '/' . $this->size;
if (!file_exists($path)) {
if(!mkdir($path, 0777, true)) {
throw new Exception($path . ' do not exists and can\'t be created!');
}
}
}
/**
* Check if the size setted exists and is valid
* Load the params from the conf or set the default values
*/
private function setBasePath()
{
if($this->group !== null) {
$this->baseDir = $this->baseDir . '/' . $this->group;
}
$this->_basePath = YiiBase::getPathOfAlias('webroot') . '/' . $this->baseDir;
}
private function setWidthAndHeight()
{
if($this->group !== null) {
$size =$this->groups[$this->group][$this->size];
} else {
$size = $this->sizes[$this->size];
}
if($size === null) {
throw new Exception($this->size . ' is not a valid size type!');
} elseif ($size['width'] === null) {
throw new Exception('The width is not defined for this size type!');
} elseif ($size['height'] === null) {
throw new Exception('The height is not defined for this size type!');
}
$this->_width = $size['width'];
$this->_height = $size['height'];
}
private function getTitle() {
return $this->title ? $this->title : $this->image;
}
private function getAlt() {
return $this->alt ? $this->alt : $this->image;
}
}