PDA

View Full Version : PHP Gallery help


HarryDude
10-06-07, 06:12 PM
I used a PHP gallery tutorial a while back and found I could do more with it. At the moment it does exactly what I want it to do except for one cosmetic error which causes it to look dodgy.

www.harrydude.co.nr/gallerytest.php

Have a look at that, at the far right of the thumbnails it tries to make an image out of the path to the gallery folder :\ its a small error but I can't get rid of it.

http://img401.imageshack.us/img401/6403/48812134nt1.jpg

I do have a work around but it requires me to have two seperate codes. (found on the actual gallery link on the site.)

Here's the script:

<?
$path = "gallery";
$files = "0";
$handle=opendir($path);
while ($file = readdir($handle)) {
if(!is_dir($path."/".$file) && $file != "." && $file!="..") {
$extension = explode (".", $file);
$extension_i = (count($extension) - 1);
$ext = $extension[$extension_i];
if($ext=="jpg"||$ext=="jpeg"||$ext=="JPG"||$ext=="JPEG") {
$images[$files] = $file;
$files++;
}
}
}
if(!$_GET["i"]) {
foreach($images as $image) {
$source = $path."/".$image;
$tsource = "thumbs/".$image;
$stsource = "small/".$image;
$thumbnail_h = "100";
$quality = "60";
$sthumbnail_h = "650";
$squality = "100";
if(file_exists($source)&&!file_exists($tsource)&&!file_exists($stsource)) {
$size = getimagesize($source);
$w = round($size[0]/($size[1]/$thumbnail_h));
$h = $thumbnail_h;
$resize = imagecreatetruecolor($w, $h);
$sw = round($size[0]/($size[1]/$sthumbnail_h));
$sh = $sthumbnail_h;
$sresize = imagecreatetruecolor($sw, $sh);
switch ($size['mime']) {
case 'image/jpeg':
$im = imagecreatefromjpeg($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagecopyresampled($sresize, $im, 0, 0, 0, 0, $sw, $sh, $size[0], $size[1]);
imagejpeg($resize, 'thumbs/'.$image, $quality);
imagejpeg($sresize, 'small/'.$image, $squality);
break;
case 'image/png':
$im = imagecreatefrompng($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagecopyresampled($sresize, $im, 0, 0, 0, 0, $sw, $sh, $size[0], $size[1]);
imagepng($resize, 'thumbs/'.$image, $quality);
imagepng($sresize, 'thumbs/'.$image, $squality);
break;
}
imagedestroy($im);
}
echo '<a href="?i='.$image.'"><img src="'.$tsource.'" border="0"></a>
';
}
}
echo '<a href="'.$path.'/'.$_GET["i"].'" target="_blank"><img src="small/'.$_GET["i"].'" border="0"></a>';
if($_GET["i"]&&file_exists($path.'/'.$_GET["i"]))
$id = array_search($_GET["i"],$images);
$files2 = $files-1;
if($id>"0"&&$id<$files2) {
$next_id = $id+1;
$prev_id = $id-1;
echo '<div align="center">';
echo '<br><a href="?i='.$images[$prev_id].'"><b>Previous</b></a><b>: :</b><a href="?i='.$images[$next_id].'"><b>Next</b></a><br><br><b><a href="gallerytest.php">Back to Main Gallery</a></b>';
}
if($id=="0") {
$next_id = "1";
echo '<br><b>Previous: :</b><a href="?i='.$images[$next_id].'"><b>Next</b></a><br><br><b><a href="gallerytest.php">Back to Main Gallery</a></b>';
}
if($id==$files2) {
$prev_id = $files2-1;
echo '<br><a href="?i='.$images[$prev_id].'"><b>Previous</b></a><b>: :Next</b><br><br><b><a href="gallerytest.php">Back to Main Gallery</a></b>';
}
echo '</div>';
?>

Could anyone locate the problem for me? I'm a noob at php and can go just about as far as editing a script to do more of what I want it to do. (in this case I made it resize the source image twice, one for thumbnails and one for viewing on the site, and then a link from that to the original in a new window)

To test the gallery youreself make 3 folders on the server: 'gallery', 'thumbs' and 'small'. Save the script as 'gallerytest.php' and upload the images to the 'gallery' folder :).

Images are just tests btw.

QuakerX10
10-07-07, 10:38 AM
Didn't run it or anything, but try removing this line:
echo '<a href="'.$path.'/'.$_GET["i"].'" target="_blank"><img src="small/'.$_GET["i"].'" border="0"></a>';

pross
10-07-07, 10:55 AM
try:
if ($_GET["i"]){echo '<a href="'.$path.'/'.$_GET["i"].'" target="_blank"><img src="small/'.$_GET["i"].'" border="0"></a>';}

HarryDude
10-07-07, 11:17 AM
Omg cheers pross it actually worked :D

pross
10-07-07, 12:17 PM
of course it did :)