jimmyp3016 Posted February 27, 2007 Share Posted February 27, 2007 Hey Guys, Can someone help me get this script up and running. I know I need to echo in a few lines where it says "use what you need" but its still giving me errors. Any help would be great. <?php require_once( "../database.cls.php" ); require_once( "../functions.inc.php" ); userPage(); $user = $_SESSION['user']; echo doHeader( "../" ); echo doNavBar(); ?> <?php if ($_SERVER['REQUEST_METHOD' ] == "POST") { /* SUBMITTED INFORMATION - use what you need * temporary filename (pointer): $imgfile * original filename : $imgfile_name * size of uploaded file : $imgfile_size * mime-type of uploaded file : $imgfile_type */ /*== upload directory where the file will be stored relative to where script is run ==*/ $uploaddir = "./images/uploads/"; /*== get file extension (fn at bottom of script) ==*/ /*== checks to see if image file, if not do not allow upload ==*/ $pext = getFileExtension($imgfile_name ); $pext = strtolower($pext ); if (($pext != "jpg") && ( $pext != "jpeg")) { print "<h1>ERROR</h1>Image Extension Unknown.<br>" ; print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>" ; print "The file you uploaded had the following extension: $pext</p>\n "; /*== delete uploaded file ==*/ unlink ($imgfile); exit(); } //-- RE-SIZING UPLOADED IMAGE /*== only resize if the image is larger than 250 x 200 ==*/ $imgsize = GetImageSize( $imgfile); /*== check size 0=width, 1=height ==*/ if (($imgsize[0] > 250) || ( $imgsize[1] > 200)) { /*== temp image file -- use "tempnam()" to generate the temp file name. This is done so if multiple people access the script at once they won't ruin each other's temp file ==*/ $tmpimg = tempnam("/tmp", "MKUP"); /*== RESIZE PROCESS 1. decompress jpeg image to pnm file (a raw image type) 2. scale pnm image 3. compress pnm file to jpeg image ==*/ /*== Step 1: djpeg decompresses jpeg to pnm ==*/ system ("djpeg $imgfile >$tmpimg"); /*== Steps 2&3: scale image using pnmscale and then pipe into cjpeg to output jpeg file ==*/ system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile" ); /*== remove temp image ==*/ unlink ($tmpimg); } /*== setup final file location and name ==*/ /*== change spaces to underscores in filename ==*/ $final_filename = str_replace( " ", "_", $imgfile_name); $newfile = $uploaddir . "/$final_filename" ; /*== do extra security check to prevent malicious abuse==*/ if (is_uploaded_file ($imgfile)) { /*== move file to proper directory ==*/ if (!copy($imgfile,"$newfile" )) { /*== if an error occurs the file could not be written, read or possibly does not exist ==*/ print "Error Uploading File."; exit(); } } /*== delete the temporary uploaded file ==*/ unlink($imgfile ); print("<img src=\"$final_filename \">"); $db = new MySQL( DB_HOST, DB_USER, DB_PASS, DB_NAME ); $db->query( "UPDATE user SET picture='$final_filename' WHERE id = ".$user->id ); $q = $db->query( "SELECT * FROM user WHERE id = ".$user->id ); $user = $q->getNext( "object" ); $_SESSION['user'] = $user; } ?> </head> <body bgcolor="#FFFFFF"> <h2>Upload and Resize an Image</h2> <form action="<?php echo $_SERVER['PHP_SELF' ]; ?>" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="50000"> <p>Upload Image: <input type="file" name="imgfile"><br> <font size="1">Click browse to upload a local file</font><br> <br> <input type="submit" value="Upload Image"> </form> <?php echo doFooter( "../" ); ?> <?php /*== FUNCTIONS ==*/ function getFileExtension($str) { $i = strrpos($str,"." ); if (!$i) { return "" ; } $l = strlen($str ) - $i; $ext = substr ($str,$i+1,$l ); return $ext; } ?> Quote Link to comment Share on other sites More sharing options...
jimmyp3016 Posted February 27, 2007 Author Share Posted February 27, 2007 Here is the error I get. Prob cause this is not defined Warning: unlink(imgfile): No such file or directory in /home/content/server/html/members/upload.php on line 46 Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 27, 2007 Share Posted February 27, 2007 try $_FILES['imgfile'] Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted March 4, 2007 Share Posted March 4, 2007 I am using this same script and I am having a problem. It will upload perfect except if the image needs to be resized, if it needs resizing it seems to go through fine but when I check the image the name of the file is there but it's 0 bytes. I have tried but can only get it to work if file is exact size or smaller. Any help would be appreciated ! Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted March 4, 2007 Share Posted March 4, 2007 Any ideas why an image that needs resizing would end up at 0 bytes ? The file name is there in the directory I wish to have it, but any image that needs to be downsized ends up at 0 bytes. Really stuck on this one. Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted March 5, 2007 Share Posted March 5, 2007 Does anyone know why this would happen ? I have tried everything and still get the file name in my folder but 0 bytes, undersized photos upload fine but all that need to be resized show up as 0 bytes ????? Any help would be appreciated on this one ! Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 5, 2007 Share Posted March 5, 2007 you need to goto this link and read it properly ok? http://uk2.php.net/gd gd is a big subject but easy look and see there loads off examples ok. resize example gd This example will resample an image to half its original size. <?php // The file $filename = 'test.jpg'; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Output imagejpeg($image_p, null, 100); ?> Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted March 5, 2007 Share Posted March 5, 2007 Thanks, for the response. I have seen many who are using the above original code at the top of this post and seems to work fine for the most part. Was just curious as to why it would not work in my case and any image that is resizing is giving me 0 bytes. Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted March 8, 2007 Share Posted March 8, 2007 Anyone have an answer to this ? 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.