forked from discoverygarden/adr_collection_viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thumbnail.inc
190 lines (178 loc) · 4.85 KB
/
Thumbnail.inc
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
<?php
// $Id$
/**
* @file
*
*/
class Thumbnail {
/**
* This datastreams id.
*
* @var string
*/
protected $dsid;
/**
* The datastreams mime type.
*
* @var string
*/
protected $mime_type;
/**
* The fedora object.
*
* @var fedora_item
*/
protected $item;
/**
* The url to this datastream's thumbnail.
*
* @var string
*/
protected $url;
/**
* The default icon for a datastream. This is used when there is no thumbnail associated with the datastream.
* The thumbnail will be choosen based on the datastreams mime type.
*
* @var array
*/
protected $defaultMimeIcons = array(
'application/pdf' => 'application-pdf.png',
'application/rdf+xml' => 'text-txt.png',
'application/xml' => 'text-txt.png',
'text/xml' => 'text-txt.png',
'audio/midi' => 'audio.png',
'audio/x-aiff' => 'audio.png',
'audio/x-mpeg' => 'audio.png',
'audio/x-wav' => 'audio.png',
'audio/x-pn-realaudio' => 'audio.png',
'audio/mpeg' => 'audio.png',
'audio/mp3' => 'audio.png',
'audio/wav' => 'audio.png',
'video/x-msvideo' => 'video.png',
'video/x-flv' => 'video.png',
'video/mpeg' => 'video.png',
'video/mp4' => 'video.png',
'video/quicktime' => 'video.png',
);
/**
* The default icon for a datastream. This is used when there is no thumbnail associated with the datastream,
* and the datastreams mime type doesn't have a default icon.
*
* @var string
*/
protected $defaultIcon = 'default.png';
/**
*
* @param Fedora_Item $fedora_item
* @param string $dsid
*/
public function __construct(&$fedora_item, $dsid) {
$this->item = &$fedora_item;
$this->dsid = $dsid;
$this->mime_type = $this->item->datastreams[$dsid]['MIMEType'];
$rels_int = $this->item->get_datastream_dissemination("RELS-INT");
if (trim($rels_int) != '') {
$this->relsInt = new SimpleXMLElement($rels_int);
}
}
/**
* Gets the url to this datastreams thumbnail.
*
* @return string
*/
public function getUrl() {
if (empty($this->url)) {
if ($this->isIcon()) {
$this->url = $this->getDatastreamUrl($this->dsid);
}
else {
$this->url = $this->getUrlFromRelsInt();
if (empty($this->url)) {
$this->url = $this->getUrlFromName();
if (empty($this->url)) {
$this->url = $this->getDefaultIconUrl();
}
}
}
}
return $this->url;
}
/**
* Determines if this datastram is a thumbnail
*
* @return boolean
*/
private function isIcon() {
return ($this->dsid == 'TN') || (preg_match('/-tn\.([^\/]*)$/i', $this->dsid) > 0);
}
/**
* Gets a url to a given datastream.
*
* @global $base_url
* @param string $dsid
* @return string
*/
private function getDatastreamUrl($dsid) {
global $base_url;
return "$base_url/fedora/repository/{$this->item->pid}/{$dsid}";
}
/**
* Gets the url from rels int if it exists, and is defined in the rels int.
*
* @return string
*/
private function getUrlFromRelsInt() {
$rels_int = $this->getRelsInt();
if (isset($rels_int)) {
$path = "/rdf:RDF/rdf:Description[@rdf:about='info:fedora/{$this->item->pid}/{$this->dsid}']/coal:hasThumbnail"; // coal Namespace is worry some.
$results = $rels_int->xpath($path);
if (is_array($results) && count($results) > 0) {
$attributes = $results[0]->attributes('rdf', TRUE);
if (isset($attributes['resource'])) {
$dsid = sprintf("%s", $attributes['resource']);
$dsid = preg_filter('/^.*\/([^\/]*)$/', '$1', $dsid);
return $this->getDatastreamUrl($dsid);
}
}
}
return NULL;
}
/**
* Gets the RELS-INT datastream as a SimpleXMLElement.
*
* @return SimpleXMLElement
*/
private function getRelsInt() {
$rels_int = $this->item->get_datastream_dissemination("RELS-INT");
if (trim($rels_int) != '') {
return new SimpleXMLElement($rels_int);
}
return NULL;
}
/**
* Gets the url from this datastreams dsid, if it exists.
*
* @global string $base_url
* @return string
*/
private function getUrlFromName() {
global $base_url;
if (preg_match('/\..+$/i', $this->dsid) > 0) { // Has extension.
$dsid = preg_replace('/\..+$/i', '-tn.jpg', $this->dsid);
if (array_key_exists($dsid, $this->item->datastreams)) {
return $this->getDatastreamUrl($dsid);
}
}
return NULL;
}
/**
* Gets the url for the default icon based on mime type.
*
* @return string
*/
private function getDefaultIconUrl() {
$default_image_path = drupal_get_path('module', 'adr_basic_viewer') . '/images/mime';
$image = isset($this->defaultMimeIcons[$this->mime_type]) ? $this->defaultMimeIcons[$this->mime_type] : $this->defaultIcon;
return "$base_url/$default_image_path/$image";
}
}