leblanc Posted October 9, 2009 Share Posted October 9, 2009 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 $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; } ?> Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/ Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 Not tested, but this should worK: Change <?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); } } to: <?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,$p."_tn",100,100); } } You are only changing lines 4 and 9 Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933926 Share on other sites More sharing options...
leblanc Posted October 9, 2009 Author Share Posted October 9, 2009 Wow, thank you for such the quick response, your to kind. Its actually getting closer. but there is still a problem. Its putting the _tn after the .jpg for example "picturename.jpg_tn" Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933945 Share on other sites More sharing options...
AustinJ Posted October 9, 2009 Share Posted October 9, 2009 seems as if the $filename is actually the filename + the extension... create seperate variables for the filename and the extension and then you will be able to place _tn at the end of the filename...im sure someone on here will do it...i suck at php Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933955 Share on other sites More sharing options...
leblanc Posted October 9, 2009 Author Share Posted October 9, 2009 Yeah, that sounds logical. I wouldn't even know where to begin though. Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933976 Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 replace this: createthumb($p,$p."_tn",100,100); with: $fName = explode(".",$p); $thumb2 = $fName[0]."_tn.".$fName[1]; createthumb($p,$thumb2,100,100); Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933981 Share on other sites More sharing options...
leblanc Posted October 9, 2009 Author Share Posted October 9, 2009 Johnsmith!! It worked. Your the best. Thank you so much for your help. I cannot wait to learn more php and be able to figure that stuff out. Thanks again. Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-933993 Share on other sites More sharing options...
leblanc Posted October 9, 2009 Author Share Posted October 9, 2009 John, I have one more quick question i thought maybe u could look at. When I have this script aka thumbs.php that we are working on in the folder with the images, it works fine as the first two lines of the code is <?php $imagefolder='.'; $thumbsfolder='.'; When I put the thumb.php in a folder one up and try to call it from there, i get a million and one warnings <?php $imagefolder='images/'; $thumbsfolder='images/'; I get these error messages when I try to call from a folder down from the thumbs.php. Warning: imagecreatefromjpeg(1958bsa.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb3.php on line 41 Warning: imagesx(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 43 Warning: imagesy(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 44 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 61 Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb3.php on line 69 Its actually pulling the images out of the folder "images/" and creating the thumbnails in one folder up from it where the thumbs.php is and giving me all those errors? Does that make sense? LOL Thanks Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-934014 Share on other sites More sharing options...
johnsmith153 Posted October 10, 2009 Share Posted October 10, 2009 Try: $imagefolder='../images/'; $thumbsfolder='../images/'; The above probably might work. Below definitely will: $imagefolder='/mnt/w1000/d25/s27/b0142d1b/www/images/'; $thumbsfolder='/mnt/w1000/d25/s27/b0142d1b/www/images/'; Or whatever the path is to this folder. The path above will work if the images folder is stored in the web root (i.e www.yourwebsite.com/images) If images is somewhere else, you will need to change this. Let me know where the images folder is, and I can help. Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-934066 Share on other sites More sharing options...
leblanc Posted October 10, 2009 Author Share Posted October 10, 2009 Thanks John, I actually tried to use the total path of the directory in there earlier today before i gave up and asked you another question lol, and i got the exact same errors. Weird Huh? Link to comment https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/#findComment-934186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.