Skip to content

Instantly share code, notes, and snippets.

@jacobq
Created January 22, 2019 01:58
Show Gist options
  • Save jacobq/61d90082a3df7b56f8f89be52b3f3fef to your computer and use it in GitHub Desktop.
Save jacobq/61d90082a3df7b56f8f89be52b3f3fef to your computer and use it in GitHub Desktop.
Quick and dirty ebook library generation page
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Jay's eBook Library</title>
<meta name="description" content="Sharing knowledge for great justice and much win">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style>
body {
background-image: url("mosaic.png");
}
h1 {
padding-left: 50px;
}
ul#book-list li {
display: inline-block;
margin: 0 10px;
}
ul#book-list li a {
margin: 4px 6px 6px 6px;
}
ul#book-list li ul:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
ul#book-list li ul {
/*display: inline-block;*/
width: 100%;
margin-bottom: 6px;
padding: 0;
background-color: #ccc;
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
opacity: 0.75;
text-align: center;
vertical-align: middle;
}
ul#book-list img.cover {
border: 2px solid black;
max-width: 300px;
height: auto;
-webkit-transition: all 0.1s;
transition: all 0.1s;
transition-timing-function: ease;
}
ul#book-list > li:hover img.cover {
-webkit-box-shadow: 0px 0px 10px 2px rgba(128, 128, 128, 0.75);
-moz-box-shadow: 0px 0px 10px 2px rgba(128, 128, 128, 0.75);
box-shadow: 0px 0px 10px 2px rgba(128, 128, 128, 0.75);
-webkit-transition: box-shadow 0.1s;
transition: box-shadow 0.1s;
transition-timing-function: ease;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.2/modernizr.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<?php
// For 'dasherize' function
require("Stringy.php");
require("StaticStringy.php");
$formats = ['PDF', 'ePub', 'mobi'];
function getBook($directory) {
global $formats;
$book = ['title' => $directory];
$image = new Imagick();
foreach (preg_grep('/^([^.])/', scandir($directory)) as $item) {
$extension = strtolower(pathinfo($item, PATHINFO_EXTENSION));
if ($item === "cover.jpg") {
$book["cover"] = $directory . "/" . $item;
$image->readImage($book["cover"]);
}
else {
foreach ($formats as $format) {
if ($extension == strtolower($format))
$book[$format] = $directory . "/" . $item;
}
}
}
if (!isset($book["cover"]) && isset($book["PDF"])) {
// !FIXME: For some reason it seems that this code runs into a problem when processing the "Building Web Apps with Ember.js" PDF:
// it generates an image that looks like the front and back covers side-by-side and the "aqua" color is closer to pure green.
$image->setResolution(72, 72);
$image->readImage($book["PDF"] . '[0]'); // [0] for first page
$image = $image->flattenImages();
$image->setImageCompressionQuality(75);
$image->setImageFormat("jpg");
$image->writeImage($directory . '/cover.jpg');
}
if ($image) {
$book["coverWidth"] = $image->getImageWidth();
$book["coverHeight"] = $image->getImageHeight();
}
return $book;
}
$filesAndDirectories = preg_grep('/^([^.])/', scandir("."));
$books = [];
foreach ($filesAndDirectories as $item) {
if (is_dir($item)) {
try {
array_push($books, getBook($item));
} catch (Exception $e) {
// Silently ignore errors from getBook, which may happen while files are uploading
}
}
}
echo '<h1>Jay\'s eBook Library</h1>';
echo '<ul id="book-list">';
foreach ($books as $book) {
echo "<li id=\"" . Stringy\StaticStringy::dasherize($book["title"]) . "\">";
echo '<img class="cover" alt="' . $book["title"] . '" src="' . $book["cover"] . '" width="' . $book["coverWidth"] . '" height="' . $book["coverHeight"] . '">';
echo "<ul>";
foreach ($formats as $format) {
if (isset($book[$format]))
echo '<li><a class="btn btn-default" href="' . $book[$format] . '">' . $format . '</a></li>';
}
echo "</ul>";
echo "</li>";
}
echo '</ul>';
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment