Programmierecke

PHP:
Einfache Galerie

Beschreibung ansehen

<?php

/* ---------------------------------------------------------

LX' Easy Gallery

(c) 2007 Alexander Schulze

---------------------------------------------------------
This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301, USA.
---------------------------------------------------------

Authors: Alexander Schulze
Contact: http://www.lxhome.de/

---------------------------------------------------------

Changelog:

v1.0 2007-10-29: first version publically available

--------------------------------------------------------- */

// directory for cached thumbnails
$cachedir = '.thumbs/';

// edge length for the thumbnails
$length = 100;

// character encoding used in this document
$encoding = 'iso-8859-1';

/**
* creates or returns an existing thumbnail of an image
*
* @param String $filename
* @param Integer $length
*/
function thumbnail ( $filename )
{
// only images of the current directory
// security relevant
$filename = basename ( $filename );

if ( !
is_readable ( $filename ) ) die ( 'Image could not be read.' );

// maybe the client has the thumbnail already in his cache?
checkModified ( $filename );

global
$cachedir;

// create directory for cached thumbnails
if ( !is_dir ( $cachedir ) )
@
mkdir ( $cachedir );

$hashpath = $cachedir . md5 ( $filename );

header ( 'Content-Type: image/png' );

// if no cache entry exists or
// if the cache entry is older than the original file
if ( !is_readable ( $hashpath ) ||
filemtime ( $hashpath ) < filemtime ( $filename ) )
{
// get image data
list ( $orig_width, $orig_height, $orig_filetype, ) = getimagesize ( $filename );

global
$length;

$left = $top = 0;

if (
$orig_width > $orig_height )
{
// landscape image:
// choose a square crop in the center of the image
$resize_ratio = $orig_height / $length;

$left = round ( ( $orig_width - $length * $resize_ratio ) / 2 );
$orig_width = $orig_height;
}
elseif (
$orig_width < $orig_height )
{
// portrait image:
// choose a square crop a bit above the center of the image
$resize_ratio = $orig_width / $length;

$top = round ( ( $orig_height - $length * $resize_ratio ) / 4 );
$orig_height = $orig_width;
}

// create temp images based on image type
switch ( $orig_filetype )
{
case
3: $source = imagecreatefrompng ( $filename ); break;
case
2: $source = imagecreatefromjpeg ( $filename ); break;
case
1: $source = imagecreatefromgif ( $filename ); break;
}

$destination = imagecreatetruecolor ( $length, $length );

imagecopyresampled ( $destination, $source,
0, 0,
$left, $top,
$length, $length,
$orig_width, $orig_height );

// try to save first, then output
@imagepng ( $destination, $hashpath );
imagepng ( $destination );
}
else
{
// output cache entry

$file = fopen ( $hashpath, 'rb' );
fpassthru ( $file );
fclose ( $file );
}
}

/**
* tests if the client browser has a file already in its cache
*
* @param String $file
* @param String $etag
*/
function checkModified ( $file, $etag = null )
{
// get modification time of the file
$mod_date = filemtime ( $file );

// no date could be determined
if ( !$mod_date )
return;

// browser cache entry should expire in 24 hours
$expires = time() + 86400;

// format dates
$mod_date = substr ( date ( 'r', $mod_date ), 0, -5 ) . 'GMT';
$expires = substr ( date ( 'r', $expires ), 0, -5 ) . 'GMT';

if (
$etag === null )
$etag = md5 ( $mod_date );
else
$etag = md5 ( $mod_date . $etag );

// send headers
header ( 'Last-Modified: ' . $mod_date );
header ( 'Expires: ' . $expires );
header ( 'ETag: ' . $etag );

header ( 'Cache-Control: max-age=3600, must-revalidate' );

// check request headers
$if_modified_since = isset ( $_SERVER [ 'HTTP_IF_MODIFIED_SINCE' ] )
?
stripslashes ( $_SERVER [ 'HTTP_IF_MODIFIED_SINCE' ] )
:
false;

$if_none_match = isset ( $_SERVER [ 'HTTP_IF_NONE_MATCH' ] )
?
stripslashes ( $_SERVER [ 'HTTP_IF_NONE_MATCH' ] )
:
false;

// no headers… nothing to do here
if ( !$if_modified_since && !$if_none_match )
return;

// at least one header found
// let's see what we can do

// ETag doesn't match with the calculated one from above
if ( $if_none_match && $if_none_match != $etag )
return;

// date doesn't match with the calculated one from above
if ( $if_modified_since && $if_modified_since != $mod_date )
return;

// if we got until here everything seems to fit
// content probably hasn't changed
header ( 'HTTP/1.0 304 Not Modified' );
exit;
}

// thumbnail or overview requested
if ( isset ( $_GET [ 'image' ] ) )
thumbnail ( $_GET [ 'image' ] );
else
{
// comment the following line if you don't want to use XHTML
// or modify the encoding appropriately
echo '<?xml version="1.0" encoding="' . $encoding . '"?>';

$dateien = array();

$handle = opendir ( '.' );

// collect files
// file type check follows later
while ( $file = readdir ( $handle ) )
if ( !
is_link ( $file ) && !is_dir ( $file ) )
$files[] = $file;

// sort files alphabetically
@natcasesort ( $files );

#####################################
#####################################
## ##
## insert custom HTML header below ##
## ##
#####################################
#####################################
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>LX' Easy Gallery v1.0</title>
<style type="text/css">
div.image { padding: 10px; float: left; }
div.image img { border: 1px solid #000; padding: 1px; }
</style>
</head>
<body>

<?php

foreach ( $files as $file )
{
$pathinfo = pathinfo ( $file );

if (
$pathinfo [ 'extension' ] != 'jpg' &&
$pathinfo [ 'extension' ] != 'jpeg' &&
$pathinfo [ 'extension' ] != 'gif' &&
$pathinfo [ 'extension' ] != 'png' ) continue;

echo
'<div class="image"><a href="' . $file
. '" title="' . $file
. '"><img src="?image=' . $file
. '" alt="' . $file
. '"/></a></div>';
}
closedir ( $handle );

#####################################
#####################################
## ##
## insert custom HTML footer below ##
## ##
#####################################
#####################################
?>

</body>
</html>
<?
}
?>