Update my php thumbnail code - DFWstangs Forums
 
LinkBack Thread Tools Display Modes
post #1 of 8 (permalink) Old 05-07-2005, 05:16 PM Thread Starter
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
Update my php thumbnail code

I have a couple of sections on my website that generates thumbnails on the fly. The problem I was having was that after putting a lot of files in the directory, the generation would take forever. The one thing I was not doing was creating actual thumbnails files because I didnt want to setup a script that checked to see if that file was there first and if not, generate it etc etc etc. Well I went ahead and did all that. I know there isn't a lot of techie php nerds on here, but the code is pretty simple and I dont mind passing off the little knowledge I got to help out any one else so here it is.

Ex: http://abecx.net/wow/
( the part in question is actually an inline frame http://abecx.net/wow/thumb.php )

thumb.php content:

PHP Code:
<?
        
include("../inc/functions.php");
#Lets find out what directory where in
        
$dir rtrim(`pwd`) . "/";
        
$dh  opendir($dir);
#Lets see only the files with .jpg and put them in an array.
        
while (false !== ($filename readdir($dh))) {
                if(
strstr($filename".jpg")) {
                        
$files[] = $filename;
                }
        }
#The following counts how many instances are in the array.
#You can do a rsort to reverse sort the array, (http://www.php.net/sort)
#The loop statement goes through and checks to see if the file is in the ./thumbs/*
# directory are there and if not, make them.  You'll have to follow the
#tn_check over to the functions section to see how that works out.
        
$t count($files);
        
sort($files);
        
$l $t 1;
        for ( 
$i=0$i $t$i++ )  {
                if (
$i == $l) {
                        
tn_check($dir,$files[$i]);
                        print 
"</p>";
                }
                else {
                        
tn_check($dir,$files[$i]);
                        echo 
"<hr>\n";
                }
        }
?>
And here are the functions which are from another file:

PHP Code:
<?

#This fucntion checks to see if the thumbnail is there, if it is, then it makes
#hyperlink.
function tn_check($path,$file)  {
        
$t_file $path "/thumb/" $file;
        if (
file_exists($t_file)) {
                echo 
"<p align=center><a href=./$file target=_blank>";
                echo 
"<img border=0 src=./thumb/$file></a>";
        }
        else {
                
create_tn($path,$file);
                echo 
"<p align=center><a href=./$file target=_blank>";
                echo 
"<img border=0 src=./thumb/$file></a>";
        }
}

#Again, this obviously creates thumbnails.
function create_tn($path,$file) {
        
$tdir $path "thumb";
        if (
is_dir($tdir)){
        }
        else {
                
mkdir($tdir,0777);
        }
        
$im imagecreatefromjpeg($path $file);
        
$size GetImageSize($path $file);
        
$x 240;
        
$y 200;
        
$thumb imagecreatetruecolor($x$y);
        
imagecopyresampled($thumb$im0000$x$y$size[0], $size[1]);
        
imagejpeg($thumb,$path."thumb/$file");
}
?>

AbecX is offline  
Sponsored Links
Advertisement
 
post #2 of 8 (permalink) Old 05-07-2005, 05:18 PM Thread Starter
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
If anyone can find much use of this, i'll be glad to help out in this thread so you can intergrate it into your site. Anyone that wants to belittle or compliment my code is free to do so, I would like some pointers on more efficeny if anywhere applicable. for instance I didnt like calling the shell to do the pwd command to figure out where I was at directory wise, if someone knows how to do that with php, I'd love to know.

AbecX is offline  
post #3 of 8 (permalink) Old 05-09-2005, 12:54 AM
 
Join Date: Sep 2002
Posts: 259
I was working on the same type of thing when I lost my HD. Thanks for the free code.

Check out getcwd()
http://docs.php.net/en/function.getcwd.html
black96gtstick is offline  
 
post #4 of 8 (permalink) Old 05-09-2005, 12:56 AM
You lookin' at mah EYE?!
 
DarkWolf's Avatar
 
Join Date: May 2000
Location: Portland, OR
Posts: 8,316
Awesome. I've been thinking of setting up a section on my site for pictures and screenshots, but haven't found a good thumbnail generator yet. Just end up batch processing them either in ACDSee or Photoshop (haven't worked with Gimp's commandline tools yet but will be doing that soon. Still trying to ween myself off Photoshop.), and creating a thumb page with links to the images.

If I ever get around to doing that, I'll give this a whirl and let you know if I figure out any further tweaks.

DarkWolf
Graphic Design / Photography / Web Design
DarkWolf is offline  
post #5 of 8 (permalink) Old 05-09-2005, 01:37 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Quote:
Originally Posted by AbecX
If anyone can find much use of this, i'll be glad to help out in this thread so you can intergrate it into your site. Anyone that wants to belittle or compliment my code is free to do so, I would like some pointers on more efficeny if anywhere applicable. for instance I didnt like calling the shell to do the pwd command to figure out where I was at directory wise, if someone knows how to do that with php, I'd love to know.
getcwd is your friend.

My 401K is now a 400K (was 301K)
01WhiteCobra is offline  
post #6 of 8 (permalink) Old 05-09-2005, 08:22 PM
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Pretty interesting stuff there Cruz - I am a complete n00b to all kinds of programming but it sure looks alot like the C++ I am working on now...I never realized they are that similar - or are they ?
turbostang is offline  
post #7 of 8 (permalink) Old 05-10-2005, 02:15 AM
 
Join Date: Sep 2002
Posts: 259
I changed it a bit to fit my application a bit more. Im finally back to where I was when my hard drive took a dump. Thanks again for the code AbecX.

Just in case anyone cares heres the shit
DSL hosting (kinda slow)

-Images popup in new windows the same size as the image (might have problems with strict popup blockers but it works in firefox so fuck everyone else)
-Image names can have spaces in them


PHP Code:
<?
        $dir 
getcwd() . "/";
        include(
"../../include/functions.php");
#Javascript popup window function        
        
js_popp();
#Lets find out what directory were in
        
$dir getcwd() . "/";
        
$dh  opendir($dir);
#Lets see only the files with .jpg and put them in an array.
        
while (false !== ($filename readdir($dh)))
        {
                if(
strstr($filename".jpg"))
                {
                        
$files[] = $filename;
                }
        }  
#The following counts how many instances are in the array.
#You can do a rsort to reverse sort the array, (http://www.php.net/sort)
#The loop statement goes through and checks to see if the file is in the ./thumbs/*
# directory are there and if not, make them.  You'll have to follow the
#tn_check over to the functions section to see how that works out.
        
$t count($files);
        
sort($files);
        
$l $t 1;
        for ( 
$i=0$i $t$i++ )
        {
#Give files ability to have spaces in the names            
                        
$link str_replace(" ","%20",$files[$i]);
#Get size of original image                        
                        
$size1 getimagesize("$files[$i]");
                        if (
$i == $l)
                {
                                
tn_check($dir,$files[$i],$link,$size1[0],$size1[1]);
                        print 
"</p>";
                }
                else
                {
                        
tn_check($dir,$files[$i],$link,$size1[0],$size1[1]);
                        echo 
"<hr>\n";
                }
        }
?>
functions

PHP Code:
<?

#This fucntion checks to see if the thumbnail is there, if it is, then it makes
#hyperlink.
function tn_check($path,$file,$link,$sizex,$sizey)
{
        
$t_file $path "thumb/" $file;
        if (
file_exists($t_file))
        {
                echo 
"<p align=center><a href=javascript:popp('$link','width=".($sizex+20).",height=".($sizey+20)."')>";
                echo 
"<img border=0 src=./thumb/$link></a>";
        }
        else
        {
                
create_tn($path,$file);
                echo 
"<p align=center><a href=javascript:popp('$link','width=".($sizex+20).",height=".($sizey+20)."')>";
                echo 
"<img border=0 src=./thumb/$link></a>";
        }
}

#Again, this obviously creates thumbnails.
function create_tn($path,$file)
{
        
$tdir $path "thumb";
        if (
is_dir($tdir))
        {
        }
        else
        {
                
mkdir($tdir,0777);
        }
        
$im imagecreatefromjpeg($path $file);
        
$size GetImageSize($path $file);
        
$x 240;
        
$y 200;
        
$thumb imagecreatetruecolor($x$y);
        
imagecopyresampled($thumb$im0000$x$y$size[0], $size[1]);
        
imagejpeg($thumb,$path."thumb/$file");
}

#Echo popup window javascript
function js_popp()
{
    echo 
"
    <script language=JavaScript>
    var newwindow;
    function popp(url,sizex,sizey)
    {
        newwindow=window.open(url,name,sizex,sizey);
        if (window.focus) {newwindow.focus()}
    }
    </script>
"
;
}
?>
black96gtstick is offline  
post #8 of 8 (permalink) Old 05-10-2005, 02:39 AM Thread Starter
\(_o)/
 
AbecX's Avatar
 
Join Date: Nov 2001
Location: Las Colinas
Posts: 25,373
lol awesome, someone used my code ;p

You know, you could put those out in tables, with the count command, you'll know how many images there are and you could loop a row/column command

btw, I have the same speakers

AbecX is offline  
Sponsored Links
Advertisement
 
Reply

Bookmarks

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the DFWstangs Forums forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



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

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

 
For the best viewing experience please update your browser to Google Chrome