-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageManager.php
159 lines (138 loc) · 5.52 KB
/
ImageManager.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
<?php
namespace Arthem\Bundle\FileBundle;
use Arthem\Bundle\FileBundle\LetterAvatar\LetterAvatarManager;
use Arthem\Bundle\FileBundle\Model\FileInterface;
use Arthem\Bundle\FileBundle\Model\ImageInterface;
use Doctrine\Common\Util\ClassUtils;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class ImageManager
{
private CacheManager $cacheManager;
private array $placeholders;
private bool $cropActive;
private array $cache = [];
private array $letterAvatars = [];
private LetterAvatarManager $avatarManager;
private PropertyAccessor $propertyAccessor;
public function __construct(CacheManager $cacheManager, array $placeholders, $cropActive = false)
{
$this->cacheManager = $cacheManager;
$this->placeholders = $placeholders;
$this->cropActive = $cropActive;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}
public function setLetterAvatars(LetterAvatarManager $avatarManager, array $letterAvatars)
{
$this->avatarManager = $avatarManager;
$this->letterAvatars = $letterAvatars;
}
public function imagePath($object, $field, $filter)
{
$key = spl_object_hash($object).'.'.$field.'.'.$filter;
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
$image = $this->propertyAccessor->getValue($object, $field);
$objectClass = ClassUtils::getRealClass(get_class($object));
if ($image instanceof FileInterface) {
$path = $image->getPath();
} elseif (
isset($this->letterAvatars[$objectClass], $this->letterAvatars[$objectClass][$field])
&& null !== $letterAvatarUrl = $this->getLetterAvatarUrl($object, $this->letterAvatars[$objectClass][$field])
) {
return $this->cache[$key] = $letterAvatarUrl;
} elseif ($this->isPlaceholderDefined($objectClass, $field)) {
return $this->cache[$key] = $this->imagePlaceholder($objectClass, $field, $filter);
} else {
return $this->cache[$key] = null;
}
if (null === $path) {
return $this->cache[$key] = null;
}
$this->cache[$key] = $this->cacheManager->generateUrl($path, $filter);
if ($this->cropActive && $image instanceof ImageInterface && (null !== $cropDate = $image->getCropDate($filter))) {
$this->cache[$key] .= '?'.$cropDate;
}
return $this->cache[$key];
}
/**
* @param string|array $fields
*/
public function getLetterAvatarUrl($object, $fields, ?string $colorField = null): ?string
{
if (is_array($fields)) {
[$textField, $colorField] = $fields;
} else {
$colorField = null;
$textField = $fields;
}
$text = $this->propertyAccessor->getValue($object, $textField);
if (null === $text) {
return null;
}
$color = null;
if (null !== $colorField) {
$color = $this->propertyAccessor->getValue($object, $colorField);
if (empty($color)) {
$color = null;
}
}
return $this->avatarManager->generatePath($text, $color);
}
public function letterAvatar($object, string $field): ?string
{
$objectClass = ClassUtils::getRealClass(get_class($object));
$key = spl_object_hash($object).'.'.$field.'.letter';
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
if ($objectClass && isset($this->letterAvatars[$objectClass][$field])) {
return $this->cache[$key] = $this->getLetterAvatarUrl($object, $this->letterAvatars[$objectClass][$field]);
} else {
throw new \InvalidArgumentException(sprintf('Letter avatar is not defined for %s::%s', $objectClass, $field));
}
}
/**
* @param object|string $class
* @param string $field
* @param string $filter
*
* @return string
*/
public function imagePlaceholder($class, $field, $filter)
{
if (is_object($class)) {
$class = get_class($class);
}
$objectClass = ClassUtils::getRealClass($class);
$key = $objectClass.'.'.$field.'.'.$filter.'.placeholder';
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
if ($this->isPlaceholderDefined($objectClass, $field)) {
$path = $this->placeholders[$objectClass][$field];
return $this->cache[$key] = $this->cacheManager->getBrowserPath($path, $filter);
} else {
throw new \InvalidArgumentException(sprintf('Placeholder is not defined for %s::%s', $objectClass, $field));
}
}
private function isPlaceholderDefined($objectClass, string $field): bool
{
return $objectClass && isset($this->placeholders[$objectClass][$field]);
}
public function getImagePath(FileInterface $image, $filter)
{
$key = '__'.spl_object_hash($image).'.'.'.'.$filter;
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
$path = $image->getPath();
$this->cache[$key] = $this->cacheManager->getBrowserPath($path, $filter);
if ($this->cropActive && $image instanceof ImageInterface && (null !== $cropDate = $image->getCropDate($filter))) {
$this->cache[$key] .= '?'.$cropDate;
}
return $this->cache[$key];
}
}