TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-02-2007, 06:59 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default 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 :)
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 2 Users Say Thank You to Rendair For This Useful Post:
Tanax (12-04-2007), Wildhoney (12-02-2007)
Old 12-04-2007, 03:56 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-04-2007, 07:38 AM   #3 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

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
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-04-2007, 08:01 AM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Hehe, this script would be pretty useful for my gallery class :O
Tanax is offline  
Reply With Quote
Old 12-04-2007, 03:57 PM   #5 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

haha thats always good to hear
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design