 |
|
|
10-09-2009, 05:03 PM
|
ok. Mercy, I need Help
|
Posts: 2
Name: Dave
|
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; } ?>
|
|
|
|
10-09-2009, 06:12 PM
|
Re: ok. Mercy, I need Help
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
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
|
|
|
|
10-09-2009, 06:47 PM
|
Re: ok. Mercy, I need Help
|
Posts: 2
Name: Dave
|
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?
|
|
|
|
10-10-2009, 11:06 AM
|
Re: ok. Mercy, I need Help
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
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
|
|
|
|
10-10-2009, 11:19 AM
|
Re: ok. Mercy, I need Help
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
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..
|
|
|
|
|
« Reply to ok. Mercy, I need Help
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|