Handle absolute paths for images and data dirs

Bizou now handles correctly data and images directories. One can now
configure

* `MEDIA_BASE_DIR`: base directory for `images` and `data` and
* `{IMAGES,DATA}_URL`: the base url that should be used to point to a
  file in `IMAGES_DIR` (resp. `DATA_DIR`).

Assuming the web server itself can correctly serve those files at the
given URL and Bizou has the correct permissions for those locations,
Bizou should be able to work as well as using relative paths.
This commit is contained in:
Théophile Bastian 2017-11-28 18:59:23 +01:00
parent 61e81e087e
commit 02479196c6
4 changed files with 49 additions and 26 deletions

View File

@ -1,7 +1,18 @@
<?php
## WARNING ##
# ALWAYS make sure your `DATA_DIR` and `DATA_URL` (resp. IMAGES_) are
# consistent. If you are using relative paths, it will be enforced by default.
# If you are using absolute paths (ie. `MEDIA_BASE_DIR` is absolute), you will
# have to make sure your web server serves `DATA_DIR` at `DATA_URL` (resp.
# IMAGES_).
#############
define('THUMB_SIZE', 100);
define('JPEG_QUALITY', '80');
define('DATA_DIR', 'data');
define('IMAGES_DIR', 'images');
define('MEDIA_BASE_DIR', ''); # Useful whenever those should be absolute locations
define('DATA_DIR', MEDIA_BASE_DIR . 'data');
define('DATA_URL', DATA_DIR); # Used in urls, but not in filesystem paths
define('IMAGES_DIR', MEDIA_BASE_DIR . 'images');
define('IMAGES_URL', IMAGES_DIR); # Used in urls, but not in filesystem paths
define('DATA_UMASK', 0002); # 0002: allow group to write for files created in data dir. change to 0022 to be more strict.
?>

View File

