Picture caching. This can take some time to complete...
';
//Load the pictures from the album
for ($i = 0; $i < $countAlbums; $i++){
//Check the cache for the albums, and cache the pictures in the folder.
if (!file_exists($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i]) || !is_dir($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i])){
if (mkdir($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i])) echo '
Creating cache folder for the album: '.$CONFIG['albums'][$i].'
';
if(!chmod($CONFIG['cacheFolderName'], $CONFIG['chmod'])) echo 'Could not chmod the cache folder. No access';
else echo 'Cant write to disc.';
}
echo '
Caching album: '.$CONFIG['albums'][$i].'';
//Fetching the files in the folder
$files = array();
if (is_dir($CONFIG['albums'][$i])) {
if ($dh = opendir($CONFIG['albums'][$i])) {
while (($file = readdir($dh)) !== false) {
if (validExt($CONFIG['albums'][$i].'/'.$file)) $files[] = $file;
}
closedir($dh);
}
}
$countFiles = count($files);
ob_flush();
for ($j = 0; $j < $countFiles; $j++){
if ($countFiles >= 0){
echo 'Caching the picture: '.$files[$j].': ';
echo 'Creating thumbnail: ... ';
createThumb($files[$j], $files[$j], $CONFIG['albums'][$i]);
echo 'Creating image: ...
';
createImg($files[$j], $files[$j], $CONFIG['albums'][$i]);
ob_flush();
}
}
}
echo '
Caching complete';
//END MANUAL CACHING
}else{
//Cache one picture on each album to create the thumbnails.
if (!isset($_GET['album'])){
for ($i = 0; $i < $countAlbums; $i++){
//Check the cache for the albums, and cache the pictures in the folder.
if (!file_exists($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i]) || !is_dir($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i])){
if (!mkdir($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i])) echo 'Cant create the cache directory. Cant write to disc.';
if (!chmod($CONFIG['cacheFolderName'].'/'.$CONFIG['albums'][$i], $CONFIG['chmod'])) echo 'Cant chmod the cache directory. Cant write to disc.';
}
//Load the pictures from the album
$files = array();
if (is_dir($CONFIG['albums'][$i])) {
if ($dh = opendir($CONFIG['albums'][$i])) {
while (($file = readdir($dh)) !== false) {
if (validExt($CONFIG['albums'][$i].'/'.$file)) $files[] = $file;
}
closedir($dh);
}
$countFiles = count($files);
//Caching one picture
if (!$countFiles <= 0){
if($countFiles > 0 && folderContent($CONFIG['albums'][$i]) == 0){
if ($countFiles >= 0) createThumb($files[0], $files[0], $CONFIG['albums'][$i]);
}
//Show the albums
echo '
';
}
}else echo 'Cant read from the album. Might be wrong access rights';
}
}else if (isset($_GET['album'])){
echo '
';
//Cache all picture in this album.
//Checking if the album is in the validated array
if (in_array($_GET['album'],$CONFIG['albums'])){
$album = $_GET['album'];
$files = array();
if (is_dir($album)) {
if ($dh = opendir($album)) {
while (($file = readdir($dh)) !== false) {
if (validExt($album.'/'.$file)) $files[] = $file;
}
closedir($dh);
}
}
$countFiles = count($files);
$num = 0;
for ($i = 0; $i < $countFiles; $i++){
if ($countFiles >= 0){
createThumb($files[$i], $files[$i], $album);
createImg($files[$i], $files[$i], $album);
}
if (@$_GET['do'] == 'browse' || !isset($_GET['do'])){
if ($num == $CONFIG['thumbsInwidth']) {
$num = 0;
echo '
';
}
echo '
';
$num++;
}
}
echo '
';
if(@$_GET['do'] == 'view'){
echo '
';
if (in_array($_GET['img'], $files)){
echo '
';
echo browserPic($_GET['img'], $_GET['album']);
echo '
';
echo '';
}else echo 'Not a valid image name';
}
} //END CACHE
}
}
function browserPic($pos, $album){
$files = array();
if (is_dir($album)) {
if ($dh = opendir($album)) {
while (($file = readdir($dh)) !== false) {
if (validExt($album.'/'.$file)) $files[] = $file;
}
closedir($dh);
}
}
$countFiles = count($files);
$key = array_search($pos, $files);
if ($key < $countFiles && $countFiles != 1){ //if not its just one pic in the folder
$nav = '';
if ($key == 0) //First picture in the folder
$nav .= '
Next';
elseif ($key >= 1 && $key < $countFiles-1)
$nav .= '
Previous |
Next';
else if ($key == $countFiles-1)
$nav .= '
Previous';
}
return $nav;
}
function folderContent($in){
global $CONFIG;
$in = $CONFIG['cacheFolderName'].'/'.$in;
$files = 0;
if (is_dir($in)) {
if ($dh = opendir($in)) {
while (($file = readdir($dh)) !== false) {
if (detectCachePicture($file, $in)) $files++;
}
closedir($dh);
}
}
return $files;
}
function validExt($in){
global $CONFIG;
if (is_file($in)){
$new = explode('.', $in);
$count = count($new);
//If the picture has an allowed extension
if (in_array(strtolower($new[(int)$count-1]),$CONFIG['allowedExtensions'] ))return true;
else return false;
}else return false;
}
function detectCachePicture($in, $album){
global $CONFIG;
$in = $album.'/'.$in;
if (is_file($in)){
$new = explode('.', $in);
$count = count($new);
//If the picture has an allowed extension
if (in_array($new[(int)$count-1],$CONFIG['allowedExtensions'] ))return true;
else return false;
}else return false;
}
function readPicture($in){
$fh = fopen($in, 'rb');
$content = fread($fh, filesize($in));
return $content;
fclose($in);
}
function tempFile($in, $album){
global $CONFIG;
$foldername = $CONFIG['cacheFolderName'];
if (file_exists($foldername) && is_dir($foldername)){
if (file_exists($foldername.'/'.$album.'/thumb.'.$in)) return false;
else return true;
} else{
mkdir($foldername);
if (!chmod($foldername, $CONFIG['chmod'])) echo 'Cant chmod the directory. Cant write to disc';
return true;
}
}
function tempFileFull($in, $album){
global $CONFIG;
$foldername = $CONFIG['cacheFolderName'];
if (file_exists($foldername) && is_dir($foldername)){
if (file_exists($foldername.'/'.$album.'/full.'.$in)) return false;
else return true;
} else{
mkdir($foldername);
if (!chmod($foldername, $CONFIG['chmod'])) echo 'Cant chmod the directory. Cant write to disc';
return true;
}
}
function createThumb($in, $file, $album){
global $CONFIG;
if(tempFile($file, $album)){
$tempfolder = $CONFIG['cacheFolderName'].'/'.$album;
$in = readPicture($album.'/'.$in);
$dim = getimagesize($album.'/'.$file);
$image = imagecreatetruecolor($CONFIG['thumbX'], $CONFIG['thumbY']);
$bgwidth = $dim[0];
$bgheight = $dim[1];
$prop = $bgwidth / $bgheight;
$bhwidth = $CONFIG['thumbX'];
$bgheight = $CONFIG['thumbY'] / $prop;
if ($bgheight < $CONFIG['thumbY']) {
$bgheight = $CONFIG['thumbY'];
$bhwidth = $CONFIG['thumbX'] *$prop;
}
//Make a new image from the file above
$imagebg = imagecreatefromstring($in);
imagecopyresized($image, $imagebg, '0', '0', '0', '0', $bhwidth, $bgheight, $dim[0], $dim[1]);
//Outut to the correct file type.
$new = explode('.', $file);
$count = count($new);
$ext = strtolower($new[(int)$count-1]);
if ($ext == 'gif')
imagegif($image, $tempfolder.'/thumb.'.$file);
else if ($ext == 'jpg' || $ext == 'jpeg')
imagejpeg($image, $tempfolder.'/thumb.'.$file);
else if ($ext == 'png')
imagepng($image, $tempfolder.'/thumb.'.$file);
else
imagejpeg($image, $tempfolder.'/thumb.'.$file);
if (!chmod($tempfolder.'/thumb.'.$file, $CONFIG['chmod'])) echo 'Cant chmod the file. Cant write to disc';
}
}
function createImg($in, $file, $album){
global $CONFIG;
if(tempFileFull($file, $album)){
$tempfolder = $CONFIG['cacheFolderName'].'/'.$album;
$in = readPicture($album.'/'.$in);
$dim = getimagesize($album.'/'.$file);
$bgwidth = $dim[0];
$bgheight = $dim[1];
//Make a new image from the file above
$imagebg = imagecreatefromstring($in);
if ($CONFIG['resizeUp'] == FALSE && $bgwidth < $CONFIG['fullX']){ //Do not resize
$image = imagecreatetruecolor($bgwidth, $bgheight);
imagecopyresized($image, $imagebg, '0', '0', '0', '0', $bgwidth, $bgheight, $bgwidth, $bgheight);
} else {
$prop = $bgwidth / $bgheight;
$heigth = $CONFIG['fullX'] / $prop;
$image = imagecreatetruecolor($CONFIG['fullX'], $heigth);
imagecopyresized($image, $imagebg, '0', '0', '0', '0', $CONFIG['fullX'], $heigth, $bgwidth, $bgheight);
}
//Outut to the correct file type.
$new = explode('.', $file);
$count = count($new);
$ext = strtolower($new[(int)$count-1]);
if ($ext == 'gif')
imagegif($image, $tempfolder.'/full.'.$file);
else if ($ext == 'jpg' || $ext == 'jpeg')
imagejpeg($image, $tempfolder.'/full.'.$file, $CONFIG['imageQuality']);
else if ($ext == 'png')
imagepng($image, $tempfolder.'/full.'.$file);
else
imagejpeg($image, $tempfolder.'/full.'.$filem, $CONFIG['imageQuality']);
if (!chmod($tempfolder.'/full.'.$file, $CONFIG['chmod'])) echo 'Cant chmod the file. Cant write to disc';
}
}
echo '