TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Thumbnails from a directory (http://www.talkphp.com/script-giveaway/1572-thumbnails-directory.html)

Rendair 12-02-2007 06:59 PM

Thumbnails from a directory
 
I have this code i did a awhile ago to create thumbnails out of images from a directory on your web server.

PHP Code:

function createthumb($name,$filename,$new_w,$new_h)
{
    
$system=explode('.',$name);
    if (
preg_match('/jpg|jpeg/',$system[1])){
        
$src_img=imagecreatefromjpeg($name);
    }
    if (
preg_match('/png/',$system[1])){
        
$src_img=imagecreatefrompng($name);
    }
    
    
$old_x=imageSX($src_img);
    
$old_y=imageSY($src_img);
    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;
    }
    
    
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    
        if (
preg_match("/png/",$system[1]))
    {
        
imagepng($dst_img,$filename); 
    } else {
        
imagejpeg($dst_img,$filename); 
    }
    
    
imagedestroy($dst_img); 
    
imagedestroy($src_img); 



Create thumbnails and display the results

PHP Code:

$path "images/";
$img_dir = @opendir($path);
$entriesperline=3//Number of images on each row of the table
$counter=1;
 
 
while (
$img_file readdir($img_dir))
{
 
createthumb('images/'.$img_file,'thumbs/tn_'.$img_file,100,100); 
}  

$path "images/";
$img_dir = @opendir($path);
$entriesperline=5;
$counter=1;  
echo 
"<center><table border='0' width=600 cellspacing=0 cellpadding=5 class=gallery>";
while (
$img_file readdir($img_dir))
{
$filetype filetype($img_file);
if(
$img_file!="." && $img_file!=".." && $filetype!="dir")

if(
$counter%$entriesperline==1)
{
echo 
"<tr><td><a href=\"images/$img_file\" rel=\"thumbnail\"><img src=thumbs/tn_$img_file border=5></a> </td>";
}
else if(
$counter%$entriesperline==0)
{
echo 
"<td><a href=\"images/$img_file\" rel=\"thumbnail\"><img src=thumbs/tn_$img_file border=5></a> </td></tr>";
}
else
{
echo 
"<td><a href=\"images/$img_file\" rel=\"thumbnail\"><img src=thumbs/tn_$img_file border=5></a> </td>";
}
$counter++;
//exit loop
$counter--;
if(
$counter%$entriesperline!=0)
{
echo 
"</tr>";
}
echo 
"</table></center>"

The function can easily be set up just to create one thumbnail

PHP Code:

createthumb('link to original image','thumbnail new name and location',width,height); 

The new thumbnail should work out the ratio so the image is the same dimensions, but smaller version.

Gets the job done i feel :)

Wildhoney 12-04-2007 03:56 AM

You're throwing a lot of code our way! What's up with using good old glob? :-( I love glob these days, couldn\'t live without it. Similar to regex in a way, but just more simplified. Used to find and traverse files and folders.

I remember doing something similar for a guy on here the other week. Well, me and Karl. Okay, more Karl than me. But it took us a while to do! Quite tedious stuff. It\'s a shame you weren\'t around then with your handy script.

One question though, do you not indent your PHP code? There are many indent styles out there, and they really improve the look of any code many times over.

Rendair 12-04-2007 07:38 AM

I find myself writting alot of my code in notepad lol usually get so involved in the code and figuring it out...tend to lose out on tidiness lol

Tanax 12-04-2007 08:01 AM

Hehe, this script would be pretty useful for my gallery class :O

Rendair 12-04-2007 03:57 PM

haha thats always good to hear


All times are GMT. The time now is 02:49 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0