PHP Gallery

Associate
Joined
5 Feb 2006
Posts
129
Location
Birmingham
<?php
// TStarGallery 1.0 engine
// With basic PHP knowledge you can understand what's happening here.
// Don't blame me for writing non-perfect code for I do not care.
// If there is an error or something, mail me at [email protected]


// Read the current directory, throw out non-jpg/gif/png + thumbfiles
// --------------------------------------------------------------------
$dirfiles = array();

$handle=opendir('.');
while ($file = readdir($handle)) {
if
((
strtolower(strrchr($file, '.')) == ".jpg" OR
strtolower(strrchr($file, '.')) == ".jpeg" OR
strtolower(strrchr($file, '.')) == ".png" OR
strtolower(strrchr($file, '.')) == ".gif"
)
&&
(
strstr($file, ".thumb.jpg") == ''
))
{
array_push($dirfiles, $file);
}
}
sort ($dirfiles);
closedir($handle);

// Write the beginning of the basic table for displaying the thumbs.
// Modify this section to make it fit your own website.
// -----------------------------------------------------------------
echo "<table width=\"505\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" id=\"structure\"><tr>";


// Read the valid filenames from the array, have your way with every single one of them
// ------------------------------------------------------------------------------------

foreach($dirfiles as $aktuellesfile)
{
// Elements of the filename are cut into pieces
$dateiendung = strrchr( $aktuellesfile, '.' );
$dateiname = substr_replace ($aktuellesfile, '', -strlen($dateiendung) );


// First a routine for creating a thumb
createthumb ($dateiname, $dateiendung);
// Now open up a table cell
echo "<td>";
// Second a routine for showing a thumb
showthumb ($dateiname, $dateiendung);
// Close the table cell
echo "</td>";
// And make a linebreak after every 5 thumbs
if(++$cnt % 5 == 0) echo "</tr>";
}

// Finished
exit;


// Function to create a thumbnail if it doesn't already exist
// -----------------------------------------------------------------
function createthumb ($thumbdateiname, $thumbdateiendung)
{
$fullname = $thumbdateiname.$thumbdateiendung;
$fullthumbname = $thumbdateiname.$thumbdateiendung.".thumb.jpg";

// If thumb exists,nothing will happen
if (file_exists($fullthumbname) OR strstr($fullname, ".thumb.jpg") != '')
{
}
// If thumb doesn't exist,it's created now
else
{
if ((strtolower($thumbdateiendung) == ".jpg") OR (strtolower($thumbdateiendung) == ".jpeg")){
$src_img = imagecreatefromjpeg($fullname);
}
if (strtolower($thumbdateiendung) == ".gif"){
$src_img = imagecreatefromgif($fullname);
}
if (strtolower($thumbdateiendung) == ".png"){
$src_img = imagecreatefrompng($fullname);
}

$origx=imagesx($src_img);
$origy=imagesy($src_img);

// Maximum width and height of the thumbnails
$max_x = 90;
$max_y = 90;

// Calc, if thumb has has to be squeezed from width or height
if($origx >= $origy AND $origx > $max_x)
{
$faktor = $origx / $max_x;
$new_x = $origx / $faktor;
$new_y = $origy / $faktor;
}

elseif($origy > $origx AND $origy > $max_y)
{
$faktor = $origy / $max_y;
$new_x = $origx / $faktor;
$new_y = $origy / $faktor;
}

else
{
$new_x = $origx;
$new_y = $origy;
}

// Squeeze and write it into a file
$dst_img = imagecreatetruecolor($new_x,$new_y);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_x,$new_y,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, $fullthumbname, 50);
}
}

// Function to show a thumbnail
// -----------------------------------------------------------------
function showthumb ($thumbdateiname, $thumbdateiendung)
{
$fullname = $thumbdateiname.$thumbdateiendung;
$fullthumbname = $thumbdateiname.$thumbdateiendung.".thumb.jpg";
if (file_exists($fullthumbname))
{
echo "<a href=\"$fullname\"><img src =\"$fullthumbname\" border=\"0\"></a>";
echo "<!-- =========================== -->";
echo "<!-- powered by TStarGallery 1.0 -->";
echo "<!-- =========================== -->";

}
else
{
}

}
?>

I have been using the above code to produce an image gallery. I drop the images into a folder alond with the script and bang an image gallery.

I would like to modify the code so if the amount of images are more than a given number (maybe 16) the script probuces a PAGE 2 link and the remaining pictures are displayed on there if it reatches 16 again PAGE 3 is made.

Is this possible?
 
Associate
OP
Joined
5 Feb 2006
Posts
129
Location
Birmingham
PHP:
<?php
// TStarGallery 1.0 engine
// With basic PHP knowledge you can understand what's happening here.
// Don't blame me for writing non-perfect code for I do not care.
// If there is an error or something, mail me at [email][email protected][/email]


