chriscloyd Posted August 22, 2006 Share Posted August 22, 2006 I want to be able to upload images into a database a thumbnail of the image also and i want it to how would i go about doing that? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 First you need to write a script to upload the file onto server. Once you've got it there you can use functions from the GD library to create thumbnails.You can find out if you've got GD library installed by using this simple script:[code]<?php phpinfo();?>[/code]Upload that onto the server and call it as a script and look at the output.You can search php.net for the GD functions, how to call them and how they work. Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 22, 2006 Share Posted August 22, 2006 upload image is easy[code]<form method='post' action='' enctype='multipart/form-data'><input type='file' name='ufile' /><br /><input type='submit' value='Upload File' /></form>[/code]Then the upload[code]<?phpif($_FILES["ufile"]["name"]){$dir = "images/".$_FILES["ufile"]["name"];if(move_uploaded_file($_FILES["ufile"]["tmp_name"], $dir){$query = "INSERT INTO table SET image = '".$dir."'";$result = mysql_query($query);if($result){echo "The image has been uploaded";}else{echo "There was an error uploading to the database.<br />".mysql_error();}}else{echo "There was an error uploading the file";}}?>[/code]For the TN, check out http://es2.php.net/manual/en/function.imagecopyresized.php Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted August 22, 2006 Author Share Posted August 22, 2006 see i dont know how to write the upload script out of all my time php coding i cant do upload scripts i can do any other script just about execpt uploads Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted August 22, 2006 Author Share Posted August 22, 2006 yes but i dont want it to resize or the image will look messed up Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 Have a peek at this as well...http://uk2.php.net/features.file-upload Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 It depends what you mean by messed up.If you constrain the image then it shouldn't look that bad. There are scripts around which you can use as functions which will read the dimensions of the image and resize it accordingly to make it fit a set size. This way you upload only one file and can get two as a result - the original file and the thumbnail. If you don't need the original then you can delete it. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted August 22, 2006 Author Share Posted August 22, 2006 this is my code[code]<?phpinclude("db.php");if($_FILES["file"]["name"]){$dir = "pictures/".$_FILES["file"]["name"];if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir){$file = $dir;$filename = $file;header('Content-type: image/jpeg');list($width, $height) = getimagesize($filename);$newwidth = 150;$newheight = 150;$thumb = imagecreatetruecolor($newwidth, $newheight);$source = imagecreatefromjpeg($filename);imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);imagejpeg($thumb);$dir2 = "thumbnails/".$_FILES["file"]["name"];$thumbnail = move_uploaded_file(imagejpeg($thumb), $dir2);$query = "INSERT INTO gallery SET image = '$dir', thumbnail = '$thumbnail'";$result = mysql_query($query);if($result){echo "The image has been uploaded";}else{echo "There was an error uploading to the database.<br />".mysql_error();}}else{echo "There was an error uploading the file";}}?> [/code]i get this errorParse error: syntax error, unexpected '{' in /home/.giro/chriscloyd/idoliztic-designs.com/upload.php on line 7 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 This line:[code]if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir){[/code]is missing a closing bracket and should be[code]if(move_uploaded_file($_FILES["file"]["tmp_name"], $dir)){[/code] Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted August 22, 2006 Author Share Posted August 22, 2006 okay it wont upload the thumbnail and it wont add it into the database help me please Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 I wouldn't store the thumbnails in the database directly, I'd store them on the server instead.I've opted for this method myself and it works great although I'm using pre-set filenames for the images and storing them in separate folders for each user that registers.You could, however, store the filenames in the database instead. Upon uploading check the filename and see if it exists in the database - if it does then decline the file.Loads of options are open. Quote Link to comment Share on other sites More sharing options...
spires Posted August 22, 2006 Share Posted August 22, 2006 [code]<?phpinclude('db.php');if(isset($_POST['Submit'])) { $size = 90; // the thumbnail height $width = 110; // size of width $filedir = 'images/'; // the directory for the original image $thumbdir = 'thumbs/'; // the directory for the thumbnail image $largedir = 'large/'; // the directory for the large image $prefix = ''; // the prefix to be added to the original name $maxfile = '1000000'; // max file size $mode = octdec('0666'); // octdec -- Octal to decimal. // The mode parameter consists of three octal number components specifying access restrictions for the owner, // the user group in which the owner is in, and to everybody else in this order. e.g (666) $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; // if you have a the image and name then carry on if (isset($_FILES['image']['name'])) { //random digits to add to the file name $random_digit=rand(00000000,99999999); $ext = '.jpg'; $new_file_name=$random_digit.$ext; // $prod_img = the image folder and the image and name $prod_img = $filedir.$new_file_name; // $prod_img_thumb = the thumb folder, the prefix (if you want it) and the image and name $prod_img_thumb = $thumbdir.$prefix.$new_file_name; //move_uploaded_file -- Moves an uploaded file to a new location. //move the uploaded file to the image folder with a tempory name. move_uploaded_file($userfile_tmp, $prod_img); // chmod -- Changes file mode, // set the user interface for the image chmod ($prod_img, $mode); // getimagesize -- Get the size of an image // find the size of the original image $sizes = getimagesize($prod_img); $aspect_ratio = $sizes[1]/$sizes[0]; // if its less than the size you want it, dont change if ($sizes[1] <= $size) { $new_width = $sizes[1]; $new_height = $sizes[0]; // else, change image size }else{ $new_height = $size; $new_width = $width; // $new_width = abs($new_height/$aspect_ratio); // this code will only change the height, and make the width in ratio } $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image'); $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image'); ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling'); ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving'); imagedestroy($destimg); } $description = $_POST['description']; $name = $_POST['name']; $cat = $_POST['cat']; $sql = mysql_query("INSERT INTO image (description, name, new_file_name, cat) VALUES ('$description', '$name', '$new_file_name', '$cat')"); echo 'Files Uploaded'; } echo ' <center> <table cellpadding="0" cellspacing="0" width="400" class="TLRB_border"> <tr> <td align="center"> <br><strong>"Please make sure all images are \'NO BIGGER\' than 600px wide"</strong><br><br> <strong>"All images must be .JPG files"</strong><br><br> <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> Thumbnail tag (Please keep it short):<br> <input type="text" name="name" size="40" value="'.$_POST['name'].'"><br><br> File Description:<br> <input type="text" name="description" size="40" value="'.$_POST['description'].'"><br><br> <input type="hidden" name="id" value="'.$_POST['id'].'"> <br>File to upload/store in database:<br> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form> </td> </tr> </table><br> </center>';?>[/code] Quote Link to comment Share on other sites More sharing options...
spires Posted August 22, 2006 Share Posted August 22, 2006 The post above is exactly what i use. Its resizes the images then restores the quality. you can choose the size of the thumbnails. give them names, and discriptions. it also generates a random name for the image so you dont have to worry about two images the same. just create two folders on you serverthumbs and imagesi've tryed to explain whats gong on next to each line.Hope this helps. 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.