-
Notifications
You must be signed in to change notification settings - Fork 5
/
gallery.php
180 lines (150 loc) · 4.83 KB
/
gallery.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
<?php
$files = scandir('pics');
$files = array_slice($files, 2);
shuffle($files);
$files = array_slice($files, 0, 25);
$pics = array();
foreach ($files as $f) {
$p = array();
$p['src'] = $f;
$p['id'] = preg_replace('/[^\d]/', '', $f);
$info = getimagesize('pics/' . $p['src']);
$p['w'] = $info[0];
$p['h'] = $info[1];
$pics[] = $p;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Photo Gallery</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
h1 {
font: bold 2.5em "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
border-bottom: 1px solid #666;
padding: 16px 0;
margin: 16px 0 32px;
}
#container {
width: 1000px;
margin: 0 auto;
}
.row {
position: relative;
}
.row:after {
content: '.';
font-size: 0;
height: 0;
visibility: hidden;
display: block;
clear: both;
}
.row-info {
position: absolute;
left: 0;
bottom: 0;
}
.pic {
float: left;
margin: 2px;
border: 1px solid #fff;
text-align: center;
-webkit-transition: box-shadow 250ms;
position: relative;
z-index: 0;
}
.pic-container {
width: 100%;
height: 100%;
background: #000;
display: none;
}
.pic-container img {
opacity: 0.85;
-webkit-transition: opacity 250ms;
}
.pic-hover:hover {
box-shadow: 0 0 32px 4px rgba(0, 0, 0, 0.9);
z-index: 1;
cursor: pointer;
}
.pic-hover:hover img {
opacity: 1;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready( function() {
var pics = $.parseJSON('<?= json_encode($pics) ?>');
var rowWidth = 1000;
var spacing = 6;
var b = 0, e = 0, rowHeight;
while (e < pics.length) {
do {
e++;
var totalWidth = 0;
for (var i = b; i < e; i++)
totalWidth += pics[i].w / pics[i].h;
rowHeight = Math.round((rowWidth - spacing * (e - b)) / totalWidth);
} while (rowHeight > 200 && e < pics.length);
var actualRowWidth = 0;
for (var i = b; i < e; i++) {
pics[i]._w = Math.round(pics[i].w / pics[i].h * rowHeight);
pics[i]._h = rowHeight;
actualRowWidth += pics[i]._w;
}
var diff = (rowWidth - spacing * (e - b)) - actualRowWidth;
var per = Math.floor(diff / (e - b));
var extra = diff % (e - b);
if (extra < 0) extra += e - b;
for (var i = b; i < e; i++) {
pics[i]._w += per;
if (i - b < extra) pics[i]._w++;
}
var rowDiv = $('<div></div>')
.addClass('row')
.appendTo('#container');
for (var i = b; i < e; i++) {
var pic = $('<div></div>')
.addClass('pic')
.css( { width: pics[i]._w, height: pics[i]._h } )
.appendTo(rowDiv);
var picContainer = $('<div></div>')
.addClass('pic-container');
var img = $(buildimg(pics[i]))
.load( function() {
$(this).parents('.pic-container').fadeIn(800, function() {
$(this).parent().addClass('pic-hover');
} );
} );
picContainer
.html(img)
.wrapInner('<a href="pic/' + pics[i].id + '" />');
pic.append(picContainer).appendTo(rowDiv);
}
b = e;
}
} );
function buildimg(pic) {
return (
'<img ' +
'src="pic/' + pic.id + '/' + pic._w + 'x' + pic._h + '/' + pic.src + '" ' +
'width="' + pic._w + '" ' +
'height="' + pic._h + '" ' +
'/>');
}
</script>
</head>
<body>
<div id="container">
<h1>Photo Gallery</h1>
</div>
</body>
</html>