// Read the current directory, throw out non-jpg/gif/png + thumbfiles
// --------------------------------------------------------------------
$dirfiles = array();

$handle=opendir('.');
while ($file = readdir($handle)) {
   if
   ((
   strtolower(strrchr($file, '.')) == ".jpg" OR
   strtolower(strrchr($file, '.')) == ".jpeg" OR
   strtolower(strrchr($file, '.')) == ".png" OR
   strtolower(strrchr($file, '.')) == ".gif"
   )
   &&
   (
   strstr($file, ".thumb.jpg") == ''
   ))
   {
       array_push($dirfiles, $file);
   }
}
sort ($dirfiles);
closedir($handle);

// Write the beginning of the basic table for displaying the thumbs.
// Modify this section to make it fit your own website.
// -----------------------------------------------------------------
echo "<table width=\"505\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" id=\"structure\"><tr>";


// Read the valid filenames from the array, have your way with every single one of them
// ------------------------------------------------------------------------------------

foreach($dirfiles as $aktuellesfile)
{
// Elements of the filename are cut into pieces
$dateiendung = strrchr( $aktuellesfile, '.' );
$dateiname = substr_replace ($aktuellesfile, '', -strlen($dateiendung) );


// First a routine for creating a thumb
createthumb ($dateiname, $dateiendung);
// Now open up a table cell
echo "<td>";
// Second a routine for showing a thumb
showthumb ($dateiname, $dateiendung);
// Close the table cell
echo "</td>";
// And make a linebreak after every 5 thumbs
if(++$cnt % 5 == 0) echo "</tr>";
}

// Finished
exit;

   
// Function to create a thumbnail if it doesn't already exist
// -----------------------------------------------------------------
function createthumb ($thumbdateiname, $thumbdateiendung)
{
    $fullname = $thumbdateiname.$thumbdateiendung;
    $fullthumbname = $thumbdateiname.$thumbdateiendung.".thumb.jpg";
    
    // If thumb exists,nothing will happen
    if (file_exists($fullthumbname) OR strstr($fullname, ".thumb.jpg") != '')
    {   
    }
    // If thumb doesn't exist,it's created now
  else
  {    
        if ((strtolower($thumbdateiendung) == ".jpg") OR (strtolower($thumbdateiendung) == ".jpeg")){
        $src_img = imagecreatefromjpeg($fullname); 
        }
        if (strtolower($thumbdateiendung) == ".gif"){
        $src_img = imagecreatefromgif($fullname); 
        }
        if (strtolower($thumbdateiendung) == ".png"){
        $src_img = imagecreatefrompng($fullname); 
        }
    
        $origx=imagesx($src_img);
      $origy=imagesy($src_img);
        
        // Maximum width and height of the thumbnails
      $max_x = 90;
      $max_y = 90;
      
      // Calc, if thumb has has to be squeezed from width or height
        if($origx >= $origy AND $origx > $max_x)
        {
        $faktor = $origx / $max_x;    
        $new_x = $origx / $faktor;
      $new_y = $origy / $faktor;    
        }
        
        elseif($origy > $origx AND $origy > $max_y)
        {
        $faktor = $origy / $max_y;    
        $new_x = $origx / $faktor;
      $new_y = $origy / $faktor;    
        }
        
        else
        {
        $new_x = $origx;
        $new_y = $origy;
      }
      
      // Squeeze and write it into a file
        $dst_img = imagecreatetruecolor($new_x,$new_y);
      imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_  x,$new_y,imagesx($src_img),imagesy($src_img));
      imagejpeg($dst_img, $fullthumbname, 50);
    }
}                    

// Function to show a thumbnail
// -----------------------------------------------------------------
function showthumb ($thumbdateiname, $thumbdateiendung)
{
    $fullname = $thumbdateiname.$thumbdateiendung;
    $fullthumbname = $thumbdateiname.$thumbdateiendung.".thumb.jpg";
    if (file_exists($fullthumbname))
    {
    echo "<a href=\"$fullname\"><img src =\"$fullthumbname\" border=\"0\"></a>";
    echo "<!-- =========================== -->";
    echo "<!-- powered by TStarGallery 1.0 -->";
    echo "<!-- =========================== -->";

    }
    else
    {
    }

}
?>

Sorry!
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
That is absolutely, positively, the worst code I have ever seen in my life.

Here, have a better one.

PHP:
<?php

$page = ( empty($_GET['page']) ) ? 1 : intval($_GET['page']);
$perpage = ( empty($_GET['perpage']) ) ? 2 : intval($_GET['perpage']);
$offset = ( $page * $perpage ) - $perpage;

$files = array();
foreach( glob('{*.JPG,*.jpg,*.JPEG,*.jpeg,*.GIF,*.gif,*.PNG,*.png}', GLOB_BRACE) as $file ) {
	if( strpos($file, '.thumb.') === false && substr($file, 0, 1) != '.' )
		$files[] = $file;
}
sort($files);

