TeddyKiller Posted March 21, 2010 Share Posted March 21, 2010 I have a resize image script, and an upload script. When the image gets uploaded, it should resize to a certain size. I'm not sure how I'd combine the two scripts together. Upload script if($_POST['upload']){ if ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "IMAGE/JPEG") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "IMAGE/PJPEG") ) && ($_FILES["file"]["size"] < 100000)) { if ($_FILES["file"]["error"] > 0) { $error = "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("profile/photos/".$_FILES["file"]["name"]."-" . time())) { $error = $_FILES["file"]["name"] . " already exists. <br />"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "profile/photos/".time()."-".$_FILES["file"]["name"]); //echo "Stored in: "."profile/photos/".$_FILES["file"]["name"]."<br />"; $filename = "profile/photos/".time()."-".$_FILES["file"]["name"]; $query = mysql_query("UPDATE users SET avatar = '$filename' WHERE id = '".$_SESSION['uid']."'") or trigger_error("Query failed: ".mysql_error()); } } } else { echo "Invalid file<br />"; } } echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\"> <label for=\"file\">Filename:</label> <input type=\"file\" name=\"file\" id=\"file\" /> <br /> <input type=\"submit\" name=\"upload\" value=\"Submit\" /> </form><br /><br />"; Resize script <?php $pic = //Not sure about this if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; list($width, $height) = getimagesize($pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); ?> There is an idea of after the file is uploaded, to pass the file name onto the resize script. Then it'll have to overwrite. If this is an option, how would I get it to overwrite? Thanks! Any help? Thanks. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/ Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 You would do so much better if you could get a function script that has something like resizeImage(); not that full script because it will automatically execute which will cause a lot of hassle. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029393 Share on other sites More sharing options...
TeddyKiller Posted March 21, 2010 Author Share Posted March 21, 2010 You mean a whole new script, or just turning that script into a function. I can turn it into a function. I can use the variable "$filename" and use that in the function, and set the filename as the location to be saved. Though would that overwrite it because it needs to overwrite. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029398 Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 Just make a function script out of your resize one and i'd be happy to combine them both together for you. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029400 Share on other sites More sharing options...
TeddyKiller Posted March 21, 2010 Author Share Posted March 21, 2010 <?php function imageResize(){ $pic = //Not sure about this if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; list($width, $height) = getimagesize($pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height); header('Content-type: image/jpeg'); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); return true; } ?> Best I can do. Not an expert on functions. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029402 Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 Try this: <?php function imageResize($pic){ if (!isset($max_width)) $max_width = 90; if (!isset($max_height)) $max_height = 90; list($width, $height) = getimagesize($pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefromjpeg($pic); $dst = imagecreatetruecolor($tn_width, $tn_height); imagecopyresampled($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height); imagejpeg($dst,NULL,100); imagedestroy($src); imagedestroy($dst); return true; } if($_POST['upload']){ if ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "IMAGE/JPEG") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "IMAGE/PJPEG") ) && ($_FILES["file"]["size"] < 100000)) { if ($_FILES["file"]["error"] > 0) { $error = "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("profile/photos/".$_FILES["file"]["name"]."-" . time())) { $error = $_FILES["file"]["name"] . " already exists. <br />"; } else { imageResize($_FILES["file"]); move_uploaded_file($_FILES["file"]["tmp_name"], "profile/photos/".time()."-".$_FILES["file"]["name"]); //echo "Stored in: "."profile/photos/".$_FILES["file"]["name"]."<br />"; $filename = "profile/photos/".time()."-".$_FILES["file"]["name"]; $query = mysql_query("UPDATE users SET avatar = '$filename' WHERE id = '".$_SESSION['uid']."'") or trigger_error("Query failed: ".mysql_error()); } } } else { echo "Invalid file<br />"; } } echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\"> <label for=\"file\">Filename:</label> <input type=\"file\" name=\"file\" id=\"file\" /> <br /> <input type=\"submit\" name=\"upload\" value=\"Submit\" /> </form><br /><br />"; ?> Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029404 Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 Have a look at this: This script doesn't actually Resize it but it makes the image height and width smaller, but when you actually go to the destination of the picture it will be bigger. function imageResize($width, $height, $target){ if ($width > $height){ $percentage = ($target / $width); }else{ $percentage = ($target / $height); } $width = round($width * $percentage); $height = round($height * $percentage); return "width=\"$width\" height=\"$height\""; } Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029406 Share on other sites More sharing options...
TeddyKiller Posted March 21, 2010 Author Share Posted March 21, 2010 I moved the function into a file called functions.php Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /home/jeanie/public_html/inc/functions.php on line 6 Warning: Division by zero in /home/jeanie/public_html/inc/functions.php on line 8 Warning: Division by zero in /home/jeanie/public_html/inc/functions.php on line 9 Warning: imagecreatefromjpeg(Array) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/jeanie/public_html/inc/functions.php on line 23 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/jeanie/public_html/inc/functions.php on line 24 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/jeanie/public_html/inc/functions.php on line 25 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/jeanie/public_html/inc/functions.php on line 26 Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/jeanie/public_html/inc/functions.php on line 27 Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/jeanie/public_html/inc/functions.php on line 28 It's basically not recieving the picture? Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029408 Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 I'll let someone else do it for you since im busy with someone elses website. Link to comment https://forums.phpfreaks.com/topic/195972-resizing-an-image-upon-upload/#findComment-1029409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.