@ -28,7 +28,7 @@ function getPathInfo()
if (! function_exists('getImageLink')) {
function getImageLink($imageSimplePath)
{
return $GLOBALS['rootUrl'].IMAGES_DIR.$imageSimplePath;
return $GLOBALS['rootUrl'].IMAGES_URL.$imageSimplePath;
}
}
@ -36,11 +36,13 @@ if (! function_exists('getPreview')) {
function getPreview($imgFile, $maxSize = THUMB_SIZE)
{
# example: data/myalbum/100.mypic.jpg
$newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
$origImgFile = IMAGES_DIR . $imgFile;
$basePath = dirname($imgFile)."/".$maxSize.".".basename($imgFile);
$newImgFile = DATA_DIR . "/" . $basePath;
# if the preview is a symlink, image is already good sized
if (is_link($newImgFile)) return $imgFile;
if (! is_file($newImgFile))
{
# this tels the template to flush output after displaying previews
@ -51,10 +53,10 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE)
$ext = strtolower(substr($imgFile, -4));
if ($ext == ".jpg") {
$img = imagecreatefromjpeg($imgFile);
$exif = exif_read_data($imgFile);
$img = imagecreatefromjpeg($origImgFile);
$exif = exif_read_data($origImgFile);
} else {
$img = imagecreatefrompng($imgFile);
$img = imagecreatefrompng($origImgFile);
}
if ($img === false) return ""; #read error (wrong permission...)
@ -96,25 +98,27 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE)
} else {
imagepng($newImg, $newImgFile);
}
imagedestroy($img);
imagedestroy($newImg);
}
return $newImgFile;
return $basePath;
}
}
function getAlbumPreview($dir)
{
$previewFile = DATA_DIR."/$dir/albumpreview";
$previewBase = "/$dir/albumpreview";
$previewFile = DATA_DIR . $previewBase;
$dirPath = IMAGES_DIR . "/$dir/";
if (is_file("$previewFile.jpg")) {
return "$previewFile.jpg";
return "$previewBase.jpg";
} else if (is_file("$previewFile.empty")) {
return "";
} else if (is_file("$previewFile.png")) {
return "$previewFile.png";
return "$previewBase.png";
} else {
# config to allow group writable files
umask(DATA_UMASK);
@ -123,17 +127,18 @@ function getAlbumPreview($dir)
or die("Could not write in data dir. Please fix permissions.");
// no preview: look for a preview in current dir, write it, return it
foreach (scandir($dir) as $file) if ($file[0] != '.') {
foreach (scandir($dirPath) as $file) if ($file[0] != '.') {
$ext = strtolower(substr($file, -4));
if ($ext == ".jpg" or $ext == ".png") {
$thumb = getPreview("$dir/$file");
$thumbUrl = getPreview("$dir/$file");
$thumb = DATA_DIR . $thumbUrl;
copy($thumb, $previewFile.$ext);
return $previewFile.$ext;
} else if (is_dir("$dir/$file")) {
return $previewBase.$ext;
} else if (is_dir("$dirPath/$file")) {
$subPreview = getAlbumPreview("$dir/$file");
if ($subPreview) {
$myPreview = dirname($previewFile)."/".basename($subPreview);
copy($subPreview, $myPreview);
$myPreview = dirname($previewBase)."/".basename($subPreview);
copy(DATA_DIR . $subPreview, DATA_DIR . $myPreview);
return $myPreview;
}
}

View File

@ -28,7 +28,6 @@ if (substr($rootUrl, -1) !== '/') $rootUrl.='/'; // add a trailing / to rootUrl
require 'functions.php';
// if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi
// if url == http://localhost/photos/index.php, path_info is not set
// if url == http://localhost/photos/, path_info is not set
@ -59,15 +58,23 @@ foreach (scandir($realDir) as $file) if ($file[0] != '.')
{
if (is_dir("$realDir/$file"))
{
$folders[] = array( "name" => $file, "file" => "$realDir/$file", "link" => "$scriptUrl$simplePath/$file" );
$folders[] = array(
"name" => $file,
"file" => "$simplePath/$file",
"link" => "$scriptUrl$simplePath/$file");
}
else
{
$ext = strtolower(substr($file, -4));
if ($ext == ".jpg" or $ext == ".png") {
$imageFiles[] = array( "name" => $file, "file" => "$realDir/$file", "link" => getImageLink("$simplePath/$file") );
$imageFiles[] = array(
"name" => $file,
"file" => "$simplePath/$file",
"link" => getImageLink("$simplePath/$file"));
} else {
$otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" );
$otherFiles[] = array(
"name" => $file,
"link" => "$rootUrl$realDir/$file");
}
}
}

View File

@ -64,7 +64,7 @@ a {
<div class="square"><div class="image_nopreview"><a href="<?= $folder["link"] ?>"></a></div></div>
<div class="square"><div class="foldername_nopreview"> <a href="<?= $folder["link"] ?>"><?= $folder["name"] ?></a> </div></div>
<?php } else { ?>
<div class="square"><div class="image"> <a href="<?= $folder["link"] ?>"><img src="<?= $rootUrl.$preview ?>" /></a> </div></div>
<div class="square"><div class="image"> <a href="<?= $folder["link"] ?>"><img src="<?= $rootUrl.DATA_URL.$preview ?>" /></a> </div></div>
<div class="square"><div class="foldername"> <a href="<?= $folder["link"] ?>"><?= $folder["name"] ?></a> </div></div>
<?php if (isset($generating)) { ob_flush(); flush(); } ?>
<?php } ?>
@ -74,7 +74,7 @@ a {
<div id="images">
<?php foreach ($imageFiles as $file) { ?>
<?php $previewfile = $rootUrl.getPreview($file["file"]) ?>
<?php $previewfile = $rootUrl . DATA_URL . getPreview($file["file"]) ?>
<div class="square"><div class="image imagepreview"><a href="<?= $file["link"] ?>"><img src="<?= $previewfile ?>" alt="<?= $file["name"] ?>" /></a></div><?php plugins_include("after_thumb.php") ?></div>
<?php if (isset($generating)) { ob_flush(); flush(); } ?>
<?php } ?>