1
|
<?php
|
2
|
|
3
|
|
4
|
/**
|
5
|
* Class GalleryItemMedia filters suitable MediaRepresentations from a CDM Media
|
6
|
* object and prepares them for to be used as item in a MediaGallery.
|
7
|
*/
|
8
|
class GalleryItemMedia {
|
9
|
|
10
|
public static $OK = 0;
|
11
|
public static $ACCESS_DENIED = -1;
|
12
|
public static $ERROR = -2;
|
13
|
|
14
|
public static $image_mime_type_list_default = [
|
15
|
'image/jpg',
|
16
|
'image/jpeg',
|
17
|
'image/png',
|
18
|
'image/gif',
|
19
|
];
|
20
|
|
21
|
private $image_mime_type_list;
|
22
|
private $thumbnail_width;
|
23
|
private $thumbnail_height;
|
24
|
|
25
|
private $thumbnail_representation = null;
|
26
|
private $full_size_representation = null;
|
27
|
private $web_app_representation = null;
|
28
|
private $first_representation = null;
|
29
|
|
30
|
/**
|
31
|
* GalleryItemMedia constructor.
|
32
|
*/
|
33
|
public function __construct($media, $thumbnail_width, $thumbnail_height, $image_mime_type_list = null) {
|
34
|
if(!$image_mime_type_list){
|
35
|
$this->image_mime_type_list = self::$image_mime_type_list_default;
|
36
|
}
|
37
|
$this->thumbnail_width = $thumbnail_width;
|
38
|
$this->thumbnail_height =$thumbnail_height;
|
39
|
$this->findRepresentations($media);
|
40
|
}
|
41
|
|
42
|
private function findRepresentations($media) {
|
43
|
|
44
|
if (isset($media->representations[0]->parts[0])) {
|
45
|
$thumbnail_representations = cdm_preferred_media_representations($media, $this->image_mime_type_list,
|
46
|
$this->thumbnail_width, $this->thumbnail_height, FALSE, TRUE
|
47
|
);
|
48
|
$full_size_representations = cdm_preferred_media_representations($media, $this->image_mime_type_list);
|
49
|
$web_app_representations = cdm_preferred_media_representations($media, $this->image_mime_type_list, NULL, NULL, TRUE);
|
50
|
|
51
|
if (isset_not_empty($media->representations)) {
|
52
|
// can be used as last resort fall back
|
53
|
$this->first_representation = $media->representations[0];
|
54
|
}
|
55
|
|
56
|
if (isset_not_empty($this->thumbnail_representation)) {
|
57
|
$this->thumbnail_representation = array_shift($thumbnail_representations);
|
58
|
}
|
59
|
if (isset_not_empty($full_size_representations)) {
|
60
|
$this->full_size_representation = array_shift($full_size_representations);
|
61
|
}
|
62
|
if (isset_not_empty($web_app_representations)) {
|
63
|
$this->web_app_representation = array_shift($web_app_representations);
|
64
|
}
|
65
|
}
|
66
|
}
|
67
|
|
68
|
public function getThumbnailImageMediaItem() {
|
69
|
$url = $this->getThumbnailImageUrl();
|
70
|
if($url){
|
71
|
return new ImageMediaItem($this->getThumbnailImageUrl(), $this->getThumbnailSize());
|
72
|
}
|
73
|
return null;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Provides the size of the thumbnail image if available.
|
78
|
* In case this GalleryItemMedia contains as web app url the favicon
|
79
|
* default size is returned.
|
80
|
*
|
81
|
* @return object|null
|
82
|
*/
|
83
|
private function getThumbnailSize() {
|
84
|
$repr = $this->getThumbnailRepresentation();
|
85
|
if($repr){
|
86
|
return $this->sizeOf($repr);
|
87
|
} else if($this->hasWebAppItem()) {
|
88
|
// returning the favicon size, actual size is not important, in this case!
|
89
|
return new MediaSize(64,64);
|
90
|
}
|
91
|
return null;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* The url of the thumbnail image if this is available. In case this
|
96
|
* GalleryItemMedia contains as web app url the according favicon is returned
|
97
|
* as thumbnail url.
|
98
|
*
|
99
|
* @return string|null
|
100
|
* The URL
|
101
|
*/
|
102
|
private function getThumbnailImageUrl() {
|
103
|
$repr = $this->getThumbnailRepresentation();
|
104
|
if($repr){
|
105
|
return $this->urlOf($repr);
|
106
|
} else if($this->hasWebAppItem()) {
|
107
|
return $this->webAppIcon($this->getWebAppUrl());
|
108
|
}
|
109
|
return null;
|
110
|
}
|
111
|
|
112
|
public function getWebAppUrl() {
|
113
|
return $this->urlOf($this->web_app_representation);
|
114
|
}
|
115
|
|
116
|
public function hasWebAppItem(){
|
117
|
return is_string($this->getWebAppUrl());
|
118
|
}
|
119
|
|
120
|
public function getFullSizeImageUrl() {
|
121
|
if($this->full_size_representation){
|
122
|
return $this->urlOf($this->full_size_representation);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
public function hasFullSizeImage(){
|
127
|
return is_string($this->getFullSizeImageUrl());
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Provides the ULR to be used for showing the the item
|
132
|
* in an overlay widget.
|
133
|
*
|
134
|
* This is currently limited to images only.
|
135
|
*
|
136
|
* @return string
|
137
|
*/
|
138
|
public function getOverlayImageUrl(){
|
139
|
if($this->hasFullSizeImage()){
|
140
|
return $this->getFullSizeImageUrl();
|
141
|
}
|
142
|
return null;
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Provides the URL to be used for linking directly to the item.
|
147
|
* Such links will open the item directly in the browser window.
|
148
|
* WebApp urls are preferred over full-size-image urls.
|
149
|
* The media item can be a web-app or image.
|
150
|
*
|
151
|
* @return mixed
|
152
|
*/
|
153
|
public function getOpenItemUrl(){
|
154
|
|
155
|
if($this->hasWebAppItem()){
|
156
|
$url = $this->getWebAppUrl();
|
157
|
if($this->urlIsAccessible($url) == self::$OK){
|
158
|
return $url;
|
159
|
}
|
160
|
}
|
161
|
if($this->hasFullSizeImage()){
|
162
|
$url = $this->getFullSizeImageUrl();
|
163
|
if($this->urlIsAccessible($url) == self::$OK){
|
164
|
return $url;
|
165
|
}
|
166
|
}
|
167
|
return null;
|
168
|
}
|
169
|
|
170
|
private function getThumbnailRepresentation() {
|
171
|
if ($this->thumbnail_representation) {
|
172
|
return $this->thumbnail_representation;
|
173
|
} else {
|
174
|
// use fallback strategy
|
175
|
if ($this->full_size_representation) {
|
176
|
return $this->full_size_representation;
|
177
|
} else if(!$this->web_app_representation->uuid || $this->first_representation->uuid !== $this->web_app_representation->uuid){
|
178
|
return $this->first_representation;
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* @param $media_representation
|
185
|
* The CDM MediaRepresentation
|
186
|
*
|
187
|
* @return string|null
|
188
|
* The fist URL found in the representation parts or null
|
189
|
*/
|
190
|
private function urlOf($media_representation){
|
191
|
if(isset($media_representation->parts)){
|
192
|
foreach($media_representation->parts as $part){
|
193
|
if(isset($part->uri)){
|
194
|
return $part->uri;
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
return null;
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* @param $url
|
203
|
*
|
204
|
* @return false|mixed|string
|
205
|
*
|
206
|
* function found at https://stackoverflow.com/questions/5701593/how-to-get-a-websites-favicon-with-php#5702084
|
207
|
*/
|
208
|
private function webAppIcon($url){
|
209
|
$favicon = new FaviconDownloader($url);
|
210
|
if ($favicon->icoExists) {
|
211
|
return $favicon->icoUrl;
|
212
|
}
|
213
|
return null;
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* @param $url
|
218
|
*
|
219
|
* @return int
|
220
|
* One of the GalleryItemMedia constants: $OK, $ACCESS_DENIED, $ERROR
|
221
|
*/
|
222
|
private function urlIsAccessible($url){
|
223
|
$result = drupal_http_request($url, ['method' => 'HEAD']);
|
224
|
if($result->code == 200){
|
225
|
return GalleryItemMedia::$OK;
|
226
|
} else if($result->code >= 401 && $result->code <= 403) {
|
227
|
return GalleryItemMedia::$ACCESS_DENIED;
|
228
|
} else {
|
229
|
return GalleryItemMedia::$ERROR;
|
230
|
}
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* @param $media_representation
|
235
|
*
|
236
|
* @return MediaSize object
|
237
|
* A MediaSize object with the fields
|
238
|
* - width: may be unset
|
239
|
* - height: may be unset
|
240
|
* or null if no size information is available
|
241
|
*/
|
242
|
private function sizeOf($media_representation){
|
243
|
if(isset($media_representation->parts[0])){
|
244
|
$part = $media_representation->parts[0];
|
245
|
if (isset_not_empty($part->width) || isset_not_empty($part->height)) {
|
246
|
return new MediaSize(
|
247
|
@$media_representation->parts[0]->width,
|
248
|
@$media_representation->parts[0]->height
|
249
|
);
|
250
|
}
|
251
|
}
|
252
|
return null;
|
253
|
}
|
254
|
|
255
|
|
256
|
}
|