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!

auto thumbnail.

Status
Not open for further replies.

kpdvx

Programmer
Dec 1, 2001
87
US
anyone know of a good script that will take images in a directory and auto thumbnail them, along with next, prev links? i had a really good script before... but i have since lost it. Nothing fancy, no mysql, just something that takes large images in a directory, and creates a page displaying their thumbnails along with next and prev. links.

Thanks,
Skyler
 
I did one a while ago that would allow upload and then take all images and create a page showing them using the GD library, dunno if it'll help but here it is:
-----------------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;\\.JPG\$&quot;,$part))) { // unix is picky, ok?

$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;
}

}

// sort out whats goin on

$file_array = getimgArray('.','asort'); //set directory and sort type here

$i=1;

// check we have stuff to play with

if (!empty($file_array)) {
echo &quot;<body bgcolor=black>&quot;;

echo &quot;<div align=\&quot;center\&quot;><table border=0 width=90% cellpadding=0 cellspacing=0><tr>&quot;;

foreach ($file_array as $file_name) {

$textfile=&quot;$file_name.txt&quot;;

//see if we are making left or right cells

if ($i%5) {





echo &quot;<td>&quot;;

echo &quot;<div align=\&quot;center\&quot;><a href=$file_name><img src=\&quot;image.php?file_name=$file_name\&quot; border=3 alt=\&quot;&quot;;
if (file_exists($textfile)){

include($textfile);

}else{

echo &quot;Picture no. $i&quot;;

}

echo &quot;\&quot;></div></a></td>&quot;;


} else {



// pic 5



echo &quot;<td>&quot;;
echo &quot;<div align=\&quot;center\&quot;><a href=$file_name><img src=\&quot;image.php?file_name=$file_name\&quot; border=3 alt=\&quot;&quot;;
if (file_exists($textfile)){

include($textfile);

}else{

echo &quot;Picture no. $i&quot;;

}
echo &quot;\&quot;></div></a>&quot;;

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

}

$i++;

}

} else {

// no piccies

echo &quot;no piccies in the directory&quot;;

}

echo &quot;</table></div>&quot;;

unset($idx);

?>

<!-- write a form for uploading piccies and captions -->

<div align=&quot;center&quot;>
<form enctype=&quot;multipart/form-data&quot; action=&quot;uploaded.php&quot; method=&quot;post&quot;>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; align=&quot;center&quot;>
<tr>
<td>
<div align=&quot;center&quot;>Upload a piccy<br>Caption<input type=&quot;text&quot; name=&quot;caption&quot;>
<input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;150000&quot;>
<input type=hidden name=return_where value=&quot;virtualkev_old&quot;>
<input type=hidden name=return_to value=&quot;3_test.phtml&quot;>
File:<input name=&quot;userfile&quot; type=&quot;file&quot;>
<input type=&quot;submit&quot; value=&quot;Send File&quot; name=&quot;submit&quot;>
</div>
</td>
</tr>
</table>
</form>
</div>



-------------------image.php--------------------------
<?php
$src_img = ImageCreateFromJPEG($file_name);

/* desired width of the thumbnail */
$picsize = 100;

/* 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);
$new_h = 120;
/* 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);
ImageDestroy($dst_img);
?>

-------------------------------------

Oh btw it stores textfiles for captions too, if there isnt a textfile it'll just say &quot;picture no#&quot;.

Have fun. ***************************************
Party on, dudes!
[cannon]
 
Here's some logic I just developed. You can specify a max width AND height. It calculates the aspect ratios and decides whether to use the max width or max height to do the scaling and maintains the images original aspect ratio (no &quot;stretching&quot;).
Code:
$imageratio = $oldwidth / $oldheight;
if (($maxwidth > 0) && ($maxheight > 0)) { $maxratio = $maxwidth / $maxheight; }
if ($maxratio == $imageratio) {
	// do not change the $maxwidth or $maxsize values
} else if (($maxratio > $imageratio) || ($maxwidth < 1)) {
	$maxwidth = abs($maxheight * $imageratio);
} else {
	$maxheight = abs($maxwidth / $imageratio);
}
$newimage = imagecreatetruecolor($maxwidth, $maxheight);
imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $maxwidth, $maxheight, $oldwidth, $oldheight);
imagejpeg($newimage);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top