-
Notifications
You must be signed in to change notification settings - Fork 2
/
album.php
852 lines (716 loc) · 20.8 KB
/
album.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
<?php /** album.php **/
/*
* cgallery v2.3
*
* album.php:
* - indexes a directory of images and creates thumbnails.
* - output is a client-side webapp (a single html file with css+js).
* - uses cdn versions of jquery and spin.js.
* - works on most newish versions of webkit, ff, ie.
*/
header("Content-Type: text/html; charset=utf-8");
$options = getopt("m:t:"); /* mode */
$protocol = $options['m'] == 'local' ? "http:" : "";
$thumbOnly = ($options['t'] == '1');
function listImages() {
$dir = opendir(".");
$result = array();
while (($fn = readdir($dir)) !== false) {
if (($fn === ".") || ($fn === "..") || !is_file($fn)) {
continue;
}
$extension = strtolower(substr($fn, strlen($fn) - 3, 3));
if (strcmp($extension, "jpg") == 0
|| strcmp($extension, "gif") == 0
|| strcmp($extension, "png") == 0)
{
array_push($result, $fn);
}
}
sort($result);
return $result;
}
function createThumbnail($inFn, $outFn) {
global $thumbOnly;
if ($thumbOnly) {
print " creating thumbnail: $outFn\n";
}
$maxHeight = 80;
$format = strtolower(end(explode(".", $inFn)));
/* load the input image and get dimensions */
$inImage = null;
switch ($format) {
case "png": $inImage = imagecreatefrompng($inFn); break;
case "jpg": $inImage = imagecreatefromjpeg($inFn); break;
case "gif": $inImage = imagecreatefromgif($inFn); break;
default: return;
}
$inWidth = imagesx($inImage);
$inHeight = imagesy($inImage);
$ratio = $inWidth / $inHeight;
/* calculate output dimensions, create image */
$outWidth = floor($maxHeight * $ratio);
$outHeight = $maxHeight;
$outImage = imagecreatetruecolor($outWidth, $outHeight);
/* generate the thumbnail into $outImage */
$result = imagecopyresampled(
$outImage,
$inImage,
0, 0, 0, 0,
$outWidth, $outHeight,
$inWidth, $inHeight
);
/* write to disk, clean up. we always create the thumbnail in
the same format as the input image, makes loading them on the
client side much simpler */
if ($result) {
switch ($format) {
case "png": imagepng($outImage, $outFn); break;
case "jpg": imagejpeg($outImage, $outFn); break;
case "gif": imagegif($outImage, $outFn); break;
default: return;
}
}
imagedestroy($inImage);
imagedestroy($outImage);
}
function createThumbnails($imageList) {
$outPath = (getcwd() . "/.thumbs/");
$inPath = (getcwd() . "/");
@mkdir($outPath);
foreach ($imageList as $imageFn) {
$outFn = $outPath . $imageFn;
if (file_exists($outFn)) {
continue; /* thumbnail exists */
}
$inFn = $inPath . $imageFn;
createThumbnail($inFn, $outFn);
}
}
$imageList = listImages();
createThumbnails($imageList);
if ($thumbOnly) {
exit(0);
}
?>
<html>
<head>
<title>photos</title>
<style>
body > * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
}
::-moz-selection {
background: transparent;
}
html {
/* sometimes webkit displays a vertical scrollbar when we're embedded.
this fixes that. */
overflow: hidden;
}
body {
font-family: sans-serif;
background-color: #404040;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
list-style-type: none;
}
.strip {
white-space: nowrap;
overflow: visible;
text-align: center;
margin: 6px;
}
.strip img {
border: 3px solid black;
margin-right: 6px;
cursor: pointer;
box-sizing: border-box;
-moz-box-sizing: border-box;
box-shadow: 0 0 4px #111;
opacity: 0.75;
}
.strip img.small {
height: 46px;
}
.strip img:hover {
opacity: 1.0;
}
.strip img.active {
border: 3px solid white;
opacity: 1.0;
}
.github {
display: none;
background-color: rgb(46, 46, 46);
position: absolute;
text-align: center;
line-height: 1.2em;
left: 0;
right: 0;
bottom: 0;
height: 20px;
}
.github a {
color: #666;
text-shadow: 0 0 3px #222;
font-size: 11px;
text-decoration: none;
}
.github a:hover {
color: #bbb;
text-shadow: 0 0 3px #000;
text-decoration: underline;
}
.project-link .github {
display: block;
}
.footer {
-webkit-overflow-scrolling: touch;
position: absolute;
bottom: 0;
left: 0;
right: 0;
overflow-x: auto;
overflow-y: hidden;
background-color: #222;
border-top: 1px solid black;
border-bottom: 1px solid #111;
box-shadow: 0 0 4px #111;
}
.project-link .footer {
bottom: 20px;
}
.middle {
position: absolute;
left: 0;
right: 0;
top: 4px;
bottom: 100px;
}
.project-link .middle {
bottom: 120px;
}
.pseudo-button {
position: absolute;
top: 0;
bottom: 0;
}
.pseudo-button.prev {
left: 8px;
right: 50%;
}
.pseudo-button.next {
right: 8px;
left: 50%;
}
.button {
font-family: monospace;
position: absolute;
top: 50%;
margin-top: -64px; /* height/2 */
height: 128px;
width: 40px;
background-color: #222;
cursor: pointer;
color: #ccc;
font-weight: bold;
font-size: 18px;
text-align: center;
line-height: 128px;
box-shadow: 0px 0px 4px #222;
}
/*.pseudo-button:hover .button,*/
.button:hover {
background-color: #2a2a2a;
}
.button.prev {
left: 0;
}
.button.next {
right: 0;
}
.center {
position: absolute;
pointer-events: none;
left: 50px;
right: 50px;
bottom: 0;
top: 0;
}
.spinner-container {
display: none;
position: absolute;
width: 64px;
height: 64px;
top: 50%;
left: 50%;
margin-top: -32px; /* height / 2 */
margin-left: -32px; /* width / 2 */
}
.loading .spinner-container {
display: block;
}
.current-image-container {
position: absolute;
top: 4px;
bottom: 8px;
left: 8px;
right: 8px;
text-align: center;
}
.current-image-container .current-image {
max-width: 100%;
max-height: 100%;
height: auto;
width: auto;
border: 4px solid black;
box-sizing: border-box;
-moz-box-sizing: border-box;
pointer-events: all;
cursor: pointer;
box-shadow: 0 0 4px #111;
}
.loading .current-image {
display: none;
}
.no-images-text {
display: none;
position: absolute;
left: 0;
right: 0;
text-align: center;
top: 50%;
margin-top: -0.6em;
height: 1.2em;
color: #bbb;
text-shadow: 0 0 8px #222;
}
.no-images .no-images-text {
display: block;
}
::-webkit-scrollbar {
height: 9px;
width: 9px;
border-radius: 8px;
background: transparent;
margin-bottom: 4px;
}
::-webkit-scrollbar-thumb {
background: #666;
border: 1px solid #111;
border-radius: 6px;
}
::-webkit-scrollbar-thumb:hover {
background: #bbb;
}
::-webkit-scrollbar-corner {
background: #000;
}
::-webkit-scrollbar-track-piece {
border-top: 1px solid black;
background-color: rgba(0, 0, 0, 0.2);
}
</style>
<?php
global $protocol;
printf('<script src="' . $protocol . '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>' . "\n");
printf('<script src="' . $protocol . '//cdnjs.cloudflare.com/ajax/libs/spin.js/1.2.7/spin.min.js"></script>' . "\n");
?>
<script>
/* https://github.com/brandonaaron/jquery-getscrollbarwidth/blob/master/jquery.getscrollbarwidth.js */
(function($) {
$.browser = {
msie: navigator.appName == 'Microsoft Internet Explorer',
chrome: /Chrome/.test(navigator.userAgent),
firefox: /Firefox/.test(navigator.userAgent)
};
})(jQuery);
</script>
<script>
(function() {
var THUMB_TEMPLATE_NORMAL = '<img data-large="{{large}}" src="{{thumb}}">';
var THUMB_TEMPLATE_SMALL = '<img class="small" data-large="{{large}}" src="{{thumb}}">';
var NORMAL_THUMB_MINIMUM_HEIGHT = 680;
var SPINNER_OPTIONS = {
lines: 12, length: 7, width: 4, radius: 10, color: '#ffffff',
speed: 1, trail: 60, shadow: true
};
var IMAGES = [
<?php
global $imageList; /* generated above */
foreach ($imageList as $filename) {
printf('"' . $filename . '",' . "\n");
}
?>
];
IMAGES = IMAGES.sort(function(a, b) {
return a.localeCompare(b);
});
$(document).ready(function() {
var $body = $("body");
var $doc = $(document);
var $win = $(window);
var $imageContainer = $(".current-image-container");
var $image = $imageContainer.find(".current-image");
var $middle = $(".middle");
var $footer = $(".footer");
var $strip = $(".strip");
var spinnerContainer = $(".spinner-container")[0];
var spinner = new Spinner(SPINNER_OPTIONS);
var smallModeOn = shouldEnableSmallMode();
var loadedThumbnails = 0;
var currentImage, lastHash;
var hashPollInterval;
var embedded = (window.parent !== window);
var disableHistory = /[&?](nohistory|n)=1/.test(window.location.search);
var back;
if (!embedded) {
$body.addClass('project-link');
}
if (embedded) {
$(window).on('message', function(event) {
event = event.originalEvent || event;
var data = event.data || { };
switch (data.message) {
case 'changeHash':
if (data.options.hash !== getHashFromUrl().substring(1)) {
render(parseHash(data.options.hash));
}
break;
case 'next': moveBy(1); break;
case 'prev': moveBy(-1); break;
}
});
}
function shouldEnableSmallMode() {
return (window.innerHeight < NORMAL_THUMB_MINIMUM_HEIGHT);
}
function notifyHashChanged(options) {
if (window.parent && window.parent.postMessage) {
var hash = generateHash(options).substring(1);
var msg = { message: 'hashChanged', options: { hash: hash } };
window.parent.postMessage(msg, "*");
}
}
function getHashFromUrl() {
/* some browsers decode the hash, we don't want that */
return "#" + (window.location.href.split("#")[1] || "");
}
function parseHash(hash) {
var result = { };
hash = hash || getHashFromUrl();
if (hash) {
if (hash.charAt(0) === '#') {
hash = hash.substring(1);
}
var parts = hash.split("+");
for (var i = 0; i < parts.length; i++) {
var keyValue = parts[i].split(":");
if (keyValue.length === 1) {
result.i = decodeURIComponent(keyValue[0]);
}
else if (keyValue.length === 2) {
result[keyValue[0]] = decodeURIComponent(keyValue[1]);
}
}
}
return result;
}
function pollHash() {
/* don't poll the hash when we're embedded -- let the outer container
control the url stack. we send events via postMessage elsewhere when
the selected image changes. */
if (!embedded && !hashPollInterval) {
hashPollInterval = setInterval(function() {
if (getHashFromUrl() !== lastHash) {
render();
}
}, 250);
}
}
function generateHash(options) {
options = options || { };
var image = options.image || $image.attr("src");
var background = options.background || $("body").css("background-color");
var result = "#" + encodeURIComponent(image);
if (background) {
result += "+b:" + encodeURIComponent(background.replace(/, /g, ","));
}
return result;
}
function writeHash(options) {
/* don't write the hash when embedded, otherwise history information
will seep up into the outer container and mess with back behavior.
we will notify the outer container via postMessage instead */
if (embedded) {
return;
}
var hash = generateHash(options);
var replace = disableHistory;
/* if there's no hash in the url yet, then don't maintain it in the
back stack. only remember the first actual image selected */
if (getHashFromUrl() === "#") {
replace = true;
}
if (hash !== getHashFromUrl() || hash !== lastHash) {
if (replace) {
var href = location.href.replace(/(javascript:|#).*$/, '');
location.replace(href + hash);
}
else {
window.location.hash = lastHash = hash;
}
lastHash = hash;
}
}
function findThumbnailForImage(filename) {
var thumbnails = $(".strip img");
for (var i = 0; i < thumbnails.length; i++) {
var $thumbnail = thumbnails.eq(i);
if ($thumbnail.attr("data-large") === filename) {
return $thumbnail;
}
}
return { addClass: function() { /* just to be safe */ } };
}
function setImage(filename) {
if ($image.attr("src") === filename) {
return false;
}
setLoading(true);
$image.attr("src", "");
currentImage = filename;
setTimeout(function() {
$image.attr("src", filename);
});
$(".strip img").removeClass("active");
findThumbnailForImage(filename).addClass("active");
updateScrollLeft();
writeHash({image: filename});
notifyHashChanged({image: filename});
}
function checkForScrollbar() {
var strip = $footer.outerHeight();
var branding = embedded ? 0 : $('.github').outerHeight();
$middle.css("bottom", strip + branding);
}
function thumbnailLoaded(event) {
checkForScrollbar();
/* once all the thumbnails have loaded, make sure the selected image
is scrolled into view */
if (++loadedThumbnails >= IMAGES.length) {
updateScrollLeft();
}
}
function updateScrollLeft(options) {
options = options || { };
var $active = $strip.find("img.active");
if ($active.length === 1) {
var offset = ($footer.width() / 2) - ($active.width() / 2);
var current = $footer.scrollLeft();
var pos = (current + $active.eq(0).position().left) - offset;
$footer.stop(true); /* stops current animations */
if (options.animate === false) {
$footer.scrollLeft(pos);
}
else {
$footer.animate({scrollLeft: pos}, 250);
}
}
}
function centerImageVertically() {
var containerHeight = $imageContainer.height();
var imageHeight = $image.height();
var offset = ((containerHeight - imageHeight) / 2 ) - 4;
offset = Math.max(offset, 0); /* no smaller than 0 */
$image.css({"margin-top": offset, "visibility": "visible"});
}
function dimensionsChanged() {
var smallMode = shouldEnableSmallMode();
if (smallMode !== smallModeOn) {
smallModeOn = smallMode;
renderThumbnails();
}
/* we use an old trick to auto-scale the image inside of its
container by setting max-width and max-height to 100%. as of
10/18/2014 this is broken in Chrome, cross platform. instead,
we need to manually set the max-height and max-width to pixel
values. hopefully this is fixed soon; this hack should be
checked and removed at some point... */
$image.css({
'max-height': $imageContainer.outerHeight(),
'max-width': $imageContainer.outerWidth()
});
centerImageVertically();
checkForScrollbar();
updateScrollLeft({animate: false});
}
function createThumbnail(filename, options) {
var small = options && options.small;
var template = small ? THUMB_TEMPLATE_SMALL : THUMB_TEMPLATE_NORMAL;
return $(
template.replace("{{large}}", filename)
.replace("{{thumb}}", ".thumbs/" + filename)
);
}
function setLoading(loading) {
loading = (loading === undefined) ? true : loading;
if (loading) {
$body.addClass("loading");
if (!spinner.spinning) {
spinner.spin(spinnerContainer);
spinner.spinning = true;
}
}
else {
$body.removeClass("loading");
if (spinner.spinning) {
spinner.stop();
spinner.spinning = false;
}
}
}
function thumbnailClicked(event) {
$image.css({"visibility": "hidden"});
if (!setImage($(event.target).attr("data-large"))) {
$image.css({"visibility": "visible"}); /* gross but cheap */
}
}
function imageLoaded(event) {
setLoading(false);
dimensionsChanged(event);
}
function imageClicked(event) {
window.open($image.attr("src"));
}
function moveBy(offset) {
var thumbnails = $(".strip img");
var current = 0;
for (var i = 0; i < thumbnails.length; i++) {
if (thumbnails.eq(i).hasClass('active')) {
current = i;
break;
}
}
/* wrap around */
var newIndex = current + offset;
if (newIndex >= thumbnails.length) {
newIndex = 0;
}
else if (newIndex < 0) {
newIndex = thumbnails.length - 1;
}
setImage(IMAGES[newIndex]);
}
function previous() {
moveBy(-1);
}
function next() {
moveBy(1);
}
function updateTitle() {
var parts = window.location.pathname.split("/");
/* last part will be empty due to trailing slash in url */
document.title = "photos - " + parts[parts.length - 2].replace(/_/g, " ");
}
function render(params) {
if (IMAGES.length === 0) {
$body.addClass('no-images');
}
else {
params = params || parseHash();
var image = params.i;
if (!image || IMAGES.indexOf(image) === -1) {
image = IMAGES[0];
}
if (params.b) {
$body.css("background-color", params.b);
}
updateTitle();
setTimeout(function() {
setImage(image);
writeHash({image: image});
pollHash();
}, 250);
}
}
function keyPressed(event) {
if (event.altKey || event.metaKey) {
return true; /* don't swallow browser back/forward shortcuts */
}
if (event.keyCode === 39) { /* right arrow */
moveBy(1);
}
else if (event.keyCode === 37) { /*left arrow */
moveBy(-1);
}
else if (embedded && event.keyCode === 38) { /* up */
window.parent.postMessage({ message: 'prevAlbum' }, "*");
}
else if (embedded && event.keyCode === 40) { /* down */
window.parent.postMessage({ message: 'nextAlbum' }, "*");
}
}
function renderThumbnails() {
$strip.empty();
for (i = 0; i < IMAGES.length; i++) {
$strip.append(createThumbnail(IMAGES[i], {
small: smallModeOn
}));
}
}
function main() {
var i;
/* add thumbnails to the strip */
renderThumbnails();
/* register events */
$image.on("load", imageLoaded);
$image.on("click", imageClicked);
$win.on("resize", dimensionsChanged);
$doc.on("click", ".strip img", thumbnailClicked);
$doc.on("click", ".button.prev", previous);
$doc.on("click", ".button.next", next);
$body.on("keydown", keyPressed);
$(".strip img").on("load", thumbnailLoaded);
$('img').on('dragstart', function(event) { event.preventDefault(); });
render();
}
main();
});
}());
</script>
</head>
<body class="loading">
<div class="middle">
<div class="pseudo-button prev">
<div class="button prev"><</div>
</div>
<div class="pseudo-button next">
<div class="button next">></div>
</div>
<div class="center">
<div class="current-image-container">
<img class="current-image">
<div class="no-images-text">there are no images here.</div>
<div class="spinner-container"></div>
</div>
</div>
</div>
<div class="footer">
<div class="strip"></div>
</div>
<div class="github">
<a href="https://github.com/clangen/cgallery" target="_new">https://github.com/clangen/cgallery</a>
</div>
</body>
</html>