Guest Posted November 12, 2006 Share Posted November 12, 2006 [code]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 = "."; /*== 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\">"); /*== DO WHATEVER ELSE YOU WANT SUCH AS INSERT DATA INTO A DATABASE ==*/}?></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></body></html><?php /*== FUNCTIONS ==*/ function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }?>[/code]this is code i found on the internet to upload and resize an imageit says this /* 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 */but i cant figure out how to make it all work correctly can some one please help. Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/ Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 That code uses really old style coding logic and should not really be used today. PHP has better file function handlers than when that code was written. I recommend you rewrite it or find better code that uses the newer functions that are made just for file uploads! Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123386 Share on other sites More sharing options...
Guest Posted November 12, 2006 Share Posted November 12, 2006 Do you know where i can find better code that resizes it both with and height and keeps ratio? Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123387 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 Give me 20 minutes, I'll rewrite for you! Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123388 Share on other sites More sharing options...
Guest Posted November 12, 2006 Share Posted November 12, 2006 Wow thank you so much. Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123390 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 Sorry went for coffee, took a little longer than I thought I would...see the attached file, it should be self explained, but if you have questions, just ask![attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123420 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 I uploaded the wrong file before, please download the new one...I made a change to the file name, when creating the thumbnail, I forgot to add the original name back to the thumbnail, it's fixed now! Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123423 Share on other sites More sharing options...
Guest Posted November 12, 2006 Share Posted November 12, 2006 Thank you so much for your help its working wonderfully thank you so much. I would like to give you some money in gratitude you saved me alott of time do you have a paypal address? Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123429 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 Thanks for the offer, but I don't have paypal, if you want to contribute to this forum I know they could use it, if you need help with anything else just ask...c, ya...Sonia Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123433 Share on other sites More sharing options...
Guest Posted November 12, 2006 Share Posted November 12, 2006 Ok thank you i will contribute to the forum though. Thank you very much. Link to comment https://forums.phpfreaks.com/topic/26988-image-upload-and-resize/#findComment-123434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.