Skip to content

Instantly share code, notes, and snippets.

@natebeaty
Last active April 25, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natebeaty/21b5d93d550330e5e1dd30d600524ff0 to your computer and use it in GitHub Desktop.
Save natebeaty/21b5d93d550330e5e1dd30d600524ff0 to your computer and use it in GitHub Desktop.
untested comic image PHP hooha for alec
<?php
// Pull all files in a directory into an array
$comic_dir = $_GET['comic']; // something like '001'
$files_in_dir = dirList("/archive/$comic_dir/",'\d+\.\w{3}'); // assumes files are named 5.jpg, can adjust regex if you have other files in dir you don't want to output (like a cover image)
function pageJumpLinks($num_pages) {
$output = '';
for ($i=0; $i < $num_pages; $i++) {
$output .= '<a href="#' . $i . '">' . $i . '</a> | ';
}
return $output;
}
function comicPages($pages_arr, $comic_dir) {
$output = '';
$i = 0;
foreach ($pages_arr as $page) {
$i++;
list($width, $height) = @getimagesize($_SERVER['DOCUMENT_ROOT'] . '/archive/' . $comic_dir . '/' . $page);
$output .= '<p><img style="height:' . $height . 'px; width: ' . $width . 'px;" src="/archive/' . $comic_dir . '/' . $page . '" id="' . $i . '"></p>' . "\n";
}
return $output;
}
function dirList($dir_path, $filter='') {
$rel_path = $_SERVER['DOCUMENT_ROOT'] . $dir_path;
$files_arr = array();
if (is_dir($rel_path)) {
if ($handle = opendir($rel_path)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && !preg_match('/^\./',$file) && preg_match("/$filter/i",$file)) {
$file = trim($file);
if (!is_dir("$rel_path/$file")) {
$files_arr[] = $file;
}
}
}
closedir($handle);
}
}
return($files_arr);
}
?>
<html> etc...
<?php
// This could go down below in your template
if(empty($files_in_dir)) {
echo '<p>No images found for comic.</p>';
} else {
// Alphanumeric sort array
sort($files_in_dir);
// Show page jump nav (can put this wherever needed to output nav)
echo pageJumpLinks(count($files_in_dir));
// Show images (put wherever needed)
echo comicPages($files_in_dir, $comic_dir);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment