Rev 229 Rev 236
1 <?php 1 <?php
2   2  
3 /** 3 /**
4 * Image class. 4 * Image class.
5 * 5 *
6 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 6 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
7 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License 7 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
8 * @copyright (c)2003-2005 Tamlyn Rhodes 8 * @copyright (c)2003-2005 Tamlyn Rhodes
9 * @version $Id: image.class.php,v 1.22 2006/08/06 13:50:20 thepavian Exp $ 9 * @version $Id: image.class.php,v 1.22 2006/08/06 13:50:20 thepavian Exp $
10 */ 10 */
11   11  
12 //include the base class 12 //include the base class
13 require_once dirname(__FILE__)."/item.class.php"; 13 require_once dirname(__FILE__)."/item.class.php";
14 14
15 /** 15 /**
16 * Data-only class used to store image data. 16 * Data-only class used to store image data.
17 * @package singapore 17 * @package singapore
18 * @author Tamlyn Rhodes <tam at zenology dot co dot uk> 18 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
19 * @copyright (c)2003-2005 Tamlyn Rhodes 19 * @copyright (c)2003-2005 Tamlyn Rhodes
20 */ 20 */
21 class sgImage extends sgItem 21 class sgImage extends sgItem
22 { 22 {
23 /** 23 /**
24 * Width in pixels of the image 24 * Width in pixels of the image
25 * @var int 25 * @var int
26 */ 26 */
27 var $width = 0; 27 var $width = 0;
28 28
29 /** 29 /**
30 * Height in pixels of the image 30 * Height in pixels of the image
31 * @var int 31 * @var int
32 */ 32 */
33 var $height = 0; 33 var $height = 0;
34 34
35 /** 35 /**
36 * Image file format flag as returned by GetImageSize() 36 * Image file format flag as returned by GetImageSize()
37 * @var int 37 * @var int
38 */ 38 */
39 var $type; 39 var $type;
40 40
41 41
42 /** 42 /**
43 * Configurable field 43 * Configurable field
44 */ 44 */
45 var $camera = ""; 45 var $camera = "";
46 var $lens = ""; 46 var $lens = "";
47 var $film = ""; 47 var $film = "";
48 var $darkroom = ""; 48 var $darkroom = "";
49 var $digital = ""; 49 var $digital = "";
50 50
51 /** 51 /**
52 * Constructor 52 * Constructor
53 * @param string image id 53 * @param string image id
54 * @param sgGallery reference to the parent gallery 54 * @param sgGallery reference to the parent gallery
55 */ 55 */
56 function sgImage($id, &$parent) 56 function sgImage($id, &$parent)
57 { 57 {
58 $this->id = $id; 58 $this->id = $id;
59 $this->parent =& $parent; 59 $this->parent =& $parent;
60 $this->config =& sgConfig::getInstance(); 60 $this->config =& sgConfig::getInstance();
61 $this->translator =& Translator::getInstance(); 61 $this->translator =& Translator::getInstance();
62 } 62 }
63 63
64 /** 64 /**
65 * Over-rides the method in the item class 65 * Over-rides the method in the item class
66 * @return true returns true 66 * @return true returns true
67 */ 67 */
68 function isImage() { return true; } 68 function isImage() { return true; }
69 69
70 70
71 /** 71 /**
72 * @return bool true if image height is greater than width 72 * @return bool true if image height is greater than width
73 */ 73 */
74 function isPortrait() 74 function isPortrait()
75 { 75 {
76 return $this->width()/$this->height() > 1; 76 return $this->width()/$this->height() > 1;
77 } 77 }
78 78
79 /** 79 /**
80 * @return bool true if image height is greater than width 80 * @return bool true if image height is greater than width
81 */ 81 */
82 function isLandscape() 82 function isLandscape()
83 { 83 {
84 return $this->width()/$this->height() < 1; 84 return $this->width()/$this->height() < 1;
85 } 85 }
86 86
87 function hasPrev() 87 function hasPrev()
88 { 88 {
89 return (bool) $this->index(); 89 return (bool) $this->index();
90 } 90 }
91 91
92 function hasNext() 92 function hasNext()
93 { 93 {
94 $index = $this->index(); 94 $index = $this->index();
95 return $index !== false && $index < $this->parent->imageCount()-1; 95 return $index !== false && $index < $this->parent->imageCount()-1;
96 } 96 }
97 97
98 function &firstImage() 98 function &firstImage()
99 { 99 {
100 return $this->parent->images[0]; 100 return $this->parent->images[0];
101 } 101 }
102 102
103 function &prevImage() 103 function &prevImage()
104 { 104 {
105 return $this->parent->images[$this->index()-1]; 105 return $this->parent->images[$this->index()-1];
106 } 106 }
107 107
108 function &nextImage() 108 function &nextImage()
109 { 109 {
110 return $this->parent->images[$this->index()+1]; 110 return $this->parent->images[$this->index()+1];
111 } 111 }
112 112
113 function &lastImage() 113 function &lastImage()
114 { 114 {
115 return $this->parent->images[count($this->parent->images)-1]; 115 return $this->parent->images[count($this->parent->images)-1];
116 } 116 }
117 117
118 function firstLink($action = null) 118 function firstLink($action = null)
119 { 119 {
120 if(!$this->hasPrev()) 120 if(!$this->hasPrev())
121 return ""; 121 return "";
122 122
123 $tmp =& $this->firstImage(); 123 $tmp =& $this->firstImage();
124 return '<a href="'.$tmp->URL(null, $action).'">'.$this->firstText().'</a>'; 124 return '<a href="'.$tmp->URL(null, $action).'">'.$this->firstText().'</a>';
125 } 125 }
126 126
127 function prevLink($action = null) 127 function prevLink($action = null)
128 { 128 {
129 if(!$this->hasPrev()) 129 if(!$this->hasPrev())
130 return ""; 130 return "";
131 131
132 return '<a href="'.$this->prevURL($action).'">'.$this->prevText().'</a>'; 132 return '<a href="'.$this->prevURL($action).'">'.$this->prevText().'</a>';
133 } 133 }
134 134
135 function nextLink($action = null) 135 function nextLink($action = null)
136 { 136 {
137 if(!$this->hasNext()) 137 if(!$this->hasNext())
138 return ""; 138 return "";
139 139
140 return '<a href="'.$this->nextURL($action).'">'.$this->nextText().'</a>'; 140 return '<a href="'.$this->nextURL($action).'">'.$this->nextText().'</a>';
141 } 141 }
142 142
143 function lastLink($action = null) 143 function lastLink($action = null)
144 { 144 {
145 if(!$this->hasNext()) 145 if(!$this->hasNext())
146 return ""; 146 return "";
147 147
148 $tmp =& $this->lastImage(); 148 $tmp =& $this->lastImage();
149 return '<a href="'.$tmp->URL(null, $action).'">'.$this->lastText().'</a>'; 149 return '<a href="'.$tmp->URL(null, $action).'">'.$this->lastText().'</a>';
150 } 150 }
151 151
152 function prevURL($action = null) 152 function prevURL($action = null)
153 { 153 {
154 $tmp =& $this->prevImage(); 154 $tmp =& $this->prevImage();
155 return $tmp->URL(null, $action); 155 return $tmp->URL(null, $action);
156 } 156 }
157 157
158 function nextURL($action = null) 158 function nextURL($action = null)
159 { 159 {
160 $tmp =& $this->nextImage(); 160 $tmp =& $this->nextImage();
161 return $tmp->URL(null, $action); 161 return $tmp->URL(null, $action);
162 } 162 }
163 163
164 function firstText() { return $this->translator->_g("image|First"); } 164 function firstText() { return $this->translator->_g("image|First"); }
165 function prevText() { return $this->translator->_g("image|Previous"); } 165 function prevText() { return $this->translator->_g("image|Previous"); }
166 function nextText() { return $this->translator->_g("image|Next"); } 166 function nextText() { return $this->translator->_g("image|Next"); }
167 function lastText() { return $this->translator->_g("image|Last"); } 167 function lastText() { return $this->translator->_g("image|Last"); }
168 function parentText() { return $this->translator->_g("image|Thumbnails"); } 168 function parentText() { return $this->translator->_g("image|Thumbnails"); }
169 169
170 function imageURL() 170 function imageURL()
171 { 171 {
172 if($this->config->full_image_resize) { 172 if($this->config->full_image_resize) {
173 $img = $this->thumbnail("image"); 173 $img = $this->thumbnail("image");
174 return $img->URL(); 174 return $img->URL();
175 } else 175 } else
176 return $this->realURL(); 176 return $this->realURL();
177 } 177 }
178 178
179 function realURL() 179 function realURL()
180 { 180 {
181 if($this->isRemote()) 181 if($this->isRemote())
182 return $this->id; 182 return $this->id;
183 else 183 else
184 return $this->config->base_url.$this->config->pathto_galleries.$this->parent->idEncoded()."/".$this->idEncoded(); 184 return $this->config->base_url.$this->config->pathto_galleries.$this->parent->idEncoded()."/".$this->idEncoded();
185 } 185 }
186 186
187 function imageHTML($class = "sgImage") 187 function imageHTML($class = "sgImage")
188 { 188 {
189 $ret = "<img src=\"".$this->imageURL().'" '; 189 $ret = "<img src=\"".$this->imageURL().'" ';
190 $ret .= 'class="'.$class.'" '; 190 $ret .= 'class="'.$class.'" ';
191 $ret .= 'width="'.$this->width().'" height="'.$this->height().'" '; 191 $ret .= 'width="'.$this->width().'" height="'.$this->height().'" ';
192 if($this->config->imagemap_navigation) $ret .= 'usemap="#sgNavMap" border="0" '; 192 if($this->config->imagemap_navigation) $ret .= 'usemap="#sgNavMap" border="0" ';
193 $ret .= 'alt="'.$this->name().$this->byArtistText().'" />'; 193 $ret .= 'alt="'.$this->name().$this->byArtistText().'" />';
194 194
195 return $ret; 195 return $ret;
196 } 196 }
197 197
198 function thumbnailURL($type = "album") 198 function thumbnailURL($type = "album")
199 { 199 {
200 $thumb = $this->thumbnail($type); 200 $thumb = $this->thumbnail($type);
201 return $thumb->URL(); 201 return $thumb->URL();
202 } 202 }
203 203
204 function thumbnailHTML($class = "sgThumbnailAlbum", $type = "album") 204 function thumbnailHTML($class = "sgThumbnailAlbum", $type = "album")
205 { 205 {
206 $thumb = $this->thumbnail($type); 206 $thumb = $this->thumbnail($type);
207 $ret = "<img src=\"".$thumb->URL().'" '; 207 $ret = "<img src=\"".$thumb->URL().'" ';
208 $ret .= 'class="'.$class.'" '; 208 $ret .= 'class="'.$class.'" ';
209 $ret .= 'width="'.$thumb->width().'" height="'.$thumb->height().'" '; 209 $ret .= 'width="'.$thumb->width().'" height="'.$thumb->height().'" ';
210 $ret .= 'alt="'.$this->name().$this->byArtistText().'" />'; 210 $ret .= 'alt="'.$this->name().$this->byArtistText().'" />';
211 return $ret; 211 return $ret;
212 } 212 }
213 213
214 function thumbnailLink($class = "sgThumbnailAlbum", $type = "album") 214 function thumbnailLink($class = "sgThumbnailAlbum", $type = "album")
215 { 215 {
216 return '<a href="'.$this->URL().'">'.$this->thumbnailHTML($class, $type).'</a>'; 216 return '<a href="'.$this->URL().'">'.$this->thumbnailHTML($class, $type).'</a>';
217 } 217 }
218 218
219 function thumbnailPopupLink($class = "sgThumbnailAlbum", $type = "album") 219 function thumbnailPopupLink($class = "sgThumbnailAlbum", $type = "album")
220 { 220 {
221 $ret = '<a href="'.$this->URL().'" onclick="'; 221 $ret = '<a href="'.$this->URL().'" onclick="';
222 $ret .= "window.open('".$this->imageURL()."','','toolbar=0,resizable=1,"; 222 $ret .= "window.open('".$this->imageURL()."','','toolbar=0,resizable=1,";
223 $ret .= "width=".($this->width()+20).","; 223 $ret .= "width=".($this->width()+20).",";
224 $ret .= "height=".($this->height()+20)."');"; 224 $ret .= "height=".($this->height()+20)."');";
225 $ret .= "return false;\">".$this->thumbnailHTML($class, $type)."</a>"; 225 $ret .= "return false;\">".$this->thumbnailHTML($class, $type)."</a>";
226 return $ret; 226 return $ret;
227 } 227 }
228 228
229 function nameForce() 229 function nameForce()
230 { 230 {
231 if($this->name) 231 if($this->name)
232 return $this->name; 232 return $this->name;
233 elseif($this->isRemote()) 233 elseif($this->isRemote())
234 return substr($this->id, strrpos($this->id,'/') + 1, strrpos($this->id,'.') - strrpos($this->id,'/') - 1); 234 return substr($this->id, strrpos($this->id,'/') + 1, strrpos($this->id,'.') - strrpos($this->id,'/') - 1);
235 else 235 else
236 return substr($this->id, 0, strrpos($this->id,'.')); 236 return substr($this->id, 0, strrpos($this->id,'.'));
237 } 237 }
238 238
239 /** 239 /**
240 * checks if image is remote (filename starts with 'http://') 240 * checks if image is remote (filename starts with 'http://')
241 */ 241 */
242 function isRemote($image = null) 242 function isRemote($image = null)
243 { 243 {
244 if($image == null) $image = $this->id; 244 if($image == null) $image = $this->id;
245 return substr($image, 0, 7) == "http://"; 245 return substr($image, 0, 7) == "http://";
246 } 246 }
247 247
248 /** 248 /**
249 * @return string the absolute, canonical system path to the image 249 * @return string the absolute, canonical system path to the image
250 */ 250 */
251 function realPath() 251 function realPath()
252 { 252 {
253 if($this->isRemote()) 253 if($this->isRemote())
254 return $this->id; 254 return $this->id;
255 else 255 else
256 return realpath($this->config->base_path.$this->config->pathto_galleries.$this->parent->id."/".$this->id); 256 return realpath($this->config->base_path.$this->config->pathto_galleries.$this->parent->id."/".$this->id);
257 } 257 }
258 258
259 /** 259 /**
260 * @return string the rawurlencoded version of the image id 260 * @return string the rawurlencoded version of the image id
261 */ 261 */
262 function idEncoded() 262 function idEncoded()
263 { 263 {
264 return rawurlencode($this->id); 264 return rawurlencode($this->id);
265 } 265 }
266 266
267 function width() 267 function width()
268 { 268 {
269 if($this->config->full_image_resize) { 269 if($this->config->full_image_resize) {
270 $img = $this->thumbnail("image"); 270 $img = $this->thumbnail("image");
271 return $img->width(); 271 return $img->width();
272 } else 272 } else
273 return $this->realWidth(); 273 return $this->realWidth();
274 } 274 }
275 275
276 function height() 276 function height()
277 { 277 {
278 if($this->config->full_image_resize) { 278 if($this->config->full_image_resize) {
279 $img = $this->thumbnail("image"); 279 $img = $this->thumbnail("image");
280 return $img->height(); 280 return $img->height();
281 } else 281 } else
282 return $this->realHeight(); 282 return $this->realHeight();
283 } 283 }
284 284
285 /** 285 /**
286 * finds position of current image in parent array 286 * finds position of current image in parent array
287 */ 287 */
288 function index() 288 function index()
289 { 289 {
290 foreach($this->parent->images as $key => $img) 290 foreach($this->parent->images as $key => $img)
291 if($this->id == $img->id) 291 if($this->id == $img->id)
292 return $key; 292 return $key;
293 293
294 return false; 294 return false;
295 } 295 }
296 296
297 function realWidth() 297 function realWidth()
298 { 298 {
299 //try to load image dimensions if not already loaded 299 //try to load image dimensions if not already loaded
300 if($this->width == 0) { 300 if($this->width == 0) {
301 $size = @GetImageSize($this->realPath()); 301 $size = @GetImageSize($this->realPath());
302 if($size) 302 if($size)
303 list($this->width, $this->height, $this->type) = $size; 303 list($this->width, $this->height, $this->type) = $size;
304 else 304 else
305 return $this->config->thumb_width_image; 305 return $this->config->thumb_width_image;
306 } 306 }
307 return $this->width; 307 return $this->width;
308 } 308 }
309 309
310 function realHeight() 310 function realHeight()
311 { 311 {
312 //try to load image dimensions if not already loaded 312 //try to load image dimensions if not already loaded
313 if($this->height == 0) { 313 if($this->height == 0) {
314 $size = @GetImageSize($this->realPath()); 314 $size = @GetImageSize($this->realPath());
315 if($size) 315 if($size)
316 list($this->width, $this->height, $this->type) = $size; 316 list($this->width, $this->height, $this->type) = $size;
317 else 317 else
318 return $this->config->thumb_height_image; 318 return $this->config->thumb_height_image;
319 } 319 }
320 return $this->height; 320 return $this->height;
321 } 321 }
322 322
323 /** Accessor methods */ 323 /** Accessor methods */
324 function type() { return $this->type; } 324 function type() { return $this->type; }
325 325
326 326
327 /* Private methods */ 327 /* Private methods */
328 328
329 function &thumbnail($type) 329 function &thumbnail($type)
330 { 330 {
331 //only create thumbnail if it doesn't already exist 331 //only create thumbnail if it doesn't already exist
332 if(!isset($this->thumbnails[$type])) 332 if(!isset($this->thumbnails[$type]))
333 $this->thumbnails[$type] =& new sgThumbnail($this, $type); 333 $this->thumbnails[$type] =& new sgThumbnail($this, $type);
334 334
335 return $this->thumbnails[$type]; 335 return $this->thumbnails[$type];
336 } 336 }
337 } 337 }
338   338  
339 ?> 339 ?>