-
Notifications
You must be signed in to change notification settings - Fork 127
/
Image.php
1494 lines (1355 loc) · 42.7 KB
/
Image.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class to describe a single Ansel image.
*
* Copyright 2001-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael J. Rubinsky <mrubinsk@horde.org>
* @package Ansel
*/
class Ansel_Image implements Iterator
{
/**
* The gallery id of this image's parent gallery
*
* @var integer
*/
public $gallery;
/**
* Image Id
*
* @var integer
*/
public $id = null;
/**
* The filename for this image
*
* @var string
*/
public $filename = 'Untitled';
/**
* Image caption
*
* @var string
*/
public $caption = '';
/**
* The image's mime type
*
* @var string
*/
public $type = 'image/jpeg';
/**
* Timestamp of uploaded datetime
*
* @var integer
*/
public $uploaded;
/**
* Sort count for this image
*
* @var integer
*/
public $sort;
/**
* The number of comments for this image, if available.
*
* @var integer
*/
public $commentCount;
/**
* Number of faces in this image
* @var integer
*/
public $facesCount;
/**
* Latitude
*
* @var string
*/
public $lat;
/**
* Longitude
*
* @var string
*/
public $lng;
/**
* Textual location
*
* @var string
*/
public $location;
/**
* Timestamp for when image was geotagged
*
* @var integer
*/
public $geotag_timestamp;
/**
* Timestamp of original date.
*
* @var integer
*/
public $originalDate;
/**
* Horde_Image object for this image.
*
* @var Horde_Image_Base
*/
protected $_image;
/**
* Dirty flag
*
* @var boolean
*/
protected $_dirty;
/**
* Flags for loaded views
*
* @var array
*/
protected $_loaded = array();
/**
* Binary image data for loaded views
*
* @var array
*/
protected $_data = array();
/**
* Holds an array of tags for this image
*
* @var array
*/
protected $_tags = array();
/**
* Cache the raw EXIF data locally
*
* @var array
*/
protected $_exif = array();
/**
* Const'r
*
* @param array $image
*
* @return Ansel_Image
*/
public function __construct(array $image = array())
{
if ($image) {
$this->filename = $image['image_filename'];
if (!empty($image['gallery_id'])) {
$this->gallery = $image['gallery_id'];
}
if (!empty($image['image_caption'])) {
$this->caption = $image['image_caption'];
}
if (isset($image['image_sort'])) {
$this->sort = $image['image_sort'];
}
if (!empty($image['image_id'])) {
$this->id = $image['image_id'];
}
if (!empty($image['data'])) {
$this->_data['full'] = $image['data'];
}
if (!empty($image['image_uploaded_date'])) {
$this->uploaded = $image['image_uploaded_date'];
} else {
$this->uploaded = time();
}
if (!empty($image['image_type'])) {
$this->type = $image['image_type'];
}
if (!empty($image['tags'])) {
$this->_tags = $image['tags'];
}
if (!empty($image['image_faces'])) {
$this->facesCount = $image['image_faces'];
}
$this->location = !empty($image['image_location']) ? $image['image_location'] : '';
// The following may have to be rewritten by EXIF.
// EXIF requires both an image id and a stream, so we can't
// get EXIF data before we save the image to the VFS.
if (!empty($image['image_original_date'])) {
$this->originalDate = $image['image_original_date'];
} else {
$this->originalDate = $this->uploaded;
}
$this->lat = !empty($image['image_latitude']) ? $image['image_latitude'] : '';
$this->lng = !empty($image['image_longitude']) ? $image['image_longitude'] : '';
$this->geotag_timestamp = !empty($image['image_geotag_date']) ? $image['image_geotag_date'] : '0';
}
$this->_image = Ansel::getImageObject();
$this->_image->reset();
$this->id = !empty($image['image_id']) ? $image['image_id'] : null;
}
/**
* Obtain a reference to the underlying Horde_Image
*
* @return Horde_Image_Base
*/
public function &getHordeImage()
{
return $this->_image;
}
/**
* Return the vfs path for this image.
*
* @param string $view The view we want.
* @param Ansel_Style $style A gallery style.
*
* @return string The vfs path for this image.
*/
public function getVFSPath($view = 'full', Ansel_Style $style = null)
{
return $this->getVFSPathFromHash($this->getViewHash($view, $style));
}
/**
* Generate a path on the VFS given a known style hash.
*
* @param string $hash The sytle hash
*
* @return string the VFS path to the directory for the provided hash
*/
public function getVFSPathFromHash($hash)
{
return '.horde/ansel/'
. substr(str_pad($this->id, 2, 0, STR_PAD_LEFT), -2)
. '/' . $hash;
}
/**
* Returns the file name of this image as used in the VFS backend.
*
* @param string $view The image view (full, screen, thumb, mini).
*
* @return string This image's VFS file name.
*/
public function getVFSName($view)
{
$vfsname = $this->id;
if ($view == 'full' && $this->type) {
$type = strpos($this->type, '/') === false ?
'image/' . $this->type :
$this->type;
if ($ext = Horde_Mime_Magic::mimeToExt($type)) {
$vfsname .= '.' . $ext;
}
} elseif (($GLOBALS['conf']['image']['type'] == 'jpeg') || $view == 'screen') {
$vfsname .= '.jpg';
} else {
$vfsname .= '.png';
}
return $vfsname;
}
/**
* Loads the given view into memory.
*
* @param string $view Which view to load.
* @param Ansel_Style $style The gallery style.
*
* @throws Ansel_Exception
*/
public function load($view = 'full', Ansel_Style $style = null)
{
// If this is a new image that hasn't been saved yet, we will
// already have the full data loaded. If we auto-rotate the image
// then there is no need to save it just to load it again.
if ($view == 'full' && !empty($this->_data['full'])) {
$this->_image->loadString($this->_data['full']);
$this->_loaded['full'] = true;
return;
} elseif ($view == 'full') {
try {
$data = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->read($this->getVFSPath('full'), $this->getVFSName('full'));
} catch (Horde_Vfs_Exception $e) {
Horde::log($e, 'ERR');
throw new Ansel_Exception($e);
}
$viewHash = 'full';
} else {
$viewHash = $this->getViewHash($view, $style);
// If we've already loaded the data, just return now.
if (!empty($this->_loaded[$viewHash])) {
return;
}
$this->createView(
$view,
$style,
(($view == 'screen' && $GLOBALS['prefs']->getValue('watermark_auto')) ?
$GLOBALS['prefs']->getValue('watermark_text', '') : '')
);
// If createView() had to resize the full image, we've already
// loaded the data, so return now.
if (!empty($this->_loaded[$viewHash])) {
return;
}
// Get the VFS info.
$vfspath = $this->getVFSPath($view, $style);
// Read in the requested view.
try {
$data = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->read($vfspath, $this->getVFSName($view));
} catch (Horde_Vfs_Exception $e) {
Horde::log($e, 'ERR');
throw new Ansel_Exception($e);
}
}
/* We've definitely successfully loaded the image now. */
$this->_loaded[$viewHash] = true;
$this->_data[$viewHash] = $data;
$this->_image->loadString($data);
}
/**
* Check if an image view exists and returns the vfs name complete with
* the hash directory name prepended if appropriate.
*
* @param integer $id Image id to check
* @param string $view Which view to check for
* @param Ansel_Style $style Style object
*
* @return mixed False if image does not exists | string vfs name
*/
public static function viewExists($id, $view, Ansel_Style $style)
{
// We cannot check empty styles since we cannot get the hash
if (empty($style)) {
return false;
}
// Get the VFS path.
$view = $style->getHash($view);
// Can't call the various vfs methods here, since this method needs
// to be called statically.
$vfspath = '.horde/ansel/' . substr(str_pad($id, 2, 0, STR_PAD_LEFT), -2) . '/' . $view;
// Get VFS name
$vfsname = $id . '.';
if ($GLOBALS['conf']['image']['type'] == 'jpeg' || $view == 'screen') {
$vfsname .= 'jpg';
} else {
$vfsname .= 'png';
}
if ($GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('images')->exists($vfspath, $vfsname)) {
return $view . '/' . $vfsname;
} else {
return false;
}
}
/**
* Creates and caches the given view.
*
* @param string $view Which view to create.
* @param Ansel_Style $style A style object
*
* @throws Ansel_Exception
*/
public function createView($view, Ansel_Style $style = null, $watermark = '')
{
// Default to the gallery's style
if (empty($style)) {
$style = $GLOBALS['injector']
->getInstance('Ansel_Storage')
->getGallery($this->gallery)
->getStyle();
}
// Get the VFS info.
$vfspath = $this->getVFSPath($view, $style);
if ($GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->exists($vfspath, $this->getVFSName($view))) {
return;
}
try {
$data = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->read($this->getVFSPath('full'), $this->getVFSName('full'));
} catch (Horde_Vfs_Exception $e) {
Horde::log($e, 'ERR');
throw new Ansel_Exception($e);
}
// Force screen images to ALWAYS be jpegs for performance/size
if ($view == 'screen' && $GLOBALS['conf']['image']['type'] != 'jpeg') {
$originalType = $this->_image->setType('jpeg');
} else {
$originalType = false;
}
$vHash = $this->getViewHash($view, $style);
$this->_image->loadString($data);
if ($view == 'thumb') {
$viewType = $style->thumbstyle;
} else {
// Screen, Mini
$viewType = ucfirst($view);
}
try {
$iview = Ansel_ImageGenerator::factory(
$viewType, array('image' => $this, 'style' => $style));
} catch (Ansel_Exception $e) {
// It could be we don't support the requested effect, try
// ansel_default before giving up.
if ($view == 'thumb' && $viewType != 'Thumb') {
$iview = Ansel_ImageGenerator::factory(
'Thumb',
array(
'image' => $this,
'style' => Ansel::getStyleDefinition('ansel_default')));
} else {
// If it wasn't a thumb, then something else must be wrong
throw $e;
}
}
// generate the view
$iview->create();
// Cache the data from the new ImageGenerator
try {
$this->_data[$vHash] = $this->_image->raw();
} catch (Horde_Image_Exception $e) {
throw new Ansel_Exception($e);
}
// ...and put it in Horde_Image obejct, then save
$this->_image->loadString($this->_data[$vHash]);
$this->_loaded[$vHash] = true;
$GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->writeData($vfspath, $this->getVFSName($vHash), $this->_data[$vHash], true);
// Watermark
if (!empty($watermark)) {
$this->watermark($view);
// Cache the data again
try {
$this->_data[$vHash] = $this->_image->raw();
} catch (Horde_Image_Exception $e) {
throw new Ansel_Exception($e);
}
$GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->writeData($vfspath, $this->getVFSName($view), $this->_data[$vHash]);
}
// Revert any type change
if ($originalType) {
$this->_image->setType($originalType);
}
}
/**
* Writes the current data to vfs, used when creating a new image
*
* @return boolean
* @throws Ansel_Exception
*/
protected function _writeData()
{
$this->_dirty = false;
try {
$GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->writeData(
$this->getVFSPath('full'),
$this->getVFSName('full'),
$this->_data['full'],
true);
} catch (Horde_Vfs_Exception $e) {
throw new Ansel_Exception($e);
}
return true;
}
/**
* Change the image data. Deletes old cache and writes the new
* data to the VFS. Used when updating an image
*
* @param string $data The new data for this image.
* @param string $view If specified, the $data represents only this
* particular view. Cache will not be deleted.
*
* @throws Ansel_Exception
*/
public function updateData($data, $view = 'full')
{
/* Delete old cached data if we are replacing the full image */
if ($view == 'full') {
$this->deleteCache();
}
try {
$GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')
->create('images')->writeData(
$this->getVFSPath($view),
$this->getVFSName($view),
$data,
true);
} catch (Horde_Vfs_Exception $e) {
throw new Ansel_Exception($e);
}
}
/**
* Update the image's geotag data. Saves to backend storage as well, so no
* need to call self::save()
*
* @param string $lat Latitude
* @param string $lng Longitude
* @param string $location Textual location
*
*/
public function geotag($lat, $lng, $location = '')
{
$this->lat = $lat;
$this->lng = $lng;
$this->location = $location;
$this->geotag_timestamp = time();
$this->save();
}
/**
* Save image details to storage.
*
* @return integer image id
* @throws Ansel_Exception
*/
public function save()
{
// Existing image, just save and exit
if ($this->id) {
return $GLOBALS['injector']
->getInstance('Ansel_Storage')
->saveImage($this);
}
// New image, need to save the image files
$GLOBALS['injector']->getInstance('Ansel_Storage')->saveImage($this);
// The EXIF functions require a stream, need to save before we read
$this->_writeData();
// Get the EXIF data if we are not a gallery key image.
if ($this->gallery > 0) {
$needUpdate = $this->getEXIF();
}
// Create tags from exif data if desired
$fields = @unserialize($GLOBALS['prefs']->getValue('exif_tags'));
if ($fields) {
$this->_exifToTags($fields);
}
// Save the tags
if (count($this->_tags)) {
try {
$this->setTags($this->_tags);
} catch (Exception $e) {
// Since we got this far, the image has been added, so
// just log the tag failure.
Horde::log($e, 'ERR');
}
}
// Save again if EXIF changed any values
if (!empty($needUpdate)) {
$GLOBALS['injector']->getInstance('Ansel_Storage')->saveImage($this);
}
return $this->id;
}
/**
* Replace this image's image data.
*
* @param array $imageData An array of image data, the same keys as Const'r
*
* @return void
* @throws Ansel_Exception
*/
public function replace(array $imageData)
{
// Reset the data array and remove all cached images
$this->_data = array();
$this->reset();
// Remove attributes
$GLOBALS['injector']
->getInstance('Ansel_Storage')
->clearImageAttributes($this->id);
// Load the new image data
$this->getEXIF();
$this->updateData($imageData);
}
/**
* Adds specified EXIF fields to this image's tags.
* Called during image upload/creation.
*
* @param array $fields An array of EXIF fields to import as a tag.
*
* @return void
*/
protected function _exifToTags(array $fields = array())
{
$tags = array();
foreach ($fields as $field) {
if (!empty($this->_exif[$field])) {
if (substr($field, 0, 8) == 'DateTime') {
$d = new Horde_Date(strtotime($this->_exif[$field]));
$tags[] = $d->format("Y-m-d");
} elseif ($field == 'Keywords') {
$tags = array_merge($tags, explode(',', $this->_exif[$field]));
} else {
$tags[] = $this->_exif[$field];
}
}
}
$this->_tags = array_merge($this->_tags, $tags);
}
/**
* Reads the EXIF data from the image, caches in the object and writes to
* storage. Also populates any local properties that come from the EXIF
* data.
*
* @param boolean $replacing Set to true if we are replacing the exif data.
*
* @return boolean True if any local properties were modified, False if not.
* @throws Ansel_Exception
*/
public function getEXIF($replacing = false)
{
/* Clear the local copy */
$this->_exif = array();
/* Get the data */
try {
$imageFile = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->readFile(
$this->getVFSPath('full'),
$this->getVFSName('full'));
} catch (Horde_Vfs_Exception $e) {
throw new Ansel_Exception($e);
}
$params = !empty($GLOBALS['conf']['exif']['params']) ?
$GLOBALS['conf']['exif']['params'] :
array();
$params['logger'] = $GLOBALS['injector']->getInstance('Horde_Log_Logger');
$exif = Horde_Image_Exif::factory(
$GLOBALS['conf']['exif']['driver'],
$params);
try {
$exif_fields = $exif->getData($imageFile);
} catch (Horde_Image_Exception $e) {
// Log the error, but it's not the end of the world, so just ignore
Horde::log($e, 'ERR');
$exif_fields = array();
return false;
}
// Flag to determine if we need to resave the image data.
$needUpdate = false;
// Populate any local properties that come from EXIF
if (!empty($exif_fields['GPSLatitude'])) {
$this->lat = $exif_fields['GPSLatitude'];
$this->lng = $exif_fields['GPSLongitude'];
$this->geotag_timestamp = time();
$needUpdate = true;
}
if (!empty($exif_fields['DateTimeOriginal'])) {
$this->originalDate = $exif_fields['DateTimeOriginal'];
$needUpdate = true;
}
// Overwrite any existing value for caption with exif data
$exif_title = $GLOBALS['prefs']->getValue('exif_title');
if (!empty($exif_fields[$exif_title])) {
$this->caption = $exif_fields[$exif_title];
$needUpdate = true;
}
// Attempt to autorotate based on Orientation field
if (!empty($exif_fields['Orientation'])) {
$this->_autoRotate($exif_fields['Orientation']);
}
// Save attributes.
if ($replacing) {
$GLOBALS['injector']
->getInstance('Ansel_Storage')
->clearImageAttributes($this->id);
}
foreach ($exif_fields as $name => $value) {
if (!empty($value)) {
$GLOBALS['injector']
->getInstance('Ansel_Storage')
->saveImageAttribute($this->id, $name, $value);
$this->_exif[$name] = Horde_Image_Exif::getHumanReadable($name, $value);
}
}
return $needUpdate;
}
/**
* Autorotate based on EXIF orientation field. Updates the data in memory
* only.
*/
protected function _autoRotate($orientation)
{
if (!empty($orientation) && $orientation != 1) {
switch ($orientation) {
case 2:
$this->mirror();
break;
case 3:
$this->rotate('full', 180);
break;
case 4:
$this->mirror();
$this->rotate('full', 180);
break;
case 5:
$this->flip();
$this->rotate('full', 90);
break;
case 6:
$this->rotate('full', 90);
break;
case 7:
$this->mirror();
$this->rotate('full', 90);
break;
case 8:
$this->rotate('full', 270);
break;
}
if ($this->_dirty) {
$this->_data['full'] = $this->raw();
$this->_writeData();
}
}
}
/**
* Reset the image, removing all loaded views.
*
*/
public function reset()
{
$this->_image->reset();
$this->_loaded = array();
}
/**
* Deletes the specified cache file.
*
* If none is specified, deletes all of the cache files.
*
* @param string $view Which cache file to delete.
*/
public function deleteCache($view = 'all')
{
// Delete cached screen image.
if ($view == 'all' || $view == 'screen') {
try {
$GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->deleteFile(
$this->getVFSPath('screen'),
$this->getVFSName('screen'));
} catch (Horde_Vfs_Exception $e ) {}
}
// Delete cached mini image.
if ($view == 'all' || $view == 'mini') {
try {
$GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->deleteFile(
$this->getVFSPath('mini'),
$this->getVFSName('mini'));
} catch (Horde_Vfs_Exception $e) {}
}
if ($view == 'all' || $view == 'thumb') {
$hashes = $GLOBALS['injector']
->getInstance('Ansel_Storage')
->getHashes();
foreach ($hashes as $hash) {
try {
$GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')
->create('images')
->deleteFile(
$this->getVFSPathFromHash($hash),
$this->getVFSName('thumb'));
} catch (Horde_Vfs_Exception $e) {}
}
}
}
/**
* Returns the raw data for the given view.
*
* @param string $view Which view to return.
*
* @return string The raw binary image data
*/
public function raw($view = 'full')
{
if ($this->_dirty) {
$data = $this->_image->raw();
$this->reset();
return $data;
} else {
$this->load($view);
return $this->_data[$view];
}
}
/**
* Sends the correct HTTP headers to the browser to download this image.
*
* @param string $view The view to download.
*/
public function downloadHeaders($view = 'full')
{
global $browser, $conf;
$filename = $this->filename;
if ($view != 'full') {
if ($ext = Horde_Mime_Magic::mimeToExt('image/' . $conf['image']['type'])) {
$filename .= '.' . $ext;
}
}
$browser->downloadHeaders($filename);
}
/**
* Display the requested view.
*
* @param string $view Which view to display.
* @param Ansel_Style $style Force use of this gallery style.
*
* @throws Horde_Exception_PermissionDenied, Ansel_Exception
*/
public function display($view = 'full', Ansel_Style $style = null)
{
if ($view == 'full' && !$this->_dirty) {
// Check full photo permissions
$gallery = $GLOBALS['injector']
->getInstance('Ansel_Storage')
->getGallery(abs($this->gallery));
if (!$gallery->canDownload()) {
throw Horde_Exception_PermissionDenied(
_("Access denied downloading photos from this gallery."));
}
try {
$data = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Vfs')
->create('images')->read(
$this->getVFSPath('full'),
$this->getVFSName('full'));
} catch (Horde_Vfs_Exception $e) {
throw new Ansel_Exception($e);
}
echo $data;
} elseif (!$this->_dirty) {
$this->load($view, $style);
$this->_image->display();
} else {
$this->_image->display();
}
}
/**
* Wraps the given view into a file.
*
* @param string $view Which view to wrap up.
*
* @return string Path to temporary file.
* @deprecated Not used anywhere, remove in A4.
*/
public function toFile($view = 'full')
{
$this->load($view);
return $this->_image->toFile(
$this->_dirty ?
false :
$this->_data[$view]);
}
/**
* Returns the dimensions of the given view.
*
* @param string $view The view (full, screen etc..) to get dimensions for
*
* @return array A hash of 'width and 'height' dimensions.
*/
public function getDimensions($view = 'full')
{
$this->load($view);
return $this->_image->getDimensions();
}
/**
* Rotates the image.
*
* @param string $view The view (size) to work with.
* @param integer $angle What angle to rotate the image by.
* @todo Ansel 4: reverse order of parameters so we can make $angle required
* but $view optional. In fact, view can be taken out entirely as
* we only ever need to rotate the full image anyway.
*/
public function rotate($view = 'full', $angle = 90)
{
$this->load($view);
$this->_dirty = true;
$this->_image->rotate($angle);
}
/**
* Crop this image to desired dimensions. Crops the currently loaded
* view present in the Horde_Image object.
*
* @see Horde_Image_Base::crop for explanation of parameters
*
* @param integer $x1
* @param integer $y1
* @param integer $x2
* @param integer $y2
*
* @throws Ansel_Exception
*/