StefanRSA Posted October 11, 2009 Share Posted October 11, 2009 Hi, I have written a script to upload images to a server... I am not to sure what I should do next to re-size the uploaded image.... Please help me with my code? <?php set_time_limit(60); $err = false; if (isset($_FILES['image'])) { $ftmp = $_FILES['image']['tmp_name']; $oname = $_FILES['image']['name']; $div_id = $_POST['div_id']; $type = @explode('/', $_FILES['image']['type']); $type = isset($type[1]) ? $type[1] : ''; $type = ($type != 'pjpeg') ? $type : 'jpeg'; $img_types = array('jpg', 'jpeg', 'gif', 'png'); if (in_array($type, $img_types)) { $file_temp_name = substr(md5(time() . $div_id), 0, 14) . 'n' . '.' . $type; /////////////////////// /////////////////////// $fname = "photos/temp/" . $file_temp_name; $afname = "photos/temp/" . $file_temp_name; if (move_uploaded_file($ftmp, $fname)){ ?> <html> <head> <script language="javascript"> window.parent.setUploadedImage('<?=$afname?>', '<?=$file_temp_name?>', '<?=$div_id?>'); </script> </head> </html> <?php exit(); } } else { $err = true; } } ?> <html> <head> <style type="text/css"> body { margin: 0px; padding: 0px; background-color: #FFF; color:#000; } </style> </head> <body> <?php if ($err) { ?> <script language="javascript"> window.parent.uploadError('<?=$div_id?>', '<?=$oname?>'); </script> <?php } ?> <div> <form name="iform" action="" method="post" enctype="multipart/form-data"> <? $hostname = 'localhost'; $username = 'root'; $password = ''; $database = 'photo'; $link = mysql_connect($hostname, $username, $password); mysql_select_db($database, $link); $result = mysql_query("SELECT * FROM ad_image"); $num_rows = mysql_num_rows($result); if ($num_rows <=3){ ?> <input id="file" type="file" name="image" onChange="window.parent.upload(this);" /><br> <span style="font-size:11px; color:#666666;">only gif, png, jpg files.</span> <input type="hidden" value="" name="div_id" /> <? } echo $num_rows; ?> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/ Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 google really is your friend http://www.lmgtfy.com/?q=php%2Btutorial%2Bresize%2Bimage Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934903 Share on other sites More sharing options...
StefanRSA Posted October 11, 2009 Author Share Posted October 11, 2009 I am sure this is why phpfreaks.com is still working! I have a problem with MY CODE and google cannot help me... I am not sure where to implement the image resize in the script I have written!!!! Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934909 Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 Then perhaps you should have mentioned that instead of getting angry. We're not mind readers. You said you weren't sure what you needed to do next to resize it, not "Where do I need to add my image resize code" Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934911 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 There are literally a million image resize tutorials/scripts out there. Do you seriously think you're the first person to ever ask about resizing images? We aren't here to sift through your code, write your code for you, or re-invent the wheel. Google for image resize scripts/tutorials. Pick any one of the millions out there. If you have a specific problem, feel free to post it here. Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934914 Share on other sites More sharing options...
StefanRSA Posted October 11, 2009 Author Share Posted October 11, 2009 Then I am really sorry... But where exactly in my existing code should I put one of the million examples to make sure the image that is saved on the server gets re-sized... Please help me Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934937 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 i'm sorry, but if you really wrote the script in the OP then you should know the answer to that... Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934942 Share on other sites More sharing options...
StefanRSA Posted October 11, 2009 Author Share Posted October 11, 2009 I think you guys are really rude... I am trying my best to do this all myself and was under the impression that if I need help, I will get it here... I did not ask for anybody to re-write my code or do it for me... I am just not to sure where in my script do I have to implement the re-size section... Will try another forum..... Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934975 Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 I'm amazed you think I'm rude since I answered your orginal post as best I could then gave you a reason for why I put my answer and told you you need to be more specific. That's not rude, it's just what needs to be done Anyway, regarding your question, you need it just after here if (move_uploaded_file($ftmp, $fname)){ Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934979 Share on other sites More sharing options...
StefanRSA Posted October 11, 2009 Author Share Posted October 11, 2009 Thanks Jay... I am sorry for being so upfront... I am so frustrated with the code as I just don't get it to do what I want it to do. I am new to php... Anyway... I was actually thinking it should be before the line you pointed out! I will try again. May I print my code on here again if I don't get it right? Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-934993 Share on other sites More sharing options...
Dorky Posted October 11, 2009 Share Posted October 11, 2009 Thanks Jay... I am sorry for being so upfront... I am so frustrated with the code as I just don't get it to do what I want it to do. I am new to php... Anyway... I was actually thinking it should be before the line you pointed out! I will try again. May I print my code on here again if I don't get it right? ok. this script could and can be consolidated but i wrote it this way because it is easier to edit individual parts for the desired application and transplanting script is less time consuming then writing one for each site. this particular application is used for an image gallery with thumbnail views. after you have taken the time to pick apart and research the parts you do not understand you will by then understand how to integrate this into your script. if you try to cut and paste this it will not work. you need to edit the required conditions, file path, desired sizes, and header pref. but if you can handles that then this script should help you. it requires the gd lib on your server and will resize without squishing or squashing the image(keeps proportions). the or statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext, that is even if they knew how to. if (isset($_SESSION['user'])) { if (isset($_POST['submitimage'])) { if (isset ($_FILES['new_image'])) { if (!empty($_FILES['new_image']['name'])) { $imagename = $_FILES['new_image']['name']; $charref = substr($imagename, 0, strrpos($imagename, '.')); if(ctype_alnum($charref)) { if (($pos = strrpos($imagename, ".")) === FALSE) { echo "<p class=\"p7\">Fail</p><br />"; exit; } else { $extension = substr($imagename, $pos + 1); } if ( $extension == "jpg" || $extension == "JPG" || $extension == "gif" || $extension == "png" ) { $source = $_FILES['new_image']['tmp_name']; $target = "images/gallery/$imagename"; move_uploaded_file($source, $target); $file = "images/gallery/$imagename"; $save = "images/gallery/$imagename"; list($width, $height) = getimagesize($file) ; if ($width > "300" ) { $modwidth = 300; } else { $modwidth = $width ; } $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; if( $extension == "jpg" || $extension == "JPG" ) { $image = imagecreatefromjpeg ($file); } if( $extension == "gif" ) { $image = imagecreatefromgif ($file); } if( $extension == "png" ) { $image = imagecreatefrompng ($file); } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; if( $extension == "jpg" || $extension == "JPG" ) { imagejpeg($tn, $save, 100) ; } if( $extension == "gif" ) { imagegif($tn, $save, 100) ; } if( $extension == "png" ) { imagepng($tn, $save, 9) ; } $save = "images/gallery/thumbs/$imagename"; list($width, $height) = getimagesize($file) ; if ($width > "100" ) { $modwidth = 100; } else { $modwidth = $width ; } $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; if( $extension == "jpg" || $extension == "JPG" ) { $image = imagecreatefromjpeg ($file); } if( $extension == "gif" ) { $image = imagecreatefromgif ($file); } if( $extension == "png" ) { $image = imagecreatefrompng ($file); } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; if( $extension == "jpg" || $extension == "JPG" ) { imagejpeg($tn, $save, 100) ; } if( $extension == "gif" ) { imagegif($tn, $save, 100) ; } if( $extension == "png" ) { imagepng($tn, $save, 9) ; } header("Location: http://#"); exit; } else { echo "Invalid or empty File <br>Valid file types .jpg .gif .png<br />"; exit; } } else { echo "Image name may have alphanumeric characters only and no spaces $charref"; exit; } } else { echo "You did not select a file to upload<br />"; exit; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-935074 Share on other sites More sharing options...
MadTechie Posted October 11, 2009 Share Posted October 11, 2009 statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext, Coughs strtolower Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-935140 Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext, Coughs strtolower OMG WUT SORCERY IS THIS???!?!?!?ONE11!!?!?! Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-935143 Share on other sites More sharing options...
StefanRSA Posted October 12, 2009 Author Share Posted October 12, 2009 Thanks Everybody! I am almost done with my script and think its a good idea to paste the whole script as I feel this is a general problem with image upload without refresh on a form for newbies like me. For the php guru's... Thanks for the help. Comment would really be appreciated on my complete script, that I will paste a bit later today. Thanks! Stefan Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-935234 Share on other sites More sharing options...
Dorky Posted October 12, 2009 Share Posted October 12, 2009 thank you both. i will put this to use right now lol. statement for jpg and JPG is because digital cameras use the JPG instead of jpg and its just easier to add this then to require clients or users to rename the file ext, Coughs strtolower OMG WUT SORCERY IS THIS???!?!?!?ONE11!!?!?! Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-935244 Share on other sites More sharing options...
StefanRSA Posted October 14, 2009 Author Share Posted October 14, 2009 I am done with the script and it works 100%. This code is built out of lost of examples found on google.com by a newbie(Me... ! The only problem I do have, at this stage is that when an image is marked for deletion, I need to click on the submit button do delete the image. Can anybody that have time to work through my code, tell me what I should do, to delete the image in the background without the need to hit submit? Any comments will also be appreciated to streamline my code and even to point out errors... The full script is zipped and attached. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/177318-image-upload-now-i-want-to-resize/#findComment-936627 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.