| px | top | add code | search | signup | login | help |
<?php
ini_set('arg_separator.output','&');
/**
* Title: imageViwewer
* Author: Michael Leaney
* License: GPL (Freeware)
* Email: leahcim@pythontech.net.au
*
* (C) Copyright 2006 Michael Leaney
* http://www.pythontech.net.au/?pg=200
*
* Please download latest code from http://www.pythontech.net.au/?pg=200
*
* Also please view the license at http://www.pythontech.net.au/thumbs/LICENSE
* or refer to the LICENSE file found inside the .zip
*
* imageViewer is a single PHP script that allows users to browse a directory and view images. It is capable of caching thumbnails, caching output.
*
* Although this program is freeware and you are allowed to modify it in anyway, I please ask you to leave the link to Python Technologies.
* It takes a long time to write and debug software and if you find this useful I just ask for the link.
* Also if you modify this please specify my name as original author. Also drop me a line if you would like.
*
*/
/* -------- SETTINGS */
$base_dir = dirname(__FILE__); // Base Directory we recommend dirname(__FILE__),
$thumb_dir = '__thumbs__'; // Sets the Cache directory for thumbs
$big_dir = '__big__'; // Sets the Cache directory for bigs
$qaulity = 100; // Sets the JPEG Compression Qaulity
$thumb_width = 120; // Specifies Thumb Width
$thumb_height = 90; // Specifies Thumb Hieght (Value of 'auto' will automatically select the height based on the images width while preserving i'ts aspect ratio)
$big_width = 800; // Specifies Big Width (Size of image's width when clicked on)
$big_height = 'auto'; // Specifies Big Height (Size of image's height when clicked one) 'auto' will do the same as before
$cache = true;
$e_error = true; // Would you like error reporting ?
$filestyle = "padding:15px;float:left; background-color: #d6e0ec; border: 1px solid #0072bc; margin:5px;";
$folderstyle = "padding:15px;float:left; background-color: #fce6d4; border: 1px solid #ffc20e; margin:5px; ";
/* END SETTINGS */
/* resolve ../ and ./ and extra /'s */
$base_dir = realpath($base_dir);
/* PHP 4 does not have stripos, let's create it */
if (!function_exists('stripos')) {
function stripos($haystack, $needle) {
return strpos (strtolower($haystack), strtolower($needle));
}
}
if (isset($_GET['folder'])) {
$folder = realpath($base_dir . '/' . urldecode($_GET['folder']));
}
if (!@$folder or @$folder=='') {
$folder = $base_dir;
}
if (isset($_GET['file'])) {
$file = realpath($folder . '/' . urldecode($_GET['file']));
/* Set folder to folder of file */
$folder = dirname($file);
/* set file to file name of the file */
$file = basename($file);
}
/* Something went wrong */
if (!$folder) {
if ($e_error){die( '<hr /><h2 style="color:red;">Error in '.basename($_SERVER['SCRIPT_FILENAME']).'</h2><p>Could not find folder, or no access.</p><hr />');}
}
/* Check if base_dir is set correctly */
if ($base_dir == false) {
if ($e_error){die( '<hr /><h2 style="color:red;">Error in '.basename($_SERVER['SCRIPT_FILENAME']).'</h2><p>Cannot locate $basedir, please check your configuration settings.</p><hr />');}
}
/* Check if $folder string is smaller then $base_dir */
if (strlen($base_dir) > strlen($folder)) {
if ($e_error){die( '<hr /><h2 style="color:red;">Error in '.basename($_SERVER['SCRIPT_FILENAME']).'</h2><p>Could not find folder, or no access.</p><hr />');}
}
/* check if outside $base_ dir */
if (substr($folder,0,strlen($base_dir)) != $base_dir) {
if ($e_error){die( '<hr /><h2 style="color:red;">Error in '.basename($_SERVER['SCRIPT_FILENAME']).'</h2><p>Folder outside base dir</p><hr />');}
}
function resize_image($source, $width, $height='auto', $string =''){
/* set old width and height */
@$oldwidth = imagesx($source);
@$oldheight = imagesy($source);
/* If the source image is invalid create a ? mark image */
if ($oldwidth == 0 or $oldheight == 0) {
$height = $width/1.33;
$dest = imagecreatetruecolor(5,8);
imagefill($dest,1,1,imagecolorallocate($dest,255,255,255));
imagestring($dest,1,0,0,"?", imagecolorallocate($dest, 0,0,0));
return resize_image ($dest,$width,$height);
}
/* Find aspect ratio */
$ar = $oldheight/$oldwidth;
/* if height is auto set it to fit the aspect ratio of width */
if ($height == 'auto') {
$height = $width*$ar;
}
/* Create new image using the new size */
$dest = imagecreatetruecolor($width, $height);
/* copy old image to the new image and resize it */
imagecopyresized($dest, $source, 0,0,0,0,$width,$height,$oldwidth,$oldheight);
/* if a string exists then brand the picture with it */
if ($string != '') {
imagestring($dest, 5, 0, 0, $string, imagecolorallocate($dest,255,255,255));
}
/* Return resized image */
return $dest;
}
/* ptlib's scandir */
function ptlib_scandir($directory){
$folderContents = array();
$directory = realpath($directory).DIRECTORY_SEPARATOR;
foreach (ptlib_scandirphp4($directory) as $folderItem) {
if ($folderItem != "." AND $folderItem != "..") {
if (is_dir($directory.$folderItem.DIRECTORY_SEPARATOR)) {
$folderContents['folders'][] = $folderItem;
}
else {
if ($folderItem != "Thumbs.db") {
$folderContents['files'][] = $folderItem;
}
}
}
}
return $folderContents;
}
/* PHP 4 Does not have support for scandir() */
function ptlib_scandirphp4($directory) {
$dh = opendir($directory);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
return $files;
}
/* Images only... This was a quick hack for the folder thumbnail preview */
function ptlib_scanimages($directory){
$contents = ptlib_scandir($directory);
$scan = array();
if (count($contents['files']) == 0) { return array(); }
foreach ($contents['files'] as $item) {
if (stripos($item,'.jpg') or stripos($item,'.jpeg') or stripos($item,'.gif') or stripos($item,'.png') ) {
$scan[] = $item;
}
}
return $scan;
}
function thumb_index() {
global $thumb_dir, $big_dir;
/* Define the styles */
echo "<style><!-- div.file { {$GLOBALS['filestyle']} } --></style>";
echo "<style><!-- div.folder { {$GLOBALS['folderstyle']} } --></style>";
$contents = ptlib_scandir($GLOBALS['folder']);
$GLOBALS['folders'] = substr($GLOBALS['folder'],strlen($GLOBALS['base_dir']));
if ($GLOBALS['folder'] != $GLOBALS['base_dir']) {
echo "<div class=\"folder\"><a href=\"?folder=" . urlencode($GLOBALS['folders'] . "/../") ."\"><img style=\"border:0px\" src=\"?folder=" . urlencode($GLOBALS['folders'] . "/../") . "&view=folder\" /></a><pre style=\"text-align: center; margin:0px;\">../</pre></div>";
}
if (count(@$contents['folders']) > 0) {
foreach ($contents['folders'] as $item) {
if ($item != $thumb_dir && $item != $big_dir){
if (strlen($item) > 13) {
$items = substr($item,0,10).'...';
} else {
$items = $item;
}
echo "<div class=\"folder\"><a href=\"?folder=".urlencode($GLOBALS['folders'] ."/". $item)."\"><img style=\"border:0px\" src=\"?folder=" .urlencode($GLOBALS['folders'].'/'.$item) . "&view=folder\" /></a><pre style=\"text-align: center; margin:0px;\">$items</pre></div>";
}
}
}
echo "<br style=\"clear:both;\">";
if (count($contents['files']) > 0) {
foreach ($contents['files'] as $item) {
if (stripos($item,'.jpg') or stripos($item,'.jpeg') or stripos($item,'.gif') or stripos($item,'.png') ) {
if (strlen($item) > 13) {
$items = substr($item,0,10).'...';
} else {
$items = $item;
}
echo "<div class=\"file\"><center><a href=\"?folder=" . urlencode($GLOBALS['folders']) . "&view=bigpage&file=" . urlencode($item) . "\"><img style=\"border:0px;\" src=\"?view=thumb&file=" . urlencode($item) . "&folder=" . urlencode($GLOBALS['folders']) . "\" /></a><pre style=\"text-align: center; margin:0px;\">$items</pre><center></div>";
}
}
}
echo "<br style=\"clear:both;\"><center style=\"font-family:arial;\">Get this script and more at <a href=\"http://www.pythontech.net.au\">Python Technologies</a></center>";
}
function thumb_pic() {
global $file, $folder, $big_dir, $thumb_dir;
$myfile=$folder .'/'.$file;
function getimage($file) {
$ext = getimagesize($file);
switch ($ext[2]) {
case 2:
return @imagecreatefromjpeg($file);
break;
case 1:
return @imagecreatefromgif($file);
break;
case 3:
return @imagecreatefrompng($file);
break;
default:
return false;
}
}
switch ($_GET['view']) {
case 'bigpage':
echo "<style><!-- div.file { {$GLOBALS['filestyle']} } --></style>";
echo "<div class=\"file\" style=\"padding-top:0px;margin:0px;\"><a href=\"?folder={$_GET['folder']}\">back</a><br /><img src=\"?folder={$_GET['folder']}&file={$_GET['file']}&view=big\" /></div>";
break;
case 'big':
/* Check for cached */
header('content-type: image/jpeg');
if (file_exists($folder.'/'.$big_dir.'/'.$file)) {
echo readfile($folder.'/'.$big_dir.'/'.$file);
exit;
}
$source = getimage($myfile);
$source = resize_image($source, $GLOBALS['big_width'], $GLOBALS['big_height']);
if ($GLOBALS['cache'] == true) {
if (!file_exists($folder.'/'.$big_dir)) {
@mkdir ($folder.'/'.$big_dir);
}
if (file_exists($folder.'/'.$big_dir)) {
imagejpeg($source, $folder.'/'.$big_dir.'/'.$file, $GLOBALS['qaulity']);
}
}
imagejpeg($source, '', $GLOBALS['qaulity']);
break;
case 'full':
header('content-type: image/jpeg');
if (file_exists($folder.'/'.$file)) {
echo file_get_contents( $folder.'/'.$file );
exit;
}
break;
case 'thumb':
header('content-type: image/jpeg');
if (file_exists($folder.'/'.$thumb_dir.'/'.$file)) {
echo file_get_contents($folder.'/'.$thumb_dir.'/'.$file);
exit;
}
$source = getimage($myfile);
$source = resize_image($source, $GLOBALS['thumb_width'], $GLOBALS['thumb_height']);
if ($GLOBALS['cache'] == true) {
if (!file_exists($folder.'/'.$thumb_dir)) {
@mkdir ($folder.'/'.$thumb_dir);
}
if (file_exists($folder.'/'.$thumb_dir)) {
imagejpeg($source, $folder.'/'.$thumb_dir.'/'.$file, $GLOBALS['qaulity']);
}
}
imagejpeg($source, '', $GLOBALS['qaulity']);
break;
case 'folder':
header('content-type: image/jpeg');
if (file_exists($folder.'/'.$thumb_dir.'/__FOLDER.JPG')) {
echo readfile($folder.'/'.$thumb_dir.'/__FOLDER.JPG');
exit();
}
$contents=ptlib_scanimages($folder);
switch (count($contents)) {
case 0:
$height = ($GLOBALS['thumb_width'])/1.33; /* make sure all folders look the same */
$dest = imagecreatetruecolor(5,8); //$GLOBALS['thumb_width'], $height);
imagefill($dest,1,1,imagecolorallocate($dest,255,255,255));
imagestring($dest,1,0,0,"?", imagecolorallocate($dest, 0,0,0));
imagejpeg( resize_image ($dest,120,90));
exit();
case 1:
$source = getimage($folder.'/'.$contents[0]);
$source = resize_image($source, $GLOBALS['thumb_width'], $GLOBALS['thumb_height']);
imagejpeg($source,'',$GLOBALS['qaulity']);
exit();
case 2:
case 3:
$source1 = getimage($folder.'/'.$contents[0]);
$source2 = getimage($folder.'/'.$contents[1]);
$source4 = getimage($folder.'/'.$contents[0]);
$source3 = getimage($folder.'/'.$contents[1]);
break;
default:
$source1 = getimage($folder.'/'.$contents[0]);
$source2 = getimage($folder.'/'.$contents[1]);
$source3 = getimage($folder.'/'.$contents[2]);
$source4 = getimage($folder.'/'.$contents[3]);
break;
}
$height = ($GLOBALS['thumb_width'])/1.33; /* make sure all folders look the same */
$source1 = resize_image($source1, ($GLOBALS['thumb_width'])/2,$height/2);
$source2 = resize_image($source2, ($GLOBALS['thumb_width'])/2,$height/2);
$source3 = resize_image($source3, ($GLOBALS['thumb_width'])/2,$height/2);
$source4 = resize_image($source4, ($GLOBALS['thumb_width'])/2,$height/2);
$dest = imagecreatetruecolor($GLOBALS['thumb_width'], $height);
/* DEST SOURCE dest X y x y w h */
imagecopyresized($dest, $source1, 0,0,0,0,@imagesx($source1),@imagesy($source1),@imagesx($source1),@imagesy($source1));
imagecopyresized($dest, $source2,$GLOBALS['thumb_width']/2,0,0,0,@imagesx($source2),@imagesy($source2),@imagesx($source2),@imagesy($source2));
imagecopyresized($dest, $source3, 0,$height/2,0,0,@imagesx($source3),@imagesy($source3),@imagesx($source3),@imagesy($source3));
imagecopyresized($dest, $source4,$GLOBALS['thumb_width']/2,$height/2,0,0,@imagesx($source4),@imagesy($source4),@imagesx($source4),@imagesy($source4));
if ($GLOBALS['cache'] == true) {
if (!file_exists($folder .'/'.$thumb_dir."/")) {
@mkdir ($folder .'/'.$thumb_dir."/");
}
if (file_exists($folder .'/'.$thumb_dir."/")) {
imagejpeg($dest,$folder .'/'.$thumb_dir.'/__FOLDER.JPG', $GLOBALS['qaulity'] );
}
}
imagejpeg($dest,'',$GLOBALS['qaulity']);
break;
}
}
if (@$_GET['view'] == 'folder') {
thumb_pic();
exit;
}
switch (isset($_GET['file'])) {
case false:
echo "<!-- This site contains code created by Michael Leaney for Python Technologies. -->";
thumb_index();
break;
case true:
thumb_pic();
break;
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.