-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slice.php
468 lines (404 loc) · 15.9 KB
/
Slice.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/**
* RokGallery_Model_Slice
*
* This class has been auto-generated by the Doctrine ORM Framework
*
* @package RokGallery
* @subpackage models
* @author RocketTheme LLC <support@rockettheme.com>
* @version SVN: $Id: Slice.php 39573 2011-07-06 09:59:45Z btowles $
*/
class RokGallery_Model_Slice extends RokGallery_Model_Base_Slice
{
/**
* @param Doctrine_Event $event
*/
public function preDelete(Doctrine_Event $event)
{
RokGallery_Model_SliceFilter::cleanThumbInfo();
// Add slice to be removed
RokGallery_Queue_FileDelete::add($this->getFullPath());
// Add thumb to be removed
RokGallery_Queue_FileDelete::add($this->getThumbFullPath());
// Remove the admin thumb
RokGallery_Queue_FileDelete::add($this->getThumbFullPath('admin-thumb'));
// Remove the admin thumb
RokGallery_Queue_FileDelete::add($this->getThumbFullPath('mini-admin-thumb'));
}
public function postDelete(Doctrine_Event $event)
{
//Make sure all linked tables are clean
//RokGallery_Model_SliceTags
$q = Doctrine_Query::create()
->delete('RokGallery_Model_SliceTags st')
->andWhere('st.slice_id NOT IN (SELECT s.id from RokGallery_Model_Slice s)');
$q->execute();
$q->free();
//rokgallery_slices_index
$conn =& Doctrine_Manager::connection();
$dbh =& $conn->getDbh();
$stmt = $dbh->prepare('delete from ' . RokCommon_Doctrine::getPlatformInstance()->setTableName('rokgallery_slices_index')
. ' where id NOT IN (SELECT s.id from ' . RokGallery_Model_SliceTable::getInstance()->getTableName() . ' s)');
$stmt->execute();
}
/**
* @param Doctrine_Event $event
*/
public function preUpdate(Doctrine_Event $event)
{
RokGallery_Model_SliceFilter::cleanThumbInfo();
$record = $event->getInvoker();
$modified = $record->getModified();
$original = $record->getModified(true);
// Dont let a direct change from the admin thumb happen
if (array_key_exists('admin_thumb', $modified)
&& $modified['admin_thumb'] == false
&& $original['admin_thumb'] == true
) {
$this->admin_thumb = true;
}
if (empty($this->title)) {
$this->title = $this->File->title;
}
if (empty($this->caption)) {
$this->caption = $this->File->description;
}
if (array_key_exists('manipulations', $modified)) {
$this->processImages();
}
// set background color to transparent
if (empty($this->thumb_background))
{
$this->thumb_background = 'transparent';
}
}
/**
* @param Doctrine_Event $event
*/
public function preInsert(Doctrine_Event $event)
{
RokGallery_Model_SliceFilter::cleanThumbInfo();
if (empty($this->title)) {
$this->title = $this->File->title;
}
if (empty($this->caption)) {
$this->caption = $this->File->description;
}
// set background color to transparent
if (empty($this->thumb_background))
{
$this->thumb_background = 'transparent';
}
$this->processImages();
}
/**
*/
protected function processImages()
{
// create the actual slice image with manipulations
$this->applyManipulations();
// create the thumbnails
$this->generateAdminThumbnail();
$this->generateMiniAdminThumbnail();
$this->generateDefaultThumbnail();
}
/**
* @param RokGallery_Model_File $file
* @param string $title
* @param string|null $caption
* @param bool $publish
* @param bool $admin_thumb
* @return RokGallery_Model_Slice
*/
public static function &createNew(RokGallery_Model_File &$file, $title = null, $caption = null, $publish = false, $admin_thumb = false)
{
$slice = new RokGallery_Model_Slice();
$slice->guid = RokGallery_Helper::createUUID();
$slice->title = $title;
$slice->caption = $caption;
$slice->admin_thumb = $admin_thumb;
$slice->published = $publish;
$file->Slices[] = $slice;
return $slice;
}
/**
* Applies an array of RokGallery_Manipulation_Action objects to a slice and saves the resulting image as the slice
* These manipulations are applied in order.
*
* @param array $manipulations
*/
public function applyManipulations()
{
$slice_image_path = $this->getFullPath();
// Get the base image
RokGallery_Memory::adjustLimitForImage($this->File->xsize, $this->File->ysize);
$start_image = WideImage::loadFromFile($this->File->getFullPath());
$changed_image =& $start_image;
$working_image =& $changed_image;
// Apply the changes to the image
foreach ($this->manipulations as $change)
{
// make sure we have a working image
if (!isset($working_image)) $working_image =& $changed_image;
// Get the base image
RokGallery_Memory::adjustLimitForImage($working_image->getWidth(), $working_image->getHeight());
// apply the change to the working image
$changed_image =& $change->apply($working_image);
// free the working image
$working_image->destroy();
unset($working_image);
}
$changed_image->saveToFile($slice_image_path, RokGallery_Helper::getImageQuality($this->File->type));
RokGallery_Queue_FileCreate::add($slice_image_path);
// record slice info
$this->xsize = $changed_image->getWidth();
$this->ysize = $changed_image->getHeight();
$this->filesize = @filesize($slice_image_path);
// free the working image
$changed_image->destroy();
unset($changed_image);
}
public function generateDefaultThumbnail()
{
if ($this->gallery_id != null) {
$this->generateThumbnail($this->Gallery->thumb_xsize, $this->Gallery->thumb_ysize, $this->Gallery->thumb_keep_aspect, $this->Gallery->thumb_background);
$this->thumb_xsize = $this->Gallery->thumb_xsize;
$this->thumb_ysize = $this->Gallery->thumb_ysize;
$this->thumb_keep_aspect = $this->Gallery->thumb_keep_aspect;
$this->thumb_background = $this->Gallery->thumb_background;
}
else
{
$xsize = $this->thumb_xsize;
$ysize = $this->thumb_ysize;
$keep = $this->thumb_keep_aspect;
$background = $this->thumb_background;
if (($xsize == null || $xsize == 0) && ($ysize == null || $ysize == 0))
{
$xsize = RokGallery_Config::getOption(RokGallery_Config::OPTION_DEFAULT_THUMB_XSIZE, RokGallery_Config::DEFAULT_DEFAULT_THUMB_XSIZE);
$ysize = RokGallery_Config::getOption(RokGallery_Config::OPTION_DEFAULT_THUMB_YSIZE, RokGallery_Config::DEFAULT_DEFAULT_THUMB_YSIZE);
$keep = RokGallery_Config::getOption(RokGallery_Config::OPTION_DEFAULT_THUMB_KEEP_ASPECT, RokGallery_Config::DEFAULT_DEFAULT_THUMB_KEEP_ASPECT);
$background = RokGallery_Config::getOption(RokGallery_Config::OPTION_DEFAULT_THUMB_BACKGROUND, RokGallery_Config::DEFAULT_DEFAULT_THUMB_BACKGROUND);
$this->thumb_xsize = $xsize;
$this->thumb_ysize = $ysize;
$this->thumb_keep_aspect = $keep;
$this->thumb_background = $background;
}
$this->generateThumbnail($xsize, $ysize, $keep, $background);
}
}
/**
*/
public function generateAdminThumbnail()
{
$this->generateThumbnail(RokGallery_Config::DEFAULT_ADMIN_THUMB_XSIZE, RokGallery_Config::DEFAULT_ADMIN_THUMB_YSIZE, true, RokGallery_Config::getOption(RokGallery_Config::OPTION_ADMIN_THUMB_BACKGROUND), "admin-thumb");
}
public function generateMiniAdminThumbnail()
{
$this->generateThumbnail(RokGallery_Config::DEFAULT_MINI_ADMIN_THUMB_XSIZE, RokGallery_Config::DEFAULT_MINI_ADMIN_THUMB_YSIZE, true, RokGallery_Config::getOption(RokGallery_Config::OPTION_ADMIN_THUMB_BACKGROUND), "mini-admin-thumb");
}
/**
* Generates a thumbnail image for a slice
*
* @param $thumb_x
* @param $thumb_y
* @param bool $keep_aspect
* @param null $background_color
* @param string $extension
*
*/
public function generateThumbnail($thumb_x, $thumb_y, $keep_aspect = true, $background_color = null, $extension = "thumb")
{
$slices_dir = dirname($this->File->getFullPath());
$thumb_path = $this->getThumbFullPath($extension);
// get the slice image
RokGallery_Memory::adjustLimitForImage($this->xsize, $this->ysize);
$slice_image = WideImage::loadFromFile($this->getFullPath());
if ($background_color == 'transparent' || empty($background_color)){
$background_color = null;
}
// if he image size is smaller then the thumbnail size
// resize the canvas with optioned color and place in center
if ($this->xsize < $thumb_x && $this->ysize < $thumb_y) {
// Resize canvas
RokGallery_Memory::adjustLimitForImage($thumb_x, $thumb_y);
$thumb_image = $slice_image->resizeCanvas($thumb_x, $thumb_y, 'center', 'center',
($background_color == null) ? $background_color
: $slice_image->allocateColor(RokGallery_Helper::html2rgb($background_color)));
$slice_image->destroy();
unset($slice_image);
// write out image
$thumb_image->saveToFile($thumb_path, RokGallery_Helper::getImageQuality($this->File->type));
RokGallery_Queue_FileCreate::add($thumb_path);
$thumb_image->destroy();
unset($thumb_image);
}
else
{
if ($keep_aspect) {
// Keep the aspect Ratio
RokGallery_Memory::adjustLimitForImage($thumb_x, $thumb_y);
$thumb_image_after_resize = $slice_image->resize($thumb_x, $thumb_y);
$slice_image->destroy();
unset($slice_image);
// fill in blank space
RokGallery_Memory::adjustLimitForImage($thumb_x, $thumb_y);
$thumb_image = $thumb_image_after_resize->resizeCanvas($thumb_x, $thumb_y, 'center', 'center',
($background_color == null) ? $background_color
: $thumb_image_after_resize->allocateColor(RokGallery_Helper::html2rgb($background_color)));
$thumb_image_after_resize->destroy();
unset($thumb_image_after_resize);
// write out image
$thumb_image->saveToFile($thumb_path, RokGallery_Helper::getImageQuality($this->File->type));
RokGallery_Queue_FileCreate::add($thumb_path);
$thumb_image->destroy();
unset($thumb_image);
}
else
{
// Create thumbnail but dont make thumbnail keep the aspect ratio
$source_aspect_ratio = $this->xsize / $this->ysize;
$desired_aspect_ratio = $thumb_x / $thumb_y;
$temp_width = $thumb_x;
$temp_height = $thumb_y;
$temp_left = 0;
$temp_top = 0;
if ($source_aspect_ratio > $desired_aspect_ratio) // wider image
{
$temp_height = $thumb_y;
$temp_width = ( int )round($thumb_y * $source_aspect_ratio);
$temp_left = (int)round(($temp_width - $thumb_x) / 2);
$temp_top = 0;
}
elseif ($source_aspect_ratio < $desired_aspect_ratio) // taller image
{
$temp_width = $thumb_x;
$temp_height = ( int )round($thumb_x / $source_aspect_ratio);
$temp_left = 0;
$temp_top = (int)round(($temp_height - $thumb_y) / 2);
}
RokGallery_Memory::adjustLimitForImage($temp_width, $temp_height);
$thumb_image_after_resize = $slice_image->resize($temp_width, $temp_height);
$slice_image->destroy();
unset($slice_image);
RokGallery_Memory::adjustLimitForImage($thumb_x, $thumb_y);
$thumb_image = $thumb_image_after_resize->crop($temp_left, $temp_top, $thumb_x, $thumb_y);
$thumb_image_after_resize->destroy();
unset($thumb_image_after_resize);
// write out image
$thumb_image->saveToFile($thumb_path, RokGallery_Helper::getImageQuality($this->File->type));
RokGallery_Queue_FileCreate::add($thumb_path);
$thumb_image->destroy();
unset($thumb_image);
}
}
}
/**
* Gets the full path to the Thumbnail file;
* @return string
*/
public function getThumbFullPath($extension = 'thumb')
{
return dirname($this->File->getFullPath()) . DS . $this->guid . '-' . $extension . '.' . $this->File->type;
}
/**
* Returns the thumbnail path relative to the RokGallery image root.
*
* @param string $seperator
* @return string
*/
public function getRelativeThumbPath($seperator = '/', $extension = 'thumb')
{
return RokGallery_Helper::getPathFromGUID($this->File->guid, $seperator) . $seperator . $this->guid . '-' . $extension . '.' . $this->File->type;
}
/**
* Returns the full path to the slice image
*
* @return string
*/
public function getFullPath()
{
return dirname($this->File->getFullPath()) . DS . $this->guid . '.' . $this->File->type;
}
/**
* Returns the relative path to the slice image
*
* @param string $seperator
* @return string
*/
public function getRelativePath($seperator = '/')
{
return RokGallery_Helper::getPathFromGUID($this->File->guid, $seperator) . $seperator . $this->guid . '.' . $this->File->type;
}
/**
* See if the slice has the passed tag
* @param $searchtag
* @return bool
*/
public function hasTag($searchtag)
{
foreach ($this->FileTags as &$filetag)
{
if ($filetag->tag == $searchtag) {
return true;
}
}
foreach ($this->Tags as &$tag)
{
if ($tag->tag == $searchtag) {
return true;
}
}
return false;
}
/**
* Add tag if it doesn't already exists;
*
* @param $tagtext
*/
public function addTag($tagtext)
{
$dbtag = null;
if ($this->hasTag($tagtext)) return;
$dbtag = new RokGallery_Model_SliceTags();
$dbtag->tag = $tagtext;
$this->Tags[] = $dbtag;
return;
}
/**
*/
public function incrementView()
{
$this->File->incrementView();
}
/**
*/
public function incrementLoves()
{
$this->File->incrementLoves();
}
/**
*/
public function decrementLoves()
{
$this->File->decrementLoves();
}
public function &getCombinedTags()
{
$out = $this->Tags->toArray('false');
$out = array_merge($out, $this->File->Tags->toArray('false'));
return $out;
}
public function populateFilterInfo()
{
$this->imageurl;
$this->thumburl;
$this->adminthumburl;
$this->adminthumbxsize;
$this->adminthumbysize;
$this->miniadminthumburl;
$this->miniadminthumbxsize;
$this->miniadminthumbysize;
}
}