r00tk1LL Posted August 2, 2007 Share Posted August 2, 2007 What is the best way to create a thumbnail from JPG/GIF/PNG images that are uploaded and save them as a separate file. Here is what I would like to do. User uploads a JPG/GIF/PNG image. A thumbnail is made of that file. The original and thumbnail are both saved to a directory. I would like to specify that the thumbnail should be no more than 100px wide and have the height automatically adjust to the right proportion I.E. original image is 200 X 200 thumbnail image will be 100 X 100 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/ Share on other sites More sharing options...
deadimp Posted August 2, 2007 Share Posted August 2, 2007 Just search the interwebs for code. I've got a thumbnail function in Thacmus (you can see it here, under Media::imgResize()), and there's more out there. Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314407 Share on other sites More sharing options...
r00tk1LL Posted August 2, 2007 Author Share Posted August 2, 2007 Thanks for the reply but that source was a little more complicated than I needed. I mean is there a more straight forward way to do it? Ive gone through a ton of web sites and found code like this: <?php //Name you want to save your file as $save = 'myfile.jpg'; $file = 'original.jpg'; echo "Creating file: $save"; $size = 0.45; header('Content-type: image/jpeg') ; list($width, $height) = getimagesize($file) ; $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; // Here we are saving the .jpg, you can make this gif or png if you want //the file name is set above, and the quality is set to 100% imagejpeg($tn, $save, 100) ; ?> I just cant seem to get the results I want out of it. Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314432 Share on other sites More sharing options...
deadimp Posted August 3, 2007 Share Posted August 3, 2007 The code I posted is about as straightforward as it's going to get. You're going to need proportions, check if the image is proportional, if not, find the largest dimension, find out the relation between that and the desired size, and resize your image accordingly. That's in the middle of the code, which you can cut out if you don't need the multiple image formats or what not. Tutorial doing just that: link Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314544 Share on other sites More sharing options...
simcoweb Posted August 3, 2007 Share Posted August 3, 2007 How you want it to work depends on if you want the thumbnail created at the time of upload. If that's the case then you'll need two functions working together. One to upload the file, confirm it's uploaded and then the second to create the thumbnail, move it to the required folder and then wrapping everything up with a query that inserts the image name (or possibly the path as well) for the original and the thumb and associate it with the id of the uploader. Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314576 Share on other sites More sharing options...
r00tk1LL Posted August 3, 2007 Author Share Posted August 3, 2007 Let me ask you guys an easier question, how would I grab a file that was sent through an html form and make and save a thumbnail of it? Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314586 Share on other sites More sharing options...
dbillings Posted August 3, 2007 Share Posted August 3, 2007 <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; if($filetype == "image/jpeg" || "image/gif" || "image/png"){ $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include("mysql_connect.php"); $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error: '.mysql_error()); mysql_close(); echo "<br />File $fileName uploaded<br />"; }else{ echo "<br />File type ".$filetype." not allowed. Must be JPEG,GIF or PNG.<br />"; } } ?> <fieldset id='submit'><legend>Upload Images</legend> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <label id='label'>Select image:</label><input id='input' name="userfile" type="file" id="userfile"><br /> <input id='submit' name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314612 Share on other sites More sharing options...
r00tk1LL Posted August 3, 2007 Author Share Posted August 3, 2007 I cant find the function in this that resizes the file, am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314926 Share on other sites More sharing options...
deadimp Posted August 3, 2007 Share Posted August 3, 2007 The code dbillings posted inserted the file into a database. You would need to add your own resizing code to that. Look into move_uploaded_file(), and after you've moved it, take your thumbnail function and use it on that file, generating the thumbnail in the relative directory (thumb/, or whatever you've chosen). Quote Link to comment https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-315246 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.