Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instant Gallery

Status
Not open for further replies.

KarveR

MIS
Dec 14, 1999
2,065
GB
Want to greate a gallery from a directory full of pics?, need thumbnails on the fly? need it to update itself?
LOOK NO FURTHER :)

-----------------------------------------------------
Gallery.php

<?php
// made by Karv copyright him and the ones he ripped some code off
// cheers peeps - abuse this code as you like and feel free to name me in it :p
function getimgArray($dir,$sort='asort')
{

if ( is_dir($dir) ) {

$fd = @opendir($dir);

while ( ($part = @readdir($fd)) == TRUE ) {

clearstatcache();

if ($part != &quot;.&quot; && $part != &quot;..&quot; && (ereg(&quot;\\.jpg\$&quot;,$part) || ereg(&quot;\\.jpeg\$&quot;,$part))) {

$dir_array[] = $part;
}
}

if($fd == TRUE) {
closedir($fd);
}

if (is_array($dir_array)) {

$sort($dir_array);

$dir_file_count = count($dir_array);

Return $dir_array;

} else {

Return FALSE;
}

} else {

Return FALSE;
}

}

$file_array = getimgArray('.','asort'); //set directory and sort type here
echo &quot;<div align=\&quot;center\&quot;><table border=1 width=30%>&quot;;
$i=1;
foreach ($file_array as $file_name) {

if ($i%2) {

echo &quot;<tr><td>&quot;;

echo &quot;<div align=\&quot;center\&quot;><a href=$file_name><img src=\&quot;image.php?file_name=$file_name\&quot; border=0></div></a>&quot;;

echo &quot;</td><td>&quot;;
} else {

echo &quot;<div align=\&quot;center\&quot;><a href=$file_name><img src=\&quot;image.php?file_name=$file_name\&quot; border=0></div></a>&quot;;
echo &quot;</td></tr>&quot;;
}

$i++;

}
echo &quot;</table></div>&quot;;
unset($idx);

?>

--------------------------------------------------------
image.php

<?php
$src_img = ImageCreateFromJPEG($file_name); // need to add a $dir here if you need it outside the image folder

/* desired width of the thumbnail */
$picsize = 30; // sets the overall size of the thumb

/* grabs the height and width */
$new_w = imagesx($src_img);
$new_h = imagesy($src_img);

/* calculates aspect ratio */
$aspect_ratio = $new_h / $new_w;

/* sets new size */
$new_w = $picsize;
$new_h = abs($new_w * $aspect_ratio);

/* creates new image of that size */
$dst_img = imagecreate($new_w,$new_h);

/* copies resized portion of original image into new image */
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));

imageJPEG($dst_img);
?>

-------------------------------------------------------
have fun :)

Oh btw both these need to be in you image directory but I guess you guys are good enough to change that if needed, clues are (hopefully provided in the code) ***************************************
Party on, dudes!
[cannon]
 
with instant gallery,
anybody run into and past:

Warning: Invalid argument supplied for foreach() in /home/x/public_html/image/gallery.php on line 48

tia
jc@pineiro.info


 
Man this post is old, this happens when no files of the given type are found in the directory (.) - you may need to ensure that the files are readable by all for this to work correctly. ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top