r/tinycode • u/somjuan • Jan 08 '15
Extremely simple gallery. Place in a directory of images to automagically generate a responsive gallery.
This is a little piece of code that can sit in a directory of jpgs on a webserver.
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no">
<title><? echo ucwords(basename(getcwd())); //page title is same as directory ?></title>
<style type="text/css">
body {
background: #acacac;
text-align: center;
font: 9px sans-serif;
}
a { /* Seems necessary to vertically center images in webkit browsers */
display:block;
height:100vh;
width:100vw;
}
img {
display: block;
margin: auto;
max-height: 77vh;
max-width: 100vw;
outline: none;
box-shadow: 0px 0px 15px #000;
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
</style>
</head>
<body><?php
$files = glob('*.jpg');
$count = count($files);
for ($i = 0; $i < $count; $i++) {
if($i<($count-1)) { $link=$i + 1; } else { $link='0'; }
echo '<div class="image"><a id="'.$i.'" name="'.$files[$i].'" href="#'.$link.'"><img src="'.$files[$i].'" /></a></div>';
}
?>
</body>
</html><? //script credit: somjuan.com ?>3
u/redditrabbitrobbit Jan 09 '15
Tag IDs shouldn't start with numbers.
1
u/somjuan Jan 10 '15
Hm, never encountered that before. Thanks!
It shouldn't matter here since we're not styling any of those elements directly, just navigating to their anchor. But it is trivial to add any preface to the numbers, so that's probably the best thing to do.
2
u/nexe mod Jan 10 '15 edited Jan 10 '15
Things I do like:
short, easily readable code
no security risks (imho), which seems rare for PHP posts
gave credit
responded to suggestions
Things I don't like:
images don't link directly to image (browsers give you zooming and fullscreen etc already, why take it away?)
user-scalable=no ... just don't, please! Use
<meta name="viewport" content="width=device-width, initial-scale=1">insteadglob doesn't take a flag to handle filenames case insensitively (imho), which kinda sucks cause I don't wanna rename all my file extensions to either lower or upper case manually
automatic thumbnail creation would have been super sweet, maybe I'll "fork" your code and add this
2
u/somjuan Jan 10 '15
I completely understand disliking disabling user scalability. I've included it because the use case I made this for is primarily mobile. I watched a few people zoom in, and then proceed through all the pictures only able to see a small portion of each.
Please fork it! I'd love to see any additions changes made. It definitely has a limited use (since at a certain point, many people would want to use a proper gallery), but it's handy for certain folks.
1
12
u/until0 Jan 08 '15
<meta name="viewport" content="width=device-width, user-scalable=no">
This is blasphemous. I'll zoom when I want to zoom. Developers, let's stop this.
1
u/born2hula Jan 09 '15
What is this vh/vw and translate transform CSS used here? I've never heard of it.
2
u/OrangeredStilton Jan 09 '15
Viewport-percentage units, apparently supported by IE10+ and recent Webkit/Gecko: https://dev.opera.com/articles/css-viewport-units/
3
u/tehdog Jan 08 '15
Small modifications to make the for <body> shorter and more readable: