Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
ok. Mercy, I need Help
Old 10-09-2009, 05:03 PM ok. Mercy, I need Help
Junior Talker

Posts: 2
Name: Dave
Trades: 0
Hey All,

Im still relatively new to php and am trying to learn it at a solid pace, but I have been working with this existing script that automatically creates thumb nails of pictures in the same folder the script is in.

When the script generates the thumbnail image, it uses the existing name but adds a tn_ in front of the original name.
So for example
Original File:
"picturename.jpg"
Thumbnail created file
"tn_picturename.jpg"

My problem is, I want to get the extension of the thumbnail on the end of the original file name for example
"picturename_tn.jpg" <---- (the _tn is on at the end)

I have tried everything and cannot figure this out.
Its a pretty simple script as well.

Here is the script. I would really appreciate if someone could help me out on this small problem of mine.

PHP Code:
<?php 
$imagefolder
='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if (
$pics[0]!="")
{
    foreach (
$pics as $p)
    {
        
createthumb($p,"tn_".$p,100,100);
    }
}

/*
    Function ditchtn($arr,$thumbname)
    filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
    foreach (
$arr as $item)
    {
        if (!
preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
    }
    return 
$tmparr;
}

/*
    Function createthumb($name,$filename,$new_w,$new_h)
    creates a resized image
    variables:
    $name        Original filename
    $filename    Filename of the resized image
    $new_w        width of resized image
    $new_h        height of resized image
*/    
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); 
}

/*
        Function directory($directory,$filters)
        reads the content of $directory, takes the files that apply to $filter 
        and returns an array of the filenames.
        You can specify which files to read, for example
        $files = directory(".","jpg,gif");
                gets all jpg and gif files in this directory.
        $files = directory(".","all");
                gets all files.
*/
function directory($dir,$filters)
{
    
$handle=opendir($dir);
    
$files=array();
    if (
$filters == "all"){while(($file readdir($handle))!==false){$files[] = $file;}}
    if (
$filters != "all")
    {
        
$filters=explode(",",$filters);
        while ((
$file readdir($handle))!==false)
        {
            for (
$f=0;$f<sizeof($filters);$f++):
                
$system=explode(".",$file);
                if (
$system[1] == $filters[$f]){$files[] = $file;}
            endfor;
        }
    }
    
closedir($handle);
    return 
$files;
}
?>
leblanc is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-09-2009, 06:12 PM Re: ok. Mercy, I need Help
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
On the 10th line in your code above, change this line
PHP Code:
createthumb($p,"tn_".$p,100,100); 
into this

PHP Code:
createthumb($p,$p."_tn",100,100); 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-09-2009, 06:47 PM Re: ok. Mercy, I need Help
Junior Talker

Posts: 2
Name: Dave
Trades: 0
Lizciz, Thanks for the response.

I have actually already tried that, and it put the _tn at the end of the .jpg.

For example
imagename.jpg_tn

My friend mentioned this
"create separate variables for the filename and the extension and then you will be able to place _tn at the end of the filename"

How would i go about doing that.
I see his point and that is probably the problem right?
leblanc is offline
Reply With Quote
View Public Profile
 
Old 10-10-2009, 11:06 AM Re: ok. Mercy, I need Help
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Aha, sorry.
Then replace that one line with these

PHP Code:
$tmp explode('.'$p);
$thumb $tmp[0] . '_tn.' $tmp[1];
createthumb($p,$thumb,100,100); 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-10-2009, 11:19 AM Re: ok. Mercy, I need Help
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Actually, don't do that, cause it won't work :P
You have to edit the regexp that seperates the original images from thumbnails, in the ditchtn() function. Try this:

On the fourth line, change to these two lines

PHP Code:
$thumbExt "_tn";
$pics=ditchtn($pics,$thumbExt); 
Change the ditchtn() function to this (I only edited the preg_match() expression)

PHP Code:
function ditchtn($arr,$thumbname)
{
    foreach (
$arr as $item)
    {
        if (!
preg_match("/^[a-zA-Z0-9]+".$thumbname."\.[a-zA-Z]{3,4}/",$item)){$tmparr[]=$item;}
    }
    return 
$tmparr;

And the three lines in my previous post, change them into this

PHP Code:
$tmp explode('.'$p);
$thumb $tmp[0] . $thumbExt '.' $tmp[1];
createthumb($p,$thumb,100,100); 
That should do it.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366

Last edited by lizciz; 10-10-2009 at 11:21 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to ok. Mercy, I need Help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not 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 Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.85633 seconds with 12 queries