php_beginner_83 Posted September 29, 2009 Share Posted September 29, 2009 Hi Everyone I'm trying to create thumbnails, using the gd library, on the fly. I was wondering if it will be possible to have these thumbnails be links. I'm not sure if this can be done or should I just create and save the thumbnails. I know this way I'd be able to make them links. This is my code ... $filename = 'images/alcatraz1.jpg'; $thumbWidth = 50; $thumbHeight = 50; // load image $img = imagecreatefromjpeg($filename); // get image size $width = imagesx($img); $height = imagesy($img); // set size of thumbnails if ($width > $height) { $newWidth = $thumbWidth; $newHeight = $thumbWidth * ($height/$width); } else { $newWidth = $thumbHeight * ($width/$height); $newHeight = $thumbHeight; } // create temporary image $tempImg = imagecreatetruecolor($newWidth, $newHeight); // create the thumbnail imagecopyresampled($tempImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // output thumbnail imagejpeg($tempImg) Quote Link to comment Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 By on the fly I assume you mean something like: <img src="thumb.php?image=something.jpg&width=100&height=100" alt="" /> ? If so, and you wanted to link them to the original image you could just do: <a href="something.jpg"><img src="thumb.php?image=something.jpg&width=100&height=100" alt="" /></a> Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted September 29, 2009 Author Share Posted September 29, 2009 Thanks AlexWD. Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted September 29, 2009 Author Share Posted September 29, 2009 I'm trying to loop through an array of images and am having problems getting it to work...any ideas? Thanks While (...........) $currentImage = "images//" . substr(strrchr($row['Path'],92),1); $counter++; if ($counter % 3 == 0) { echo '<a href=#><img src="shannaThumb.php?image=\"$currentImage\"" alt="" /></a><br/>'; } else { echo '<a href=#><img src="shannaThumb.php?image=\"$currentImage\"" alt="" /></a>'; } Quote Link to comment Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 It would be nice if you were a little more specific, and if you showed us the array of images, would be easier for us to tell you how to set it up. Right away though, I see this: $currentImage = "images//" . substr(strrchr($row['Path'],92),1); Is that supposed to be images/ with only 1 '/'? Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted September 29, 2009 Author Share Posted September 29, 2009 I've had to use 2 '/' in other code because 1 didnt work so I figured I would do the same here but I have tried removing one and the code still didnt work. My array of images comes from a database of images. This is my code I'm trying to take each image from the database and create a thumbnail and then have each thumbnail be a link. Thanks for the help. $result = mysql_query("SELECT * FROM pictures INNER JOIN pics_in_albums ON pictures.ID = pics_in_albums.PicID INNER JOIN albums ON pics_in_albums.AlbumID = albums.ID WHERE albums.ID = 1") or die(mysql_error()); $paths = array(); $counter = 0; //fetch tha data from the database while ($row = mysql_fetch_assoc($result)) { $currentImage = "images//" . substr(strrchr($row['Path'],92),1); $counter++; if ($counter % 3 == 0) { echo '<a href=#><img src="shannaThumb.php?image=$currentImage alt="" /></a><br/>'; } else { echo '<a href=#><img src="shannaThumb.php?image=$currentImage" alt="" /></a>'; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.