Thumbnails and image preview.

This commit is contained in:
Lars Jung 2012-05-11 18:54:52 +02:00
parent ce3ef3a195
commit 4cb9967b39
65 changed files with 1510 additions and 806 deletions

View file

@ -1,9 +1,9 @@
<?php
require_once(str_replace("\\", "/", __DIR__) . "/inc/H5ai.php");
require_once(str_replace("\\", "/", dirname(__FILE__)) . "/inc/H5ai.php");
$h5ai = new H5ai();
$h5ai = new H5ai(__FILE__);
$options = $h5ai->getOptions();
@ -41,24 +41,20 @@ if ($action === "getthumbsrc") {
json_fail(1, "thumbnails disabled");
}
list($srcAbsHref, $width, $height, $mode) = check_keys(array("href", "width", "height", "mode"));
H5ai::req_once("/php/inc/Thumbnail.php");
H5ai::req_once("/php/inc/Image.php");
$srcAbsPath = $h5ai->getRootAbsPath() . rawurldecode($srcAbsHref);
if (!Thumbnail::isUsable()) {
H5ai::req_once("/php/inc/Thumb.php");
if (!Thumb::is_supported()) {
json_fail(2, "thumbnails not supported");
}
$thumbnail = new Thumbnail($h5ai, $srcAbsHref, $mode, $width, $height);
$thumbnail->create(1);
if (!file_exists($thumbnail->getPath())) {
list($type, $srcAbsHref, $mode, $width, $height) = check_keys(array("type", "href", "mode", "width", "height"));
$thumb = new Thumb($h5ai);
$thumbHref = $thumb->thumb($type, $srcAbsHref, $mode, $width, $height);
if ($thumbHref === null) {
json_fail(3, "thumbnail creation failed");
}
json_exit(array("absHref" => $thumbnail->getHref()));
json_exit(array("absHref" => $thumbHref));
}
@ -102,22 +98,46 @@ else if ($action === "getarchive") {
else if ($action === "getchecks") {
$php = $h5ai->checks["php"];
$cache = $php && $h5ai->checks["cache"];
$temp = $php && $h5ai->checks["temp"];
$php = version_compare(PHP_VERSION, "5.2.1") >= 0;
$archive = class_exists("PharData");
$gd = false;
if (function_exists("gd_info")) {
$gdinfo = gd_info();
$gd = array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"];
}
$cache = is_writable($h5ai->getH5aiAbsPath() . "/cache");
$temp = is_writable(sys_get_temp_dir());
$tar = preg_match("/tar$/", `which tar`) > 0;
$zip = preg_match("/zip$/", `which zip`) > 0;
$convert = preg_match("/convert$/", `which convert`) > 0;
$ffmpeg = preg_match("/ffmpeg$/", `which ffmpeg`) > 0;
$du = preg_match("/du$/", `which du`) > 0;
json_exit(array(
"php" => $php,
"cache" => $cache,
"thumbs" => $cache && $h5ai->checks["gd"],
"thumbs" => $gd,
"temp" => $temp,
"archive" => $temp && $h5ai->checks["archive"],
"tar" => $temp && $h5ai->checks["tar"],
"zip" => $temp && $h5ai->checks["zip"]
"archive" => $archive,
"tar" => $tar,
"zip" => $zip,
"convert" => $convert,
"ffmpeg" => $ffmpeg,
"du" => $du
));
}
else if ($action === "getentries") {
list($href, $content) = check_keys(array("href", "content"));
$content = intval($content, 10);
json_exit(array("entries" => $h5ai->getEntries($href, $content)));
}
else {
json_fail(100, "unsupported action");
}

View file

@ -0,0 +1,70 @@
<?php header("Content-type: text/html;h5ai=%BUILD_VERSION%"); ?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Directory index · styled with h5ai</title>
<meta name="description" content="Directory index styled with h5ai (http://larsjung.de/h5ai)">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/png" href="/_h5ai/images/h5ai-16x16.png">
<link rel="apple-touch-icon" type="image/png" href="/_h5ai/images/h5ai-48x48.png">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ubuntu:regular,italic,bold">
<link rel="stylesheet" href="/_h5ai/css/styles.css">
<script src="/_h5ai/js/modernizr-2.5.3.min.js"></script>
</head>
<body id="h5ai-main">
<div id="topbar" class="clearfix">
<ul id="navbar"></ul>
</div>
<div id="content">
<div id="extended" class="clearfix"></div>
</div>
<div id="bottombar" class="clearfix">
<span class="left">
<a id="h5ai-reference" href="http://larsjung.de/h5ai" title="h5ai project page">h5ai %BUILD_VERSION%</a>
<span class="hideOnJs noJsMsg"> JavaScript is disabled! </span>
<span class="oldBrowser"> Some features disabled! Works best in <a href="http://browsehappy.com">modern browsers</a>. </span>
</span>
<span class="right"></span>
<span class="center"></span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/_h5ai/js/jquery-1.7.2.min.js"><\/script>')</script>
<script src="/_h5ai/config.js"></script>
<script src="/_h5ai/js/scripts.js"></script>
<div id="data-generic-json" class="hidden">
<?php if (stripos($_SERVER["REQUEST_METHOD"], "HEAD") === false) {
$h5ai_php = str_replace("\\", "/", dirname(__FILE__)) . "/inc/H5ai.php";
if (!file_exists($h5ai_php)) {
function find_h5ai($path, $h5ai) {
if (file_exists($path . $h5ai)) {
return $path . $h5ai;
}
$parent = str_replace("\\", "/", dirname($path));
if ($parent !== $path) {
return find_h5ai($parent, $h5ai);
}
error_log("h5ai not found: " . __FILE__);
}
$h5ai_php = find_h5ai(str_replace("\\", "/", dirname(__FILE__)), "/_h5ai/php/inc/H5ai.php");
}
require_once($h5ai_php);
$h5ai = new H5ai(__FILE__);
echo $h5ai->getGenericJson();
} ?>
</div>
</body>
</html>

View file

@ -21,7 +21,7 @@ class Archive {
$this->files = array();
$this->sc401 = false;
$this->addHrefs($hrefs);
$this->add_hrefs($hrefs);
if ($this->sc401) {
return 401;
@ -55,7 +55,7 @@ class Archive {
$archive->addEmptyDir($archivedDir);
}
foreach ($this->files as $realFile => $archivedFile) {
$archive->addFile($realFile, $archivedFile); // very, very slow :/
$archive->add_file($realFile, $archivedFile); // very, very slow :/
}
}
@ -67,7 +67,7 @@ class Archive {
}
private function addHrefs($hrefs) {
private function add_hrefs($hrefs) {
foreach ($hrefs as $href) {
@ -79,22 +79,22 @@ class Archive {
$this->sc401 = true;
}
if ($code == "h5ai" && !$this->h5ai->ignoreThisFile($n)) {
if ($code == "h5ai" && !$this->h5ai->is_ignored($n)) {
$realFile = $this->h5ai->getAbsPath($href);
$archivedFile = preg_replace("!^" . H5ai::normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile);
if (is_dir($realFile)) {
$this->addDir($realFile, $archivedFile);
$this->add_dir($realFile, $archivedFile);
} else {
$this->addFile($realFile, $archivedFile);
$this->add_file($realFile, $archivedFile);
}
}
}
}
private function addFile($realFile, $archivedFile) {
private function add_file($realFile, $archivedFile) {
if (is_readable($realFile)) {
$this->files[$realFile] = $archivedFile;
@ -102,7 +102,7 @@ class Archive {
}
private function addDir($realDir, $archivedDir) {
private function add_dir($realDir, $archivedDir) {
$code = $this->h5ai->getHttpCode($this->h5ai->getAbsHref($realDir));
if ($code == 401) {
@ -112,16 +112,16 @@ class Archive {
if ($code == "h5ai") {
$this->dirs[] = $archivedDir;
$files = $this->h5ai->readDir($realDir);
$files = $this->h5ai->read_dir($realDir);
foreach ($files as $file) {
$realFile = $realDir . "/" . $file;
$archivedFile = $archivedDir . "/" . $file;
if (is_dir($realFile)) {
$this->addDir($realFile, $archivedFile);
$this->add_dir($realFile, $archivedFile);
} else {
$this->addFile($realFile, $archivedFile);
$this->add_file($realFile, $archivedFile);
}
}
}

View file

@ -1,99 +0,0 @@
<?php
##############################################
# taken from here:
# http://www.jongales.com/blog/2009/02/18/simple-file-based-php-cache-class/
# with minor modifications
##############################################
class Cache {
private $dir;
function __construct($dir) {
$this->dir = $dir;
}
private function _name($key) {
return $this->dir . "/" . sha1($key);
}
public function get($key, $expiration = 3600) {
if (!is_dir($this->dir) || !is_writable($this->dir)) {
return false;
}
$cache_path = $this->_name($key);
if (!@file_exists($cache_path)) {
return false;
}
if (filemtime($cache_path) < (time() - $expiration)) {
$this->clear($key);
return false;
}
if (!$fp = @fopen($cache_path, "rb")) {
return false;
}
flock($fp, LOCK_SH);
$cache = "";
if (filesize($cache_path) > 0) {
$cache = unserialize(fread($fp, filesize($cache_path)));
} else {
$cache = null;
}
flock($fp, LOCK_UN);
fclose($fp);
return $cache;
}
public function set($key, $data) {
if (!is_dir($this->dir) || !is_writable($this->dir)) {
return false;
}
$cache_path = $this->_name($key);
if (! $fp = fopen($cache_path, "wb")) {
return false;
}
if (flock($fp, LOCK_EX)) {
fwrite($fp, serialize($data));
flock($fp, LOCK_UN);
} else {
return false;
}
fclose($fp);
@chmod($cache_path, 0777);
return true;
}
public function clear($key) {
$cache_path = $this->_name($key);
if (file_exists($cache_path)) {
unlink($cache_path);
return true;
}
return false;
}
}
?>

View file

@ -2,11 +2,13 @@
class Entry {
private static $FOLDER_SIZE_CMD = "du -sb \"[DIR]\"";
private static $cache = array();
public static function getCache() {
public static function get_cache() {
return Entry::$cache;
}
@ -24,16 +26,18 @@ class Entry {
public static function sort() {
uasort(Entry::$cache, function ($entry1, $entry2) {
function cmp($entry1, $entry2) {
return strcasecmp($entry1->absHref, $entry2->absHref);
});
}
uasort(Entry::$cache, "cmp");
}
public $h5ai, $absPath, $absHref, $date, $size, $isFolder, $parent;
public $h5ai, $absPath, $absHref, $date, $size, $isFolder, $parent, $isContentFetched;
private function __construct($h5ai, $absPath, $absHref) {
@ -49,6 +53,11 @@ class Entry {
if ($this->isFolder) {
$this->size = null;
$options = $h5ai->getOptions();
if ($options["foldersize"]["enabled"]) {
$cmd = str_replace("[DIR]", $this->absPath, Entry::$FOLDER_SIZE_CMD);
$this->size = intval(preg_replace("/\s.*$/", "", `$cmd`), 10);
}
} else {
$this->size = filesize($this->absPath);
}
@ -58,6 +67,8 @@ class Entry {
$this->parent = Entry::get($this->h5ai, H5ai::normalize_path(dirname($this->absPath)), H5ai::normalize_path(dirname($this->absHref), true));
}
$this->isContentFetched = false;
Entry::$cache[$this->absHref] = $this;
}
@ -72,6 +83,7 @@ class Entry {
if ($withStatus && $this->isFolder) {
$obj["status"] = $this->h5ai->getHttpCode($this->absHref);
$obj["content"] = $this->isContentFetched;
}
return $obj;
@ -92,12 +104,14 @@ class Entry {
return $content;
}
$files = $this->h5ai->readDir($this->absPath);
$files = $this->h5ai->read_dir($this->absPath);
foreach ($files as $file) {
$entry = Entry::get($this->h5ai, $this->absPath . "/" . $file, $this->absHref . rawurlencode($file));
$content[$entry->absPath] = $entry;
}
$this->isContentFetched = true;
return $content;
}
}

View file

@ -5,7 +5,6 @@ define("H5AI_ABS_PATH", H5ai::normalize_path(dirname(dirname(dirname(__FILE__)))
H5ai::req_once("/config.php");
H5ai::req_once("/php/inc/Cache.php");
H5ai::req_once("/php/inc/Entry.php");
@ -25,18 +24,6 @@ class H5ai {
}
public static final function starts_with($sequence, $start) {
return strcasecmp(substr($sequence, 0, strlen($start)), $start) === 0;
}
public static final function ends_with($sequence, $end) {
return strcasecmp(substr($sequence, -strlen($end)), $end) === 0;
}
private static final function load_config($file) {
$str = file_exists($file) ? file_get_contents($file) : "";
@ -51,26 +38,30 @@ class H5ai {
}
private static $H5AI_CONTENT_TYPE = "Content-Type: text/html;h5ai=";
private $h5aiAbsPath,
$rootAbsPath, $ignore, $ignoreRE,
private $requested_from,
$h5aiAbsPath,
$rootAbsPath, $ignore_names, $ignore_patterns, $index_files,
$config, $options,
$rootAbsHref, $h5aiAbsHref,
$absHref, $absPath,
$cache;
public $checks;
$absHref, $absPath;
public function __construct() {
public function __construct($requested_from) {
$this->requested_from = H5ai::normalize_path($requested_from);
$this->h5aiAbsPath = H5ai::normalize_path(H5AI_ABS_PATH);
global $H5AI_CONFIG;
$this->rootAbsPath = H5ai::normalize_path($H5AI_CONFIG["ROOT_ABS_PATH"]);
$this->ignore = $H5AI_CONFIG["IGNORE"];
$this->ignoreRE = $H5AI_CONFIG["IGNORE_PATTERNS"];
$this->ignore_names = $H5AI_CONFIG["IGNORE"];
$this->ignore_patterns = $H5AI_CONFIG["IGNORE_PATTERNS"];
$this->index_files = $H5AI_CONFIG["INDEX_FILES"];
$this->config = H5ai::load_config($this->h5aiAbsPath . "/config.js");
$this->options = $this->config["options"];
@ -78,20 +69,8 @@ class H5ai {
$this->rootAbsHref = H5ai::normalize_path($this->options["rootAbsHref"], true);
$this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true);
$this->absHref = H5ai::normalize_path(preg_replace('/\\?.*/', '', getenv("REQUEST_URI")), true);
$this->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true);
$this->absPath = $this->getAbsPath($this->absHref);
$this->cache = new Cache($this->h5aiAbsPath . "/cache");
$this->checks = array(
"php" => version_compare(PHP_VERSION, "5.2.0") >= 0,
"archive" => class_exists("PharData"),
"gd" => GD_VERSION != "GD_VERSION",
"cache" => is_writable($this->h5aiAbsPath . "/cache"),
"temp" => is_writable(sys_get_temp_dir()),
"tar" => preg_match("/tar$/", `which tar`) > 0,
"zip" => preg_match("/zip$/", `which zip`) > 0
);
}
@ -119,12 +98,6 @@ class H5ai {
}
public function api() {
return $this->h5aiAbsHref . "php/api.php";
}
public function getOptions() {
return $this->options;
@ -161,18 +134,18 @@ class H5ai {
}
public function ignoreThisFile($file) {
public function is_ignored($name) {
// always ignore
if ($file === "." || $file === ".." || H5ai::starts_with($file, '.ht')) {
if ($name === "." || $name === "..") {
return true;
}
if (in_array($file, $this->ignore)) {
if (in_array($name, $this->ignore_names)) {
return true;
}
foreach ($this->ignoreRE as $re) {
if (preg_match($re, $file)) {
foreach ($this->ignore_patterns as $re) {
if (preg_match($re, $name)) {
return true;
}
}
@ -181,13 +154,13 @@ class H5ai {
}
public function readDir($path) {
public function read_dir($path) {
$content = array();
if (is_dir($path)) {
if ($dir = opendir($path)) {
while (($file = readdir($dir)) !== false) {
if (!$this->ignoreThisFile($file)) {
if (!$this->is_ignored($file)) {
$content[] = $file;
}
}
@ -200,25 +173,6 @@ class H5ai {
public function getHttpCode($absHref) {
//return $this->cachedHttpCode($absHref);
return $this->fetchHttpCode($absHref);
}
public function cachedHttpCode($absHref) {
$cached = $this->cache->get($absHref);
if ($cached === false) {
$code = $this->fetchHttpCode($absHref);
$cached = array("href" => $absHref, "code" => $code);
$this->cache->set($absHref, $cached);
}
return $cached["code"];
}
public function fetchHttpCode($absHref) {
if (!is_dir($this->getAbsPath($absHref))) {
return null;
}
@ -230,8 +184,30 @@ class H5ai {
}
}
$contentType = "Content-Type:";
$h5aiContentType = "Content-Type: text/html;h5ai=";
// return $this->fetchHttpCode($absHref);
return $this->guessHttpCode($absHref);
}
public function guessHttpCode($absHref) {
$absPath = $this->getAbsPath($absHref);
foreach ($this->index_files as $if) {
if (file_exists($absPath . "/" . $if)) {
if ($if === "index.php") {
$fileheader = file_get_contents($absPath . "/" . $if, false, null, -1, 50);
return stripos($fileheader, H5ai::$H5AI_CONTENT_TYPE) === false ? 200 : "h5ai";
}
return 200;
}
}
return "h5ai";
}
public function fetchHttpCode($absHref) {
$host = getenv("HTTP_HOST");
$port = getenv("SERVER_PORT");
$msg = "HEAD $absHref HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n";
@ -251,10 +227,10 @@ class H5ai {
$content = fgets($socket);
$code = intval(trim(substr($content, 9, 4)));
if ($code === 200) {
while (! H5ai::starts_with($content, $contentType)) {
while ($content !== false && stripos($content, "Content-Type") === false) {
$content = fgets($socket);
}
if (H5ai::starts_with($content, $h5aiContentType)) {
if (stripos($content, H5ai::$H5AI_CONTENT_TYPE) !== false) {
$code = "h5ai";
}
}
@ -272,31 +248,49 @@ class H5ai {
public function getGenericJson() {
$entries = $this->getEntries($this->absHref, 1);
$header = $this->options["custom"]["header"];
$footer = $this->options["custom"]["footer"];
$header = $this->fileExists($header ? $this->absPath . "/" . $header : null) ? $header : null;
$footer = $this->fileExists($footer ? $this->absPath . "/" . $footer : null) ? $footer : null;
// collect and sort entries
$folder = Entry::get($this, $this->absPath, $this->absHref);
while ($folder !== null) {
$json = array(
"entries" => $entries,
"customHeader" => $header,
"customFooter" => $footer,
"mode" => $this->requested_from === $this->h5aiAbsPath . "/php/h5ai-index.php" ? "php" : "idx.php",
"server" => array(
"name" => strtolower(preg_replace("/\\/.*$/", "", getenv("SERVER_SOFTWARE"))),
"version" => strtolower(preg_replace("/^.*\\//", "", preg_replace("/\\s.*$/", "", getenv("SERVER_SOFTWARE"))))
)
);
return json_encode($json) . "\n";
}
public function getEntries($absHref, $content) {
$folder = Entry::get($this, $this->getAbsPath($absHref), $absHref);
if ($content > 1 && $folder !== null) {
foreach ($folder->getContent() as $entry) {
$entry->getContent();
}
$folder = $folder->getParent();
}
while ($content > 0 && $folder !== null) {
$folder->getContent();
$folder = $folder->getParent();
}
Entry::sort();
$entries = array();
foreach(Entry::getCache() as $entry) {
foreach (Entry::get_cache() as $entry) {
$entries[] = $entry->toJsonObject(true);
}
$json = array(
"entries" => $entries,
"customHeader" => $header,
"customFooter" => $footer
);
return json_encode($json) . "\n";
return $entries;
}
}

View file

@ -1,225 +0,0 @@
<?php
class Image {
private $sourceFile, $source, $width, $height, $type, $dest;
public static function isUsable() {
return GD_VERSION != "GD_VERSION";
}
public static function showImage($filename) {
$image = file_get_contents($filename);
header("content-type: image");
echo $image;
}
public function __construct($filename = null) {
$this->sourceFile = null;
$this->source = null;
$this->width = null;
$this->height = null;
$this->type = null;
$this->dest = null;
$this->setSource($filename);
}
public function __destruct() {
$this->releaseSource();
$this->releaseDest();
}
public function setSource($filename) {
$this->releaseSource();
$this->releaseDest();
if (is_null($filename)) {
return;
}
$this->sourceFile = $filename;
list($this->width, $this->height, $this->type) = @getimagesize($this->sourceFile);
if (!$this->width || !$this->height) {
$this->sourceFile = null;
$this->width = null;
$this->height = null;
$this->type = null;
return;
}
$this->source = imagecreatefromstring(file_get_contents($this->sourceFile));
}
public function showDest() {
if (!is_null($this->dest)) {
header("Content-type: image/jpeg");
imagejpeg($this->dest, null, 100);
}
}
public function saveDest($filename) {
if (!is_null($this->dest)) {
@imagejpeg($this->dest, $filename, 90);
@chmod($filename, 0775);
}
}
public function releaseDest() {
if (!is_null($this->dest)) {
@imagedestroy($this->dest);
$this->dest = null;
}
}
public function releaseSource() {
if (!is_null($this->source)) {
@imagedestroy($this->source);
$this->sourceFile = null;
$this->source = null;
$this->width = null;
$this->height = null;
$this->type = null;
}
}
private function magic($destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight, $canWidth = null, $canHeight = null, $color = null) {
if (is_null($this->source)) {
return;
}
if (!is_null($canWidth) && !is_null($canHeight)) {
$this->dest = imagecreatetruecolor($canWidth, $canHeight);
} else {
$this->dest = imagecreatetruecolor($destWidth, $destHeight);
}
if (is_null($color)) {
$color = array(255, 255, 255);
}
$icol = imagecolorallocate($this->dest, $color[0], $color[1], $color[2]);
imagefill($this->dest, 0, 0, $icol);
imagecopyresampled($this->dest, $this->source, $destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight);
}
public function thumb($mode, $width, $height = null, $color = null) {
if ($height === null) {
$height = $width;
}
if ($mode === "square") {
$this->squareThumb($width);
} elseif ($mode === "rational") {
$this->rationalThumb($width, $height);
} elseif ($mode === "center") {
$this->centerThumb($width, $height, $color);
} else {
$this->freeThumb($width, $height);
}
}
public function squareThumb($width) {
if (is_null($this->source)) {
return;
}
$a = min($this->width, $this->height);
$x = intval(($this->width - $a) / 2);
$y = intval(($this->height - $a) / 2);
$this->magic(0, 0, $x, $y, $width, $width, $a, $a);
}
public function rationalThumb($width, $height) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
}
public function centerThumb($width, $height, $color = null) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$x = intval(($width - $w) / 2);
$y = intval(($height - $h) / 2);
$this->magic($x, $y, 0, 0, $w, $h, $this->width, $this->height, $width, $height, $color);
}
public function freeThumb($width, $height) {
if (is_null($this->source)) {
return;
}
$w = intval($width);
$h = intval($height);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
}
}
?>

380
src/_h5ai/php/inc/Thumb.php Normal file
View file

@ -0,0 +1,380 @@
<?php
class Thumb {
private static $FFMPEG = "ffmpeg -i \"[SOURCE]\" -an -ss 3 -vframes 1 \"[TARGET]\"";
private static $CONVERT = "convert -strip \"[SOURCE][0]\" \"[TARGET]\"";
public static final function is_supported() {
if (!function_exists("gd_info")) {
return false;
}
$gdinfo = gd_info();
return array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"];
}
private $h5ai;
public function __construct($h5ai) {
$this->h5ai = $h5ai;
}
public function thumb($type, $sourceAbsHref, $mode, $width, $height) {
$sourceAbsPath = $this->h5ai->getAbsPath($sourceAbsHref);
if ($type === "img") {
$captureAbsPath = $sourceAbsPath;
} else if ($type === "mov") {
$captureAbsPath = $this->capture(Thumb::$FFMPEG, $sourceAbsPath);
} else if ($type === "doc") {
$captureAbsPath = $this->capture(Thumb::$CONVERT, $sourceAbsPath);
}
return $this->thumb_href($captureAbsPath, $mode, $width, $height);
}
private function thumb_href($sourceAbsPath, $mode, $width, $height) {
if (!file_exists($sourceAbsPath)) {
return null;
}
$name = "cache/thumb-" . sha1("$sourceAbsPath-$width-$height-$mode") . ".jpg";
// $name = "cache/thumb-" . sha1("$sourceAbsPath-$width-$height-$mode") . ".png";
$thumbAbsHref = $this->h5ai->getH5aiAbsHref() . $name;
$thumbAbsPath = $this->h5ai->getH5aiAbsPath() . "/" . $name;
if (!file_exists($thumbAbsPath) || filemtime($sourceAbsPath) >= filemtime($thumbAbsPath)) {
$image = new Image();
$image->setSource($sourceAbsPath);
$image->thumb($mode, $width, $height);
$image->saveDestJpeg($thumbAbsPath, 80);
// $image->saveDestPng($thumbAbsPath, 9);
// Magic::thumb($mode, $sourceAbsPath, $thumbAbsPath, $width, $height);
}
return file_exists($thumbAbsPath) ? $thumbAbsHref : null;
}
private function capture($cmd, $sourceAbsPath) {
if (!file_exists($sourceAbsPath)) {
return null;
}
$captureAbsPath = $this->h5ai->getH5aiAbsPath() . "/cache/capture-" . sha1($sourceAbsPath) . ".jpg";
if (!file_exists($captureAbsPath) || filemtime($sourceAbsPath) >= filemtime($captureAbsPath)) {
$cmd = str_replace("[SOURCE]", $sourceAbsPath, $cmd);
$cmd = str_replace("[TARGET]", $captureAbsPath, $cmd);
`$cmd`;
}
return file_exists($captureAbsPath) ? $captureAbsPath : null;
}
}
class Magic {
private static $GET_SIZE_CMD = "identify -format \"%w %h\" \"[SOURCE]\"";
private static $RESIZE_CMD = "convert -strip -transparent-color \"#ffffff\" -resize [WIDTH]x[HEIGHT] -quality 80 \"[SOURCE]\" \"[TARGET]\"";
private static $SQUARE_CMD = "convert -strip -transparent-color \"#ffffff\" -crop [CWIDTH]x[CWIDTH]+[CLEFT]+[CTOP] -resize [WIDTH]x[WIDTH] -quality 80 \"[SOURCE]\" \"[TARGET]\"";
private static final function img_size($source) {
$cmd = str_replace("[SOURCE]", str_replace("\"", "\\\"", $source), Magic::$GET_SIZE_CMD);
$size = explode(" ", `$cmd`);
$size[0] = intval($size[0]);
$size[1] = intval($size[1]);
return $size;
}
private static final function rational($source, $target, $width, $height) {
$cmd = str_replace("[SOURCE]", str_replace("\"", "\\\"", $source), Magic::$RESIZE_CMD);
$cmd = str_replace("[TARGET]", str_replace("\"", "\\\"", $target), $cmd);
$cmd = str_replace("[WIDTH]", $width, $cmd);
$cmd = str_replace("[HEIGHT]", $height, $cmd);
`$cmd`;
}
private static final function square($source, $target, $width) {
$size = Magic::img_size($source);
$w = $size[0];
$h = $size[1];
$cwidth = min($w, $h);
$cleft = ($w - $cwidth) / 2;
$ctop = ($h - $cwidth) / 2;
$cmd = str_replace("[SOURCE]", str_replace("\"", "\\\"", $source), Magic::$SQUARE_CMD);
$cmd = str_replace("[TARGET]", str_replace("\"", "\\\"", $target), $cmd);
$cmd = str_replace("[CWIDTH]", $cwidth, $cmd);
$cmd = str_replace("[CLEFT]", $cleft, $cmd);
$cmd = str_replace("[CTOP]", $ctop, $cmd);
$cmd = str_replace("[WIDTH]", $width, $cmd);
`$cmd`;
}
public static final function thumb($mode, $source, $target, $width, $height = null, $color = null) {
if ($height === null) {
$height = $width;
}
if ($mode === "square") {
Magic::square($source, $target, $width);
} elseif ($mode === "rational") {
Magic::rational($source, $target, $width, $height);
}
}
}
class Image {
private $sourceFile, $source, $width, $height, $type, $dest;
public static final function is_supported() {
if (!function_exists("gd_info")) {
return false;
}
$gdinfo = gd_info();
return array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"];
}
public function __construct($filename = null) {
$this->sourceFile = null;
$this->source = null;
$this->width = null;
$this->height = null;
$this->type = null;
$this->dest = null;
$this->setSource($filename);
}
public function __destruct() {
$this->releaseSource();
$this->releaseDest();
}
public function setSource($filename) {
$this->releaseSource();
$this->releaseDest();
if (is_null($filename)) {
return;
}
$this->sourceFile = $filename;
list($this->width, $this->height, $this->type) = @getimagesize($this->sourceFile);
if (!$this->width || !$this->height) {
$this->sourceFile = null;
$this->width = null;
$this->height = null;
$this->type = null;
return;
}
$this->source = imagecreatefromstring(file_get_contents($this->sourceFile));
}
public function saveDestJpeg($filename, $quality = 80) {
if (!is_null($this->dest)) {
@imagejpeg($this->dest, $filename, $quality);
@chmod($filename, 0775);
}
}
public function saveDestPng($filename, $quality = 9) {
if (!is_null($this->dest)) {
@imagepng($this->dest, $filename, $quality);
@chmod($filename, 0775);
}
}
public function releaseDest() {
if (!is_null($this->dest)) {
@imagedestroy($this->dest);
$this->dest = null;
}
}
public function releaseSource() {
if (!is_null($this->source)) {
@imagedestroy($this->source);
$this->sourceFile = null;
$this->source = null;
$this->width = null;
$this->height = null;
$this->type = null;
}
}
private function magic($destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight, $canWidth = null, $canHeight = null, $color = null) {
if (is_null($this->source)) {
return;
}
if ($canWidth === 0) {
$canWidth = 1;
}
if ($canHeight === 0) {
$canHeight = 1;
}
if ($destWidth === 0) {
$destWidth = 1;
}
if ($destHeight === 0) {
$destHeight = 1;
}
if (!is_null($canWidth) && !is_null($canHeight)) {
$this->dest = imagecreatetruecolor($canWidth, $canHeight);
} else {
$this->dest = imagecreatetruecolor($destWidth, $destHeight);
}
if (is_null($color)) {
$color = array(255, 255, 255);
}
$icol = imagecolorallocate($this->dest, $color[0], $color[1], $color[2]);
imagefill($this->dest, 0, 0, $icol);
imagecopyresampled($this->dest, $this->source, $destX, $destY, $srcX, $srcY, $destWidth, $destHeight, $srcWidth, $srcHeight);
}
public function thumb($mode, $width, $height = null, $color = null) {
if ($height === null) {
$height = $width;
}
if ($mode === "square") {
$this->squareThumb($width);
} elseif ($mode === "rational") {
$this->rationalThumb($width, $height);
} elseif ($mode === "center") {
$this->centerThumb($width, $height, $color);
} else {
$this->freeThumb($width, $height);
}
}
public function squareThumb($width) {
if (is_null($this->source)) {
return;
}
$a = min($this->width, $this->height);
$x = intval(($this->width - $a) / 2);
$y = intval(($this->height - $a) / 2);
$this->magic(0, 0, $x, $y, $width, $width, $a, $a);
}
public function rationalThumb($width, $height) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
}
public function centerThumb($width, $height, $color = null) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$x = intval(($width - $w) / 2);
$y = intval(($height - $h) / 2);
$this->magic($x, $y, 0, 0, $w, $h, $this->width, $this->height, $width, $height, $color);
}
public function freeThumb($width, $height) {
if (is_null($this->source)) {
return;
}
$w = intval($width);
$h = intval($height);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
}
}
?>

View file

@ -1,65 +0,0 @@
<?php
H5ai::req_once("/php/inc/Image.php");
class Thumbnail {
private $srcAbsHref, $srcAbsPath, $width, $height, $name, $href, $path;
public static function isUsable() {
return Image::isUsable();
}
public function __construct($h5ai, $absHref, $mode, $width, $height) {
$this->h5ai = $h5ai;
$this->srcAbsHref = $absHref;
$this->srcAbsPath = $this->h5ai->getRootAbsPath() . urldecode($absHref);
$this->width = $width;
$this->height = $height;
$this->mode = $mode;
$this->name = sha1("$this->srcAbsPath-$this->width-$this->height-$this->mode");
$this->href = $this->h5ai->getH5aiAbsHref() . "cache/thumb-" . $this->name . ".jpg";
$this->path = $this->h5ai->getRootAbsPath() . $this->href;
$this->liveHref = $this->h5ai->api() . "?action=thumb&href=" . $this->srcAbsHref . "&width=" . $this->width . "&height=" . $this->height . "&mode=" . $this->mode;
}
public function create($force = 0) {
if (
$force === 2
|| ($force === 1 && !file_exists($this->path))
|| (file_exists($this->path) && filemtime($this->srcAbsPath) >= filemtime($this->path))
) {
$image = new Image();
$image->setSource($this->srcAbsPath);
$image->thumb($this->mode, $this->width, $this->height);
$image->saveDest($this->path);
}
}
public function getHref() {
return $this->href;
}
public function getPath() {
return $this->path;
}
public function getLiveHref() {
return $this->liveHref;
}
}
?>