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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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)
 



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:21 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