jamkelvl Posted December 29, 2009 Share Posted December 29, 2009 Okay so I put this code together a few months ago and it worked just fine. I do not take credit for writing all of it, it's more or less a bunch of tutorials all put together. Either way, I've recently tried to use it again for another site and have no idea as to why it is not working! gallery.php is supposed to iterate thru a directory looking for all jpegs, then create thumbnails and place them in another directory called /thumbs/ currently.. it is not doing this. example.php basically just displays the images... The reason I split this into two files is because gallery.php was called on by more than one page as this site has/had more than one photo gallery. Below is gallery.php: <?PHP function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) { // open the directory $dir = opendir( $pathToImages ); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); if (file_exists("{$pathToThumbs}{$fname}")) { break; } else { // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpg' ) { // load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); if ($new_height > 150) { // change calculation because image is too tall $new_width = floor( $width * ( 150 / $height ) ); $new_height = 150; } // create a new tempopary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a file imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } } // close the directory closedir( $dir ); } } ?> and example.php: <?PHP include("gallery.php"); createThumbs("images/gallery/","images/gallery/thumbs/",150); # image types to display $imagetypes = array("image/jpeg", "image/gif"); function getImages($dir) { global $imagetypes; # array to hold return value $retval = array(); # add trailing slash if missing if(substr($dir, -1) != "/") $dir .= "/"; # full server path to directory $fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir"; $d = @dir($fulldir) or die("getImages: Failed opening directory $dir for reading"); while(false !== ($entry = $d->read())) { # skip hidden files if($entry[0] == ".") continue; # check for image files $f = escapeshellarg("$fulldir$entry"); if(in_array($mimetype = trim( `file -bi $f` ), $imagetypes)) { $retval[] = array( "file" => "/$dir$entry", "size" => getimagesize("$fulldir$entry") ); } } $d->close(); return $retval; } # fetch image details $images = getImages("new/images/gallery/thumbs"); # constants $search = array("new/images/gallery/thumbs/", ".JPG"); $replace = array("", ""); $count = 0; # display on page foreach($images as $img) { echo $img['file']; // changes the string for bigger image $imgstring = $img['file']; $searchfor = array("/new/images/gallery/thumbs/"); $newimgstring = array(""); $bigimg = str_replace($searchfor, $newimgstring, $imgstring); // if thumbnail is clicked displays larger image switch ($_GET['img']) { case ($_GET['img'] == "whysthiswork?") : echo ('<a href="gallery.php?img='.$bigimg.'"><img src="'.$img['file'].'" /></a>'); break; case ($_GET['img']) : echo '<a href="gallery.php"><img src="images/gallery/'.$_GET['img'].'" /></a><br/>'; echo '<a href="gallery.php">Back</a>'; include("footer.php"); exit(); } $count++; } ?> No errors.. it just seems to sit there. I can remember I used to comment out a line or two of code to sort turn the gallery on or off so that it does not re-create the thumbnails on the fly every time a page is called. Thanks for the help or at least reading my code! Link to comment https://forums.phpfreaks.com/topic/186614-any-help/ Share on other sites More sharing options...
iPixel Posted December 29, 2009 Share Posted December 29, 2009 I'd like to see how you set $pathToThumbs. Either i missed it or it's not included in your posted code. Also, are you sure that the server has full write privelages to that folder? That might be an issue. Link to comment https://forums.phpfreaks.com/topic/186614-any-help/#findComment-985610 Share on other sites More sharing options...
jamkelvl Posted December 29, 2009 Author Share Posted December 29, 2009 I'm on the same server that I did this on before. I tried messing around with the other page I did this with and can not get it to work. //All other code omitted function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) //All other code omitted createThumbs("images/gallery/","images/gallery/thumbs/",150); Link to comment https://forums.phpfreaks.com/topic/186614-any-help/#findComment-985611 Share on other sites More sharing options...
iPixel Posted December 29, 2009 Share Posted December 29, 2009 Sorry i guess i wasnt clear, i always have my foot in my mouth... when i asked hwo you set $pathToThumbs i meant somewhere in your code you must have something like $pathToThumbs = /thumbs/etc... let me see that. Link to comment https://forums.phpfreaks.com/topic/186614-any-help/#findComment-985622 Share on other sites More sharing options...
jamkelvl Posted December 29, 2009 Author Share Posted December 29, 2009 $ Sorry i guess i wasnt clear, i always have my foot in my mouth... when i asked hwo you set $pathToThumbs i meant somewhere in your code you must have something like $pathToThumbs = /thumbs/etc... let me see that. $pathToThumbs is set when I call the function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) This is done here... // All other code omitted... createThumbs("images/gallery/","images/gallery/thumbs/",150); Therefore, the $pathToThumbs is "images/gallery/thumbs" As of right now, I have the gallery.php code workign 100% it now creates the thumbs. As for example.php I am now working to have it properly iterate thru the proper directories and display the images. ***** EDIT ***** Consider this solved. Thanks for taking interest. Feel free to use this code if you'd like. Link to comment https://forums.phpfreaks.com/topic/186614-any-help/#findComment-985635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.