vetman Posted June 4, 2009 Share Posted June 4, 2009 I have this script to upload images into a database and view them. My problem is the thumbnail images don't use the original file name along with the thumb_ designation. It save the original filename with the full size image, but generates a random number to add to the thumbnail image designation. It works fine except I would like to use the original image name along with the thumb_ designation. Any help would be appreciated. Thanks in advance. <?php //Include connection files. require_once("db_connection.php"); ?> <form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <label for="title" id="title">Title</label><br /> <input type="text" name="title"/><br /> <label for="imageupload" id="imageupload">Browse Image</label><br /> <input type="file" name="imagefile"/><br /> <input type="submit" name="submit" value="Submit" /><br /> </form> <?php //If the form has been submitted if(isset($_POST['submit'])){ //Validate the fields if(strlen($_POST['title']) < 2 || empty($_FILES["imagefile"]) || $_FILES["imagefile"]["size"] > 1048576){ echo "You must enter a title longer than two characters and select an image to upload the image should be no larger than 1MB"; }else{ //Assign the title to a variable $imgTitle = $_POST['title']; //Assign the paths needed to variables for use in the script $filePathFull = "assets/imgf/".$_FILES["imagefile"]["name"]; $filePathThumb = "assets/imgt/"; //Assign the files TMP name and TYPE to variables for use in the script $tmpName = $_FILES["imagefile"]["tmp_name"]; $imageType = $_FILES["imagefile"]["type"]; //Use a switch statement to check the extension of the file type. If file type is not valid echo an error switch ($imageType) { case $imageType == "image/gif": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/jpeg": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/pjpeg": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/png": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/x-png": move_uploaded_file($tmpName,$filePathFull); break; default: echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.'; } // Get information about the image list($srcWidth, $srcHeight, $type) = getimagesize($filePathFull); //Original image ratio $origRatio = $srcWidth/$srcHeight; //Create the correct file type based on imagetype switch($type) { case IMAGETYPE_JPEG: $startImage = imagecreatefromjpeg($filePathFull); break; case IMAGETYPE_PNG: $startImage = imagecreatefrompng($filePathFull); break; case IMAGETYPE_GIF: $startImage = imagecreatefromgif($filePathFull); break; default: return false; } //Set target height and width. $width = 200; $height = 200; //Calculate the height and width to keep the correct image ratio if ($width/$height > $origRatio) { $width = $height*$origRatio; }else{ $height = $width/$origRatio; } //Create the thumbnail with true colours $thumbImage = imagecreatetruecolor($width, $height); //Generate the resized image imagecopyresampled($thumbImage, $startImage, 0,0,0,0, $width, $height, $srcWidth, $srcHeight); //Generate a random number to append the filename. $ran = "thumb_".rand () ; $thumb2 = $ran.".jpg"; //Create the path to store the thumbnail in. $thumb_Add_thumb = $filePathThumb; $thumb_Add_thumb .= $thumb2; //Create the new image and put it into the thumbnails folder. imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2"); //Create sql query and insert the values into the database $sql = "INSERT INTO imageUpload (title, thumbpath) VALUES ('$imgTitle', '$thumb_Add_thumb')"; $query = mysql_query($sql) or die(mysql_error()); }} ?> Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 4, 2009 Share Posted June 4, 2009 You can get rid of the random part and then replace this. //Create the new image and put it into the thumbnails folder. imagejpeg($thumbImage, "" .$filePathThumb. "$thumb2"); with //Create the new image and put it into the thumbnails folder. imagejpeg($thumbImage, "" .$filePathThumb. "$_FILES["imagefile"]["name"]); essentially replacing $thumb2 which is your random thumb name with the actual name of the file. Quote Link to comment Share on other sites More sharing options...
vetman Posted June 4, 2009 Author Share Posted June 4, 2009 Thanks for the fast reply, I input your code, got this error message: Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/r/i/g/rigtway/html/tuts/imageupload.php on line 103 Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 4, 2009 Share Posted June 4, 2009 just a syntax error, try this... imagejpeg($thumbImage,$filePathThumb.$_FILES[imagefile][name]); Quote Link to comment Share on other sites More sharing options...
vetman Posted June 4, 2009 Author Share Posted June 4, 2009 I'm not sure what happened, but it doesn't write what it should to the database. It puts assets/imgt/ in the thumbpath but that's it, it doesn't fill in the fullpath file name either. It does put in the title in the right column. ??? Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 5, 2009 Share Posted June 5, 2009 Sorry hard to troubleshoot without playing with the code in front of me.. Try echoing out variables to see what the results are yielding. For example prior to the imagejpeg statement echo out $_FILES[imagefile][name] to see what value it holds Do the same for $filePathThumb If these both hold valid values then create a new var such as $newthumb = $filePathThumb.$_FILES[imagefile][name]; then in the imagejpeg statement use imagejpeg($thumbImage,$newthumb); Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 5, 2009 Share Posted June 5, 2009 switch ($imageType) { case $imageType == "image/gif": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/jpeg": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/pjpeg": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/png": move_uploaded_file($tmpName,$filePathFull); break; case $imageType == "image/x-png": move_uploaded_file($tmpName,$filePathFull); break; default: echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.'; } switch ($imageType) { case "image/gif": move_uploaded_file($tmpName,$filePathFull); break; case "image/jpeg": move_uploaded_file($tmpName,$filePathFull); break; case "image/pjpeg": move_uploaded_file($tmpName,$filePathFull); break; case "image/png": move_uploaded_file($tmpName,$filePathFull); break; case "image/x-png": move_uploaded_file($tmpName,$filePathFull); break; default: echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.'; } Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 5, 2009 Share Posted June 5, 2009 why not just put the filename on the end of hte thumbnail rather than a random number?! i.e. get rid of //Generate a random number to append the filename. $ran = "thumb_".rand () ; $thumb2 = $ran.".jpg"; and replace with //append filename to thumb $thumb2 = "thumb_".$_FILES["imagefile"]["name"]; ...? Quote Link to comment Share on other sites More sharing options...
vetman Posted June 5, 2009 Author Share Posted June 5, 2009 Thanks, now I have an image in the thumbpath but it doesn't have the image in the fullpath column. I think we have part of the problem fixed. Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 5, 2009 Share Posted June 5, 2009 you're not inserting the full path into the database...? //Create sql query and insert the values into the database $sql = "INSERT INTO imageUpload (title, thumbpath) VALUES ('$imgTitle', '$thumb_Add_thumb')"; you'd need to change to //Create sql query and insert the values into the database $sql = "INSERT INTO imageUpload (title, thumbpath, fullImageColumn) VALUES ('$imgTitle', '$thumb_Add_thumb', '$filePathFull')"; the fullImageColumn should be renamed to whatever the column is that stores your full image name...? and $filePathFull contains the full file path to that image... 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.