-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfoliogallery.php
More file actions
321 lines (255 loc) · 9.24 KB
/
Copy pathfoliogallery.php
File metadata and controls
321 lines (255 loc) · 9.24 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!--
folioGallery v1.3 - 2014-05-01
(c) 2014 Harry Ghazanian - foliopages.com/php-jquery-ajax-photo-gallery-no-database
This content is released under the http://www.opensource.org/licenses/mit-license.php MIT License.
-->
<?php
/***** gallery settings *****/
$mainFolder = 'albums'; // main folder that holds albums - this folder resides on root directory of your domain
$album_page_url = $_SERVER['PHP_SELF']; // url of page where gallery/albums are located
$no_thumb = 'foliogallery/noimg.png'; // show this when no thumbnail exists
$extensions = array("jpg","png","gif","JPG","PNG","GIF"); // allowed extensions in photo gallery
$itemsPerPage = '12'; // number of images per page if not already specified in ajax mode
$thumb_width = '150'; // width of thumbnails in pixels
$sort_albums_by_date = FALSE; // TRUE will sort albums by upload date, FALSE will sort albums by name
$sort_images_by_date = TRUE; // TRUE will sort thumbs by creation date, FALSE will sort images by name
$random_thumbs = TRUE; // TRUE will display random thumbnails, FALSE will display the first image from thumbs folders
$show_captions = TRUE; // TRUE will display file names as captions on thumbs inside albums, FALSE will display no captions
$num_captions_chars = '20'; // number of characters displayed in album and thumb captions
/***** end gallery settings *****/
$numPerPage = (!empty($_REQUEST['numperpage']) ? (int)$_REQUEST['numperpage'] : $itemsPerPage);
$fullAlbum = (!empty($_REQUEST['fullalbum']) ? 1 : 0);
// function to create thumbnails from images
function make_thumb($folder,$file,$dest,$thumb_width) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
switch($ext)
{
case "jpg":
$source_image = imagecreatefromjpeg($folder.'/'.$file);
break;
case "jpeg":
$source_image = imagecreatefromjpeg($folder.'/'.$file);
break;
case "png":
$source_image = imagecreatefrompng($folder.'/'.$file);
break;
case "gif":
$source_image = imagecreatefromgif($folder.'/'.$file);
break;
}
$width = imagesx($source_image);
$height = imagesy($source_image);
if($width < $thumb_width) // if original image is smaller don't resize it
{
$thumb_width = $width;
$thumb_height = $height;
}
else
{
$thumb_height = floor($height*($thumb_width/$width));
}
$virtual_image = imagecreatetruecolor($thumb_width,$thumb_height);
if($ext == "gif" or $ext == "png") // preserve transparency
{
imagecolortransparent($virtual_image, imagecolorallocatealpha($virtual_image, 0, 0, 0, 127));
imagealphablending($virtual_image, false);
imagesavealpha($virtual_image, true);
}
imagecopyresampled($virtual_image,$source_image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
switch($ext)
{
case 'jpg': imagejpeg($virtual_image, $dest,80); break;
case 'jpeg': imagejpeg($virtual_image, $dest,80); break;
case 'gif': imagegif($virtual_image, $dest); break;
case 'png': imagepng($virtual_image, $dest); break;
}
imagedestroy($virtual_image);
imagedestroy($source_image);
}
// return array sorted by date or name
function sort_array(&$array,$dir,$sort_by_date) { // array argument must be passed as reference
if($sort_by_date)
{
foreach ($array as $key=>$item)
{
$stat_items = stat($dir .'/'. $item);
$item_time[$key] = $stat_items['ctime'];
}
return array_multisort($item_time, SORT_DESC, $array);
}
else
{
return usort($array, 'strnatcasecmp');
}
}
// display pagination
function paginate_array($numPages,$urlVars,$alb,$currentPage) {
$html = '';
if ($numPages > 1)
{
if ($currentPage > 1)
{
$html .= "<div id='nextPage'>";
$prevPage = $currentPage - 1;
$html .= ' <a class="pag prev" rel="'.$alb.'" rev="'.$prevPage.'" href="?'.$urlVars.'p='.$prevPage.'">Last Page</a> ';
$html .= '</div>';
}
if ($currentPage != $numPages)
{
$html .= "<div id='prevPage'>";
$nextPage = $currentPage + 1;
$html .= ' <a class="pag next" rel="'.$alb.'" rev="'.$nextPage.'" href="?'.$urlVars.'p='.$nextPage.'">Next Page</a>';
$html .= '</div>';
}
for( $i=0; $i < $numPages; $i++ )
{
$p = $i + 1;
$class = ($p==$currentPage ? 'current-paginate' : 'paginate');
$html .= '<a rel="'.$alb.'" rev="'.$p.'" class="'.$class.' pag" href="?'.$urlVars.'p='.$p.'">'.$p.'</a>';
}
}
return $html;
}
?>
<div class="fg">
<?php
if (empty($_REQUEST['album'])) // if no album requested, show all albums
{
$albums = array_diff(scandir($mainFolder), array('..', '.'));
$numAlbums = count($albums);
if($numAlbums == 0)
{ ?>
<div class="titlebar"><p>There are currently no albums</p></div>
<?php
}
else
{
sort_array($albums,$mainFolder,$sort_albums_by_date); // rearrange array either by date or name
$numPages = ceil( $numAlbums / $numPerPage );
if(isset($_REQUEST['p']))
{
$currentPage = ((int)$_REQUEST['p'] > $numPages ? $numPages : (int)$_REQUEST['p']);
}
else
{
$currentPage=1;
}
$start = ($currentPage * $numPerPage) - $numPerPage; ?>
<div class="p10-lr">
<span class="title">Photo Gallery</span> - <?php echo $numAlbums; ?> albums
</div>
<div class="clear"></div>
<?php
for( $i=$start; $i<$start + $numPerPage; $i++ )
{
if(isset($albums[$i]))
{
$thumb_pool = glob($mainFolder.'/'.$albums[$i].'/thumbs/*{.'.implode(",", $extensions).'}', GLOB_BRACE);
if (count($thumb_pool) == 0)
{
$album_thumb = $no_thumb;
}
else
{
$album_thumb = ($random_thumbs ? $thumb_pool[array_rand($thumb_pool)] : $thumb_pool[0]); // display a random thumb or the 1st thumb
} ?>
<div class="thumb-wrapper">
<div class="thumb">
<a class="showAlb" rel="<?php echo $albums[$i]; ?>" href="<?php echo $_SERVER['PHP_SELF']; ?>?album=<?php echo urlencode($albums[$i]); ?>">
<img src="<?php echo $album_thumb; ?>" alt="<?php echo $albums[$i]; ?>" />
</a>
</div>
<div class="caption"><?php echo substr($albums[$i],0,$num_captions_chars); ?></div>
</div>
<?php
}
}
?>
<div class="clear"></div>
<div align="center" class="paginate-wrapper">
<?php
$urlVars = "";
$alb = "";
echo paginate_array($numPages,$urlVars,$alb,$currentPage);
?>
</div>
<?php
}
}
else //display photos in album
{
$album = $mainFolder.'/'.$_REQUEST['album'];
$files = array_diff(scandir($album), array('..', '.','thumbs'));
$numFiles = count($files); ?>
<div class="p10-lr">
<?php if($fullAlbum==1) { ?>
<span class="title"><a href="<?php echo $album_page_url; ?>" class="refresh">Albums</a></span>
<span class="title">»</span>
<?php } ?>
<span class="title"><?php echo $_REQUEST['album']; ?></span> - <?php echo $numFiles; ?> images
</div>
<div class="clear"></div>
<?php
if($numFiles == 0)
{ ?>
<div class="p10-lr"><p>There are no images in this album.</p></div>
<?php
}
else
{
sort_array($files,$album,$sort_images_by_date); // rearrange array either by date or name
$numPages = ceil( $numFiles / $numPerPage );
if(isset($_REQUEST['p']))
{
$currentPage = ((int)$_REQUEST['p'] > $numPages ? $numPages : (int)$_REQUEST['p']);
}
else
{
$currentPage=1;
}
$start = ($currentPage * $numPerPage) - $numPerPage;
if (!is_dir($album.'/thumbs'))
{
mkdir($album.'/thumbs');
chmod($album.'/thumbs', 0777);
//chown($album.'/thumbs', 'apache');
}
for( $i=0; $i <= $numFiles; $i++ )
{
if(isset($files[$i]) && is_file($album .'/'. $files[$i]))
{
$ext = strtolower(pathinfo($files[$i], PATHINFO_EXTENSION));
$caption = substr($files[$i], 0, -(strlen($ext)+1));
if(in_array($ext, $extensions))
{
$thumb = $album.'/thumbs/'.$files[$i];
if($i>=$start && $i<$start + $numPerPage) {
if (!file_exists($thumb)) {
make_thumb($album,$files[$i],$thumb,$thumb_width);
}
?>
<div class="thumb-wrapper">
<div class="thumb">
<a href="<?php echo $album; ?>/<?php echo $files[$i]; ?>" title="<?php echo $files[$i]; ?>" class="albumpix">
<img src="<?php echo $thumb; ?>" alt="<?php echo $files[$i]; ?>" />
</a>
</div>
<?php if($show_captions) { ?><div class="caption"><?php echo substr($caption,0,$num_captions_chars); ?></div><?php } ?>
<?php
}
if($i>=$start && $i<$start + $numPerPage) { ?></div><?php } }
}
} ?>
<div class="clear"></div>
<div align="center" class="paginate-wrapper">
<?php
$urlVars = "album=".urlencode($_REQUEST['album'])."&";
$alb = $_REQUEST['album'];
echo paginate_array($numPages,$urlVars,$alb,$currentPage);
?>
</div>
<?php
} // end if numFiles not 0
}
?>
</div>