forked from burzum/cakephp-file-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHelper.php
More file actions
103 lines (92 loc) · 2.63 KB
/
Copy pathImageHelper.php
File metadata and controls
103 lines (92 loc) · 2.63 KB
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
<?php
/**
* ImageHelper
*
* @author Florian Krämer
* @copyright 2012 Florian Krämer
* @license MIT
*/
class ImageHelper extends AppHelper {
/**
* Helpers
*
* @var array
*/
public $helpers = array(
'Html');
/**
* Generates an image url based on the image record data and the used Gaufrette adapter to store it
*
* @param array $image FileStorage array record or whatever else table that matches this helpers needs without the model, we just want the record fields
* @param string $version Image version string
* @param array $options HtmlHelper::image(), 2nd arg options array
* @return string
*/
public function display($image, $version = null, $options = array()) {
$url = $this->imageUrl($image, $version, $options);
if ($url !== false) {
return $this->Html->image($url, $options);
}
return $this->fallbackImage($options, $image, $version);
}
/**
* URL
*
* @param array $image FileStorage array record or whatever else table that matches this helpers needs without the model, we just want the record fields
* @param string $version Image version string
* @param array $options HtmlHelper::image(), 2nd arg options array
* @throws InvalidArgumentException
* @return string
*/
public function imageUrl($image, $version = null, $options = array()) {
if (empty($image) || empty($image['id'])) {
return false;
}
if (!empty($version)) {
$hash = Configure::read('Media.imageHashes.' . $image['model'] . '.' . $version);
if (empty($hash)) {
throw new InvalidArgumentException(__d('FileStorage', 'No valid version key (%s %s) passed!', @$image['model'], $version));
}
} else {
$hash = null;
}
$Event = new CakeEvent('FileStorage.ImageHelper.imagePath', $this, array(
'hash' => $hash,
'image' => $image,
'version' => $version,
'options' => $options));
CakeEventManager::instance()->dispatch($Event);
if ($Event->isStopped()) {
return $this->normalizePath($Event->data['path']);
} else {
return false;
}
}
/**
* Provides a fallback image if the image record is empty
*
* @param array $options
* @return string
*/
public function fallbackImage($options = array(), $image = array(), $version = null) {
if (isset($options['fallback'])) {
if ($options['fallback'] === true) {
$image = 'placeholder/' . $version . '.jpg';
} else {
$image = $options['fallback'];
}
unset($options['fallback']);
return $this->Html->image($image, $options);
}
return '';
}
/**
* Turns the windows \ into / so that the path can be used in an url
*
* @param string $path
* @return string
*/
public function normalizePath($path) {
return str_replace('\\', '/', $path);
}
}