BarneyJoe Posted December 1, 2006 Share Posted December 1, 2006 Have just been looking at this, and found the following code that works when I specify a specisic image :[CODE]<?php$im_file_name = 'Photos/2Large.jpg'; $image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); ?> <?php $th_max_width = 120; $th_max_height = 120; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); ?> <?php $th_file_name = 'Thumbs/2Small.jpg'; imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new,$th_file_name,100); ?> [/CODE] However, I'm not quite getting the syntax for it to resize a dynamic image.I have a page with the following :[CODE]<img src="Photos/<?php echo $row_PhotoSimple['Photo_File']; ?>" alt="Photo" name="Photo" border="0">[/CODE]How can I reconcile the code so that the dynamic image is resized?Presumably it's this line in the first part that needs changing :[CODE]$im_file_name = 'Photos/2Large.jpg'; [/CODE] Where the filename needs to echo the field's contents, ie something along the lines of :[CODE]$im_file_name = 'Photos/ echo $row_PhotoSimple['Photo_File'];[/CODE]Any pointers?Cheers,Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/ Share on other sites More sharing options...
BarneyJoe Posted December 1, 2006 Author Share Posted December 1, 2006 Fixed - it just needed to be :[code]<?php$im_file_name = 'Photos/'.$row_PhotoSimple['Photo_File'];$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); ?> <?php $th_max_width = 250; $th_max_height = 250; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); ?> <?php $th_file_name = 'Thumbs/'.$row_PhotoSimple['Photo_File'];imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new,$th_file_name,100); ?>[/code]Is pretty much working - although I'm not sure exactly how the max height and width settings are working.In the above, they are 250 each which is mostly OK, but I don't think it's pixels.The image was originally constrained to 350x350, so I would have thought that using 350 as the max height and width would restrain the images to those limits, and retain their ratio.But even going as low as 250, some are extending beyond the old 350 width and messing up the page layout as a result.Anyone know how to fix this?Also, what modifications are needed to make it work to resize all images in a gallery page?Cheers,Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133384 Share on other sites More sharing options...
pkSML Posted December 1, 2006 Share Posted December 1, 2006 "Also, what modifications are needed to make it work to resize all images in a gallery page?"Save the php code that creates the thumbnails as thumb.php.Then call each image in your gallery with <img src="thumb.php?file=myself.jpg">For this to work, the input file in thumb.php will need to be $_GET['file'].Is this helpful? Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133386 Share on other sites More sharing options...
BarneyJoe Posted December 1, 2006 Author Share Posted December 1, 2006 Mostly - although not sure about the myself.jpg.Where I currently have :[code]<img src="../Photos/<?php echo $row_PhotoSimple['Photo_File']; ?>"[/code]Would I change that to :<img src="thumb.php?file=Thumbs/'.$row_PhotoSimple['Photo_File']"> Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133417 Share on other sites More sharing options...
pkSML Posted December 1, 2006 Share Posted December 1, 2006 [color=red]Make dynamically-generated thumbnails:[/color]Your script just saves the thumbnail to your server (in the thumbs folder). It seems you want it to dynamically create thumbs when thumb.php is loaded.To dynamically create it, thumb.php will have to send a JPG content-type header and output the thumbnail file.Here's the code to accomplish this:Assuming...1. the page displaying the thumbs is in the server root (gallery.php)2. thumb.php is in the server root3. images are in /photos4. $row_PhotoSimple['Photo_File'] contains a filename, like "picture1.jpg", "picture2.jpg" ...[b][u]gallery.php[/u][/b][code]<img src="thumb.php?file=/photos/<?php echo $row_PhotoSimple['Photo_File'];?>">[/code][b][u]thumb.php[/u][/b][code]<?php$im_file_name = $_GET['file'];$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); ?> <?php $th_max_width = 250; $th_max_height = 250; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); ?> <?phpHeader("Content-type: image/jpeg"); //Tells the browser this is an image. This must be put before any output.imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new, NULL, 100); //Output the imageImageDestroy($im_new); //Finish the image.ImageDestroy($im_old); //Finish the image.?>[/code] Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133521 Share on other sites More sharing options...
pkSML Posted December 2, 2006 Share Posted December 2, 2006 I just decided I wanted this script too. So I went to work and spent a few hours on it. It works perfectly now.You can get the code here:[url=http://stephen.calvarybucyrus.org/redirect.php?code=photo_gal]http://stephen.calvarybucyrus.org/redirect.php?code=photo_gal[/url] Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133773 Share on other sites More sharing options...
BarneyJoe Posted December 2, 2006 Author Share Posted December 2, 2006 Cheers.I'm at home now, but coincidentally am working on another project that I might use this for.I've just created a test page :[url=http://www.oriental-chamber.co.uk/gallery.php]http://www.oriental-chamber.co.uk/gallery.php[/url]Which includes the following code :[code]<table width="780" border="0" cellspacing="0" cellpadding="0"> <tr> <td>Preview</td> <td>Design</td> </tr> <?php do { ?> <tr> <td><img src="thumb.php?file=/photos/<?php echo $row_designs['Image'];?>"></td> <td><?php echo $row_designs['Carpet']; ?></td> </tr> <?php } while ($row_designs = mysql_fetch_assoc($designs)); ?></table></body></html><?phpmysql_free_result($designs);?>[/code]and also the thumb.php page :[code]<?php$im_file_name = $_GET['file'];$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); ?> <?php $th_max_width = 120; $th_max_height = 120; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); ?> <?phpHeader("Content-type: image/jpeg"); //Tells the browser this is an image. This must be put before any output.imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new, NULL, 100); //Output the imageImageDestroy($im_new); //Finish the image.ImageDestroy($im_old); //Finish the image.?>[/code]But it's just drawing blanks - so either I'm still missing something somewhere, or maybe GD isn't installed / running on my host server? It's standard hosting, but I'm not sure how I can check that?Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133774 Share on other sites More sharing options...
BarneyJoe Posted December 2, 2006 Author Share Posted December 2, 2006 Just missed you there - that seems to be working perfectly now - so thank you very much for posting that - it's much appreciated.Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133783 Share on other sites More sharing options...
pkSML Posted December 2, 2006 Share Posted December 2, 2006 You're welcome! Glad to help. ;D Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-133840 Share on other sites More sharing options...
BarneyJoe Posted December 5, 2006 Author Share Posted December 5, 2006 I knew there was something else! This little script is great, as it's doing exactly what I was looking for, but without being at all long winded. I've been trying to adapt it to display file types of than jpeg.I've been trying to adapt it to display other image file types, but am unsure with the syntax around the three lines referencing the jpegs :1. [code]header('Content-type: image/jpeg');[/code] to :[code]header('Content-type: image/jpeg, image/gif, image/png etc');[/code]?2. [code]$thumb = imagecreatetruecolor($newwidth, $newheight);$source = imagecreatefromjpeg($filename);[/code] to :[code]$thumb = imagecreatetruecolor($newwidth, $newheight);$source = imagecreatefromjpeg($filename);&& $source = imagecreatefromgif($filename);&& $source = imagecreatefrompng($filename);etc[/code]or[code]$thumb = imagecreatetruecolor($newwidth, $newheight);$source = imagecreatefromjpeg($filename);|| $source = imagecreatefromgif($filename);|| $source = imagecreatefrompng($filename);etc[/code]or something like :[code]$thumb = imagecreatetruecolor($newwidth, $newheight);if ( $filetype == "png") {$source = imagecreatefrompng($filename);} else if ( $filetype == "jpg") {$source = imagecreatefromjpeg($filename);} else ( $filetype == "gif") {$source = imagecreatefromgif($filename);};[/code]But i'm obviously barking up the wrong tree, as I can't seem to get it to work - but presume this is pretty straighforward if you know what the correct syntax is!Cheers,Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-135393 Share on other sites More sharing options...
BarneyJoe Posted December 5, 2006 Author Share Posted December 5, 2006 got it![code]<?php // Max Sizes $th_max_width = 120; // Maximum width of the thumbnail $th_max_height = 120; // Maximum height of the thumbnail //------------------- // File $filename = $_GET['file']; // Get image sizes, type & mime $img_data = getimagesize($filename); $width = $img_data[0]; $height = $img_data[1]; $img_type = $img_data[2]; // Numeric value (check in manual) $mime = $img_data['mime']; // Content type header('Content-type: ' . $mime); // Resize $ratio = ($width > $height) ? $th_max_width/$width : $th_max_height/ $height; $newwidth = round($width * $ratio); $newheight = round($height * $ratio); // Load $thumb = imagecreatetruecolor($newwidth, $newheight); switch ($img_type){ case 1: $source = imagecreatefromgif($filename); break; case 2: $source = imagecreatefromjpeg($filename); break; case 3: $source = imagecreatefrompng($filename); break; case 4: $source = imagecreatefromwbmp($filename); break;// Other file types... } //Resize imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagejpeg($thumb); ?> <?php exit; ?> [/code]looking at the getimagesize function it looks as tho' it's a bit limited with file type support - TIF and photoshop would have been handy.Iain Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-135415 Share on other sites More sharing options...
pkSML Posted December 5, 2006 Share Posted December 5, 2006 Thanks for your reply. Two heads are better than one! Link to comment https://forums.phpfreaks.com/topic/29099-gd-library-dynamic-thumbnail-generation/#findComment-135748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.