herghost Posted April 17, 2009 Share Posted April 17, 2009 Hi all, I have a form which very nicely creates a folder on myserver named after the userid of the user that is logged in. IE user/4 and has an upload function. The code is: <?php session_start(); include('include/database.php'); include('include/auth.php'); $userid = $_SESSION['SESS_USERID']; $sql = "SELECT * FROM user WHERE userid = $userid"; $result = mysql_query($sql) or die(mysql_error()); if ($result) { while ($row = mysql_fetch_array($result)) { $userid = $row["userid"]; if (is_dir('users/'.$userid) == FALSE) { mkdir('users/'.$userid); } } } ?><br /> <br /> <br /> Upload a Picture of your band!<br /> <br /> <form enctype="multipart/form-data" action="do/bandpicdo.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="102400" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <input value="Submit" type="submit"> Which of course then completes the action of uploading by loading file bandpicdo.php which currently contains: <?php session_start(); include('../include/auth.php'); include('../include/database.php'); ?> I am stumped! What I want this to do is : a) Upload the image submitted in the form b) resize the image to 120x120 c) rename the image to 'mainpic.jpg' 4) save to the users directory Ehm, Help! Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/ Share on other sites More sharing options...
runnerjp Posted April 17, 2009 Share Posted April 17, 2009 [code<form action="<?php echo $_server['php-self']; ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm"> <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /> <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button> </form> <?php if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; // Adding extension verification $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) die("Extension: $file_ext is not allowed!"); // End extension verification $imagename = "mainpic.jpg"; $source = $_FILES['new_image']['tmp_name']; $target = "images/$id.$file_ext"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "what ever the user path is" . $imagepath; //This is the new file you saving $file = "what ever the user path is" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; // Adding proportionate image resizing, with max values /* $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; */ $max_w = 120; $max_h = 120; if($width <= $max_w && $height <= $max_h){ // if it fits $modheight = $height; $modwidth = $width; }else{ // Then resize $diff = ($width > $height) ? ($width/$max_w) : ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800) $modheight = $height / $diff; $modwidth = $width / $diff; } // End $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $sql = "UPDATE `where ever u stored the pic` SET image = 'mainpic.jpg; mysql_query($sql) or die(mysql_error()); echo "<center> <table width=\"50%\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td width=\"4%\"><img src='images/thumbs/".$imagepath."'></td> </tr> </table> </center>"; } } Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812695 Share on other sites More sharing options...
herghost Posted April 17, 2009 Author Share Posted April 17, 2009 runnerjp, I can not thank you enough, I never expected anyone to be kind enough to write it for me! I will test it soon and come back to you if there is anything I dont understand, and thank you for the comments in the script, I am trying to learn by doing and that probably saved me hours of trying to work out what each peice of script is doing. Again, Many many thanks Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812701 Share on other sites More sharing options...
herghost Posted April 17, 2009 Author Share Posted April 17, 2009 Ok mate, Stumped again: <?php $userid = $_SESSION['SESS_USERID']; if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; // Adding extension verification $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) die("Extension: $file_ext is not allowed!"); // End extension verification $imagename = "mainpic.jpg"; $source = $_FILES['new_image']['tmp_name']; $target = "C:/wamp/www/fanjunky/temp/$userid/$imagename"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving $file = "C:/wamp/www/fanjunky/temp/$userid/$imagepath"; //This is the original file list($width, $height) = getimagesize($file); // Adding proportionate image resizing, with max values // $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; $max_w = 120; $max_h = 120; if($width <= $max_w && $height <= $max_h){ // if it fits $modheight = $height; $modwidth = $width; }else{ // Then resize $diff = ($width > $height) ? ($width/$max_w) : ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800) $modheight = $height / $diff; $modwidth = $width / $diff; } // End $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; } } ?> This is giving errors: Warning: move_uploaded_file(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 28 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpF702.tmp' to 'C:/wamp/www/fanjunky/temp/6/mainpic.jpg' in C:\wamp\www\fanjunky\do\bandpicdo.php on line 28 Warning: getimagesize(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 34 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\fanjunky\do\bandpicdo.php on line 55 Warning: imagecreatefromjpeg(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 56 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 57 Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 58 Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812767 Share on other sites More sharing options...
runnerjp Posted April 17, 2009 Share Posted April 17, 2009 <?php $userid = $_SESSION['SESS_USERID']; if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; // Adding extension verification $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) die("Extension: $file_ext is not allowed!"); // End extension verification $imagename = "mainpic.jpg"; $source = $_FILES['new_image']['tmp_name']; $target = "C:/wamp/www/fanjunky/temp/$userid"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving $file = "C:/wamp/www/fanjunky/temp/$userid/$imagepath"; //This is the original file list($width, $height) = getimagesize($file); // Adding proportionate image resizing, with max values // $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; $max_w = 120; $max_h = 120; if($width <= $max_w && $height <= $max_h){ // if it fits $modheight = $height; $modwidth = $width; }else{ // Then resize $diff = ($width > $height) ? ($width/$max_w) : ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800) $modheight = $height / $diff; $modwidth = $width / $diff; } // End $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; } } ?> try that Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812769 Share on other sites More sharing options...
herghost Posted April 17, 2009 Author Share Posted April 17, 2009 Warning: getimagesize(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 34 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\fanjunky\do\bandpicdo.php on line 55 Warning: imagecreatefromjpeg(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 56 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 57 Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 58 Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812772 Share on other sites More sharing options...
herghost Posted April 17, 2009 Author Share Posted April 17, 2009 Using this: <?php $userid = $_SESSION['SESS_USERID']; if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $file_name = $_FILES['new_image']['name']; $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; // Adding extension verification $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|"; if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) die("Extension: $file_ext is not allowed!"); // End extension verification $imagename = "mainpic.jpg"; $source = $_FILES['new_image']['tmp_name']; $target = "C:/wamp/www/fanjunky/temp/$userid"; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving $file = "C:/wamp/www/fanjunky/temp/$userid"; //This is the original file list($width, $height) = getimagesize($file); // Adding proportionate image resizing, with max values // $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; $max_w = 120; $max_h = 120; if($width <= $max_w && $height <= $max_h){ // if it fits $modheight = $height; $modwidth = $width; }else{ // Then resize $diff = ($width > $height) ? ($width/$max_w) : ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800) $modheight = $height / $diff; $modwidth = $width / $diff; } // End $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; } } ?> It nows save to the folder without an error However it has cropped the width to 120, but cropped the height to 14 pixels? Any thoughts on that? You are, by the way, my hero! Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812780 Share on other sites More sharing options...
runnerjp Posted April 18, 2009 Share Posted April 18, 2009 a hero!! i like the sound of that haha,,, well people have given up the time to do code for me so i will do same fo others ermm can i see the script in action so i can see resize for myself (im off to bed so will look in morning) Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-812826 Share on other sites More sharing options...
herghost Posted April 18, 2009 Author Share Posted April 18, 2009 sure www.fanjunky.com user test pass test Thanks once again Link to comment https://forums.phpfreaks.com/topic/154553-uploading-image-to-server-and-perform-actions-help/#findComment-813068 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.