adam291086 Posted April 8, 2008 Share Posted April 8, 2008 Anybody know of a tutorial on how to generate thumbnails on the fly? Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/ Share on other sites More sharing options...
discomatt Posted April 8, 2008 Share Posted April 8, 2008 http://www.google.ca/search?q=php+thumbnails+on+the+fly That wasn't difficult Just as a warning, generating thumbnails on the fly can be taxing on a server. If you're with a shared host, they may not appreciate generation on the fly if you start seeing some decent traffic. Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512344 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 http://www.phpfreaks.com/forums/index.php/topic,191541.0.html Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512348 Share on other sites More sharing options...
adam291086 Posted April 8, 2008 Author Share Posted April 8, 2008 i am using the code by craygo, thanks very much. Although i am getting no images, just a box with a red cross. Heres the code <?php $folder = glob("images/*.jpg"); foreach($folder AS $file) { //large image $large = $file; //thumbnail ?> <td> <img src="imageresize.php?maxsize=xxx&source=<?php echo $file ?>" border=0 /> </td> <td> <?php } ?> when i look at the html source code of the images i get <td> <img src="imageresize.php?maxsize=xxx&source=images/Picture 043.jpg" border=0 /> </td> <td> Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512361 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 have to change the maxsize(xxxx) to the max size you want <img src="imageresize.php?maxsize=200&source=<?php echo $file ?>" border=0 /> Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512370 Share on other sites More sharing options...
adam291086 Posted April 8, 2008 Author Share Posted April 8, 2008 Still the same any idea Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512373 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 i imagine you put the function in it's own file and called it imageresize.php?? And that file is in the same directory as the script your running?? Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512379 Share on other sites More sharing options...
adam291086 Posted April 8, 2008 Author Share Posted April 8, 2008 yep. I copied the code exactly as its printed in your thread, and called it imageresize.php Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512382 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 I copied your exact code, I happen to have an images folder also , and it works fine. thumbs.php <?php $folder = glob("images/*.jpg"); foreach($folder AS $file) { //large image $large = $file; //thumbnail ?> <td> <img src="imageresize.php?maxsize=200&source=<?php echo $file; ?>" border=0 /> </td> <td> <?php } ?> and imageresize.php <?php /********************************** * Will resize an image to a * * max width or height and keep * * aspect ratio Name this file * * anything you like. * * I will use imageresize.php * **********************************/ header('Content-type: image/jpeg'); function resampleimage($maxsize, $sourcefile, $imgcomp=0){ // SET THE IMAGE COMPRESSION $g_imgcomp=100-$imgcomp; // CHECK TO SEE IF THE IMAGE EXISTS FIRST if(file_exists($sourcefile)){ // FIRST WE GET THE CURRENT IMAGE SIZE $g_is=getimagesize($sourcefile); /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/ // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){ // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE $new_width=$g_is[0]; $new_height=$g_is[1]; } else { // GET VALUE TO CALCULATE WIDTH AND HEIGHT $w_adjust = ($maxsize / $g_is[0]); $h_adjust = ($maxsize / $g_is[1]); // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT if($w_adjust <= $h_adjust){ // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER $new_width=($g_is[0]*$w_adjust); $new_height=($g_is[1]*$w_adjust); } else { // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER $new_width=($g_is[0]*$h_adjust); $new_height=($g_is[1]*$h_adjust); } } //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." ) $image_type = strrchr($sourcefile, "."); //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($image_type) { case '.jpg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.jpeg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.png': $img_src = imagecreatefrompng($sourcefile); break; case '.gif': $img_src = imagecreatefromgif($sourcefile); break; default: echo("Error Invalid Image Type"); die; break; } // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT $img_dst=imagecreatetruecolor($new_width,$new_height); // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]); // OUTPUT THE IMAGE AS A JPEG. // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE. imagejpeg($img_dst); // DESTROY THE NEW IMAGE imagedestroy($img_dst); return true; } else { return false; } } // NOW CALL THE IMAGE FROM ANY OTHER PAGE WITH <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> xxx=a value for the max size resampleimage($_GET['maxsize'], $_GET['source']); ?> They cannot be in the same file. If it is not working I am not sure why. I copied and pasted your code verbatim and it works fine. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512388 Share on other sites More sharing options...
adam291086 Posted April 8, 2008 Author Share Posted April 8, 2008 they are definitely in the same file. I have a folder called gallery. In there there is the two files along with another folder storing the images Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512392 Share on other sites More sharing options...
dennismonsewicz Posted April 8, 2008 Share Posted April 8, 2008 Try this thread: http://www.phpfreaks.com/forums/index.php/topic,191420.msg859481.html#msg859481 Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-512393 Share on other sites More sharing options...
adam291086 Posted April 9, 2008 Author Share Posted April 9, 2008 still having problems here is the current situation. I have a folder called gallery and in here there are the imageresizer script and a template.php that calls on the imagersizer script. The images are coming from a folder called imager which is in the gallery folder. Is this correct Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-513260 Share on other sites More sharing options...
writer Posted April 9, 2008 Share Posted April 9, 2008 It could be that the script can't handle the space in your image title (perhaps "Picture_043.jpg" would work). Maybe you need to echo it out in ASCII? <img src="imageresize.php?maxsize=xxx&source=images/Picture 043.jpg" border=0 /> Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-513365 Share on other sites More sharing options...
laffin Posted April 9, 2008 Share Posted April 9, 2008 <img src="imageresize.php?maxsize=xxx&source=images/Picture 043.jpg" border=0 />/[code] will not work, use urlencode, or remove/replace spaces in the name [/code] Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-513367 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 i have removed the spaces and nothing has changed Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514015 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 List each file and the code in each file so we can get to the bottom of this. Also list your directory structure. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514039 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 ok, i have a folder in the root directory of my server called gallery. In here i have; image.php which is the script you gave me template.php which calls on your script to generate the images a folder call images which contains all my images these are all the scripts image.php <?php error_reporting(E_ALL); /********************************** * Will resize an image to a * * max width or height and keep * * aspect ratio Name this file * * anything you like. * * I will use imageresize.php * **********************************/ header('Content-type: image/jpeg'); function resampleimage($maxsize, $sourcefile, $imgcomp=0){ // SET THE IMAGE COMPRESSION $g_imgcomp=100-$imgcomp; // CHECK TO SEE IF THE IMAGE EXISTS FIRST if(file_exists($sourcefile)){ // FIRST WE GET THE CURRENT IMAGE SIZE $g_is=getimagesize($sourcefile); /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/ // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){ // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE $new_width=$g_is[0]; $new_height=$g_is[1]; } else { // GET VALUE TO CALCULATE WIDTH AND HEIGHT $w_adjust = ($maxsize / $g_is[0]); $h_adjust = ($maxsize / $g_is[1]); // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT if($w_adjust <= $h_adjust){ // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER $new_width=($g_is[0]*$w_adjust); $new_height=($g_is[1]*$w_adjust); } else { // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER $new_width=($g_is[0]*$h_adjust); $new_height=($g_is[1]*$h_adjust); } } //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." ) $image_type = strrchr($sourcefile, "."); //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($image_type) { case '.jpg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.jpeg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.png': $img_src = imagecreatefrompng($sourcefile); break; case '.gif': $img_src = imagecreatefromgif($sourcefile); break; default: echo("Error Invalid Image Type"); die; break; } // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT $img_dst=imagecreatetruecolor($new_width,$new_height); // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]); // OUTPUT THE IMAGE AS A JPEG. // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE. imagejpeg($img_dst); // DESTROY THE NEW IMAGE imagedestroy($img_dst); return true; } else { return false; } } // NOW CALL THE IMAGE FROM ANY OTHER PAGE WITH <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> xxx=a value for the max size resampleimage($_GET['maxsize'], $_GET['source']); ?> template.php <?php error_reporting(E_ALL); $folder = glob("images/*.jpg"); foreach($folder AS $file) { //large image $large = $file; //thumbnail ?> <td> <img src="image.php?maxsize=200&source=<?php echo $file ?>" border=0 /> </td> <td> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514058 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 the only way this will not work is if GD in not activated on your server. Do you know if it is?? Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514075 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 no i don't know if it's active or not. It seems it's not Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514078 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 is the web server yours?? Do you have access to the php.ini file?? To find out if it is on just make a temp page and inside put in <?php phpinfo(); ?> This will output php setting about a third of the way down the page there should be a section called gd. if it's enabled then something unknown is going on, but if it is not there or sayd disabled then you have to activate it. And you can only do that in the php.ini file. Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514086 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 according to the phpinfo its all enabled. What the f..k is going on Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514095 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 is this a public or private site?? Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514102 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 not sure what you mean by that, the hosting company is 1 and 1. Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514108 Share on other sites More sharing options...
craygo Posted April 10, 2008 Share Posted April 10, 2008 ok can I get to the site from here. can you give me a link Ray Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514115 Share on other sites More sharing options...
adam291086 Posted April 10, 2008 Author Share Posted April 10, 2008 yeah its adamplowman.co.uk/template.php Quote Link to comment https://forums.phpfreaks.com/topic/100205-how-to-create-a-thumbnail-on-the-fly/#findComment-514119 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.