echo '<p id="pagination">Pages: ';
for( $i = 0; $i < ( count($files) / $perpage ); $i++ )
	echo '<a href="?page='.($i + 1).'">'.($i + 1).'</a>';
echo '</p>';

echo '
	<table id="images" width="700">
		<tr>
';

for( $i = $offset; $i < ( $offset + $perpage ); $i++ ) {
	$file = @$files[$i];
	
	if( !$file )
		break;
	
	$extension	= substr($file, strrpos($file, '.'));
	$basename	= basename($file, $extension);
	
	$thumb_file	= $basename . '.thumb' . $extension;
	
	if( !file_exists($thumb_file) )
		resize_image($file, $thumb_file);
	
	echo '
			<td><img src="'.$thumb_file.'" alt="'.$thumb_file.'" /></td>
	';
	
	if( $i % 5 == 0 && $i > 0 ) {
		echo '
		</tr>
		<tr>
		';
	}
}

echo '
		</tr>
	</table>
';

function resize_image($if, $of = null, $quality = 75, $new_w = 150, $new_h = 150) {
	
	list($old_x, $old_y, $mime, $attr) = getimagesize($if);
	
	$im = null;
	
	switch( $mime ) {
		case 1:
			$type = 'gif';
			$im = imagecreatefromgif($if);
			break;
		case 2:
			$type = 'jpg';
			$im = imagecreatefromjpeg($if);
			break;
		case 3:
			$type = 'png';
			$im = imagecreatefrompng($im);
			break;
	}
	
	// If $im hasn't been created by now, $if isn't a valid image type.
	if( !$im )
		return false;
	
	if( $old_x > $new_w || $old_y > $new_h ) {
		if( $old_x > $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $old_y * ($new_h / $old_x);
		}
		if( $old_x < $old_y ) {
			$thumb_w = $old_x * ($new_w / $old_y);
			$thumb_h = $new_h;
		}
		if( $old_x == $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $new_h;
		}
		
		// Resize!
		$dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopyresampled($dst_img, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
		
		$im = $dst_img;
	}
	
	if( $of && !is_writable(dirname($of)) ) {
		die("Whoops - your album directory is not writable. Please fix this, or I won't be able to generate any thumbnails!");
	}
	
	// Output the image.
	if( $type == 'jpg' )
		imagejpeg($im, $of, $quality); 
	elseif( $type == 'png' )
		imagepng($im, $of); 
	else
		imagegif($im, $of);
	
	@imagedestroy($dst_img); 
	@imagedestroy($im);
}
?>

(Mostly untested but should work fine)
 
Last edited:
Associate
OP
Joined
5 Feb 2006
Posts
129
Location
Birmingham
robmiller, just tested it and it works good, exept it only creates the first 16 thumbs. If you click on page two or three it displays the same thumbs as page one!
 
Associate
OP
Joined
5 Feb 2006
Posts
129
Location
Birmingham
PHP:
<?php

$page = ( empty($_GET['page']) ) ? 1 : intval($_GET['page']);
$perpage = ( empty($_GET['perpage']) ) ? 15 : intval($_GET['perpage']);
$offset = ( $page * $perpage ) - $perpage;

$files = array();
foreach( glob('{*.JPG,*.jpg,*.JPEG,*.jpeg,*.GIF,*.gif,*.PNG  ,*.png}', GLOB_BRACE) as $file ) {
	if( strpos($file, '.thumb.') === false && substr($file, 0, 1) != '.' )
		$files[] = $file;
}
sort($files);

echo '<p id="pagination">Pages: ';
for( $i = 0; $i < ( count($files) / $perpage ); $i++ )
	echo '<a href="?page='.($i + 1).'">'.($i + 1).'</a>';
echo '</p>';

echo '
	<table id="images" width="700">
		<tr>
';

for( $i = $offset; $i < ( $offset + $perpage ); $i++ ) {
	$file = @$files[$i];
	
	if( !$file )
		break;
	
	$extension	= substr($file, strrpos($file, '.'));
	$basename	= basename($file, $extension);
	
	$thumb_file	= $basename . '.thumb' . $extension;
	
	if( !file_exists($thumb_file) )
		resize_image($file, $thumb_file);
	
	echo '
			<td><img src="'.$thumb_file.'" alt="'.$thumb_file.'" /></td>
	';
	
	if( $i % 5 == 0 && $i > 0 ) {
		echo '
		</tr>
		<tr>
		';
	}
}

echo '
		</tr>
	</table>
';

function resize_image($if, $of = null, $quality = 75, $new_w = 150, $new_h = 150) {
	
	list($old_x, $old_y, $mime, $attr) = getimagesize($if);
	
	$im = null;
	
	switch( $mime ) {
		case 1:
			$type = 'gif';
			$im = imagecreatefromgif($if);
			break;
		case 2:
			$type = 'jpg';
			$im = imagecreatefromjpeg($if);
			break;
		case 3:
			$type = 'png';
			$im = imagecreatefrompng($im);
			break;
	}
	
	// If $im hasn't been created by now, $if isn't a valid image type.
	if( !$im )
		return false;
	
	if( $old_x > $new_w || $old_y > $new_h ) {
		if( $old_x > $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $old_y * ($new_h / $old_x);
		}
		if( $old_x < $old_y ) {
			$thumb_w = $old_x * ($new_w / $old_y);
			$thumb_h = $new_h;
		}
		if( $old_x == $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $new_h;
		}
		
		// Resize!
		$dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopyresampled($dst_img, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
		
		$im = $dst_img;
	}
	
	if( $of && !is_writable(dirname($of)) ) {
		die("Whoops - your album directory is not writable. Please fix this, or I won't be able to generate any thumbnails!");
	}
	
	// Output the image.
	if( $type == 'jpg' )
		imagejpeg($im, $of, $quality); 
	elseif( $type == 'png' )
		imagepng($im, $of); 
	else
		imagegif($im, $of);
	
	@imagedestroy($dst_img); 
	@imagedestroy($im);
}
?>

I dont want to take the **** but i have one more problem i have looked at the code but cant work it out. I have it set to put 15 pics on page and 5 on a row. But it puts 6 on the first row???

and on the second page it puts only 1 on the top row the rows below are fine.

Can anyone help
 
Last edited:
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
PHP:
<?php

$page = ( empty($_GET['page']) ) ? 1 : intval($_GET['page']);
$perpage = ( empty($_GET['perpage']) ) ? 15 : intval($_GET['perpage']);
$offset = ( $page * $perpage ) - $perpage;

$files = array();
foreach( glob('{*.JPG,*.jpg,*.JPEG,*.jpeg,*.GIF,*.gif,*.PNG,*.png}', GLOB_BRACE) as $file ) {
	if( strpos($file, '.thumb.') === false && substr($file, 0, 1) != '.' )
		$files[] = $file;
}
sort($files);

echo '<p id="pagination">Pages: ';
for( $i = 0; $i < ( count($files) / $perpage ); $i++ )
	echo '<a href="?page='.($i + 1).'">'.($i + 1).'</a>';
echo '</p>';

echo '
	<table id="images" width="700">
		<tr>
';

for( $i = $offset; $i < ( $offset + $perpage ); $i++ ) {
	$file = @$files[$i];
	
	if( !$file )
		break;
	
	$extension	= substr($file, strrpos($file, '.'));
	$basename	= basename($file, $extension);
	
	$thumb_file	= $basename . '.thumb' . $extension;
	
	if( !file_exists($thumb_file) )
		resize_image($file, $thumb_file);
	
	echo '
			<td><img src="'.$thumb_file.'" alt="'.$thumb_file.'" /></td>
	';
	
	if( ($i + 1) % 5 == 0 ) {
		echo '
		</tr>
		<tr>
		';
	}
}

echo '
		</tr>
	</table>
';

function resize_image($if, $of = null, $quality = 75, $new_w = 150, $new_h = 150) {
	
	list($old_x, $old_y, $mime, $attr) = getimagesize($if);
	
	$im = null;
	
	switch( $mime ) {
		case 1:
			$type = 'gif';
			$im = imagecreatefromgif($if);
			break;
		case 2:
			$type = 'jpg';
			$im = imagecreatefromjpeg($if);
			break;
		case 3:
			$type = 'png';
			$im = imagecreatefrompng($im);
			break;
	}
	
	// If $im hasn't been created by now, $if isn't a valid image type.
	if( !$im )
		return false;
	
	if( $old_x > $new_w || $old_y > $new_h ) {
		if( $old_x > $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $old_y * ($new_h / $old_x);
		}
		if( $old_x < $old_y ) {
			$thumb_w = $old_x * ($new_w / $old_y);
			$thumb_h = $new_h;
		}
		if( $old_x == $old_y ) {
			$thumb_w = $new_w;
			$thumb_h = $new_h;
		}
		
		// Resize!
		$dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopyresampled($dst_img, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
		
		$im = $dst_img;
	}
	
	if( $of && !is_writable(dirname($of)) ) {
		die("Whoops - your album directory is not writable. Please fix this, or I won't be able to generate any thumbnails!");
	}
	
	// Output the image.
	if( $type == 'jpg' )
		imagejpeg($im, $of, $quality); 
	elseif( $type == 'png' )
		imagepng($im, $of); 
	else
		imagegif($im, $of);
	
	@imagedestroy($dst_img); 
	@imagedestroy($im);
}
?>
 
Back
Top Bottom