<?php
function Thumb($thumb_w,$thumb_h,$thumb_q) {
$update_thumbs = true;
$base_dir = 'uploaded';
$gal_name = 'fading dreams gallery';
if (isset($_GET["update"]) && $_GET["update"] == "true")
$update_thumbs = TRUE;
if (isset($_GET["base"]))
$base_dir = htmlentities(stripslashes($_GET["base"]));
if (isset($_GET["name"]))
$gal_name = htmlentities(stripslashes($_GET["name"]));
$images = array();
$xm_str = '<gal name="'.$gal_name.'" base="'.$base_dir.'">';
if (is_dir($base_dir) && $dhd = opendir($base_dir)) {
while ($file = readdir($dhd)) {
$link = $base_dir.'/'.$file;
if (is_file($link)) {
$size = filesize($link);
$measures = @getimagesize($link);
array_push($images, $file);
if ($update_thumbs) {
$thumb = createThumb($base_dir, $file, $measures[0], $measures[1]);
}
else {
$thumb = (is_file($base_dir.'/thumbs/'.$file))? 'thumbs/'.$file : $file;
}
$xm_str.= "\r\n".'<img src="'.$file.'" thumb="'.$thumb.'" width="'.$measures[0].'" height="'.$measures[1].'" size="'.$size.'" />';
}
}
closedir($dhd);
}
$xm_str.= "\r\n\r\n</gal>";
echo $xm_str; // testweise: '<pre>'.htmlentities($xm_str).'</pre>';
if ($update_thumbs) {
clearThumbs($base_dir, $images);
}
}
function clearThumbs($base_dir, $images) {
if (is_dir($base_dir.'/'.'thumbs') && $dhd = opendir($base_dir.'/'.'thumbs')) {
while ($file = readdir($dhd)) {
if (is_file($base_dir.'/'.'thumbs'.'/'.$file)) {
$found = false;
foreach($images as $val) {
if ($file == $val) {
$found = true;
break;
}
}
if (!$found) {
$u = unlink($base_dir.'/'.'thumbs'.'/'.$file);
}
}
}
closedir($dhd);
}
}
function createThumb($base_dir, $file, $w, $h) {
global $thumb_w, $thumb_h, $thumb_q;
$path = $base_dir.'/'.$file;
if (!is_dir($base_dir.'/'.'thumbs')) {
mkdir($base_dir.'/'.'thumbs', 0777);
}
$temp = imagecreatefromjpeg($path);
$r = $w / $h;
$thumb_h = intval($thumb_w / $r);
$small = imagecreatetruecolor($thumb_w, $thumb_h);
if ($suc = imagecopyresampled($small, $temp, 0, 0, 0, 0, $thumb_w, $thumb_h, $w, $h)) {
$result = imagejpeg($small, $base_dir.'/thumbs/'.$file, $thumb_q);
}
else {
$result = false;
}
imagedestroy($temp);
imagedestroy($small);
if ($result) {
return 'thumbs/'.$file;
}
else {
return $file;
}
}
Thumb($thumb_w = 76,$thumb_h = 90,$thumb_q = 95);
Thumb($thumb_w = 60,$thumb_h = 40,$thumb_q = 95);
Thumb($thumb_w = 750,$thumb_h = 600,$thumb_q = 95);
?>