Hi!
I got this image uploading script that will upload the user's choosen picture, and save it in the user's folder with the users nickname.
Now, when i got the picture uploaded i want to resize it to 3 exact sizes (3 versions of the image).
240x250 and 2 other sizes.
I don't really know where to start with this, so i thought i should ask here. I have been looking at some threads here, but i can't find some good explained ones.
My image uploading script looks like this:
<?php
if ($_SESSION['username'])
{
include 'db_connect.php';
define ("MAX_SIZE","2000");
$username = $_SESSION['username'];
//Checks the file extensions
function getExtension($str){
$i = strrpos($str,".");
if (!$i) { return ""; }
$I = strlen($str) - $i;
$ext = substr($str,$i+1,$I);
return $ext;
}
//If error, value change to 1, file will not be uploaded
$errors=0;
if (isset($_POST['Submit']))
{
//Reads the image name
$image=$_FILES['image']['name'];
//If empty
if ($image == "") {
echo 'Velg et bilde!';
$errors = 1;
}
//If not empty
if ($image)
{
//Get orgiginal name from client machine
$filename = stripslashes($_FILES['image']['name']);
//Get the extension
$extension = getExtension($filename);
$extension = strtolower($extension);
//If file extension not known, error. If not, continue
if (($extension != "jpg") && ($extension != "jpeg"))
{
//Error message
echo 'Bare JPG og JPEG er tilatte filtyper!';
$errors = 1;
}
else
{
if ($extension == "jpg") { $extension = "jpeg"; }
//Get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//Compare the size with the maximum size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo 'Bildet er for stort! 2M er maks!';
$errors=1;
}
//Giving the uploaded files new name
$imageName = "$username.$extension";
$newName = "users/$username/$imageName";
}}}
if(!$errors) {
if (!move_uploaded_file($_FILES['image']['tmp_name'], $newName))
$errors=1;
}
//If no errors do this
if (isset($_POST['Submit']) && !$errors)
{
echo 'Bildet er lastet opp og blir byttet!<br>Vær ops på at vi sjekker bildet senere.<br>Vis vi finner det utilpassende vil vi slette det.';
$sql = "UPDATE members SET user_pic='http://www.something.net/$newName' WHERE username='$username'";
$_SESSION['user_pic'] = $newName;
$result = mysql_query($sql);
mysql_close($conn);
}
//The form
echo '
<div class="box_square">
<form id="changeuserpic" action="" method="POST" enctype="multipart/form-data">
<img src="resources/byttbilde.png" border="0" width="173" height="27" style="margin-left:-8px;" /><br><br>
<input class="f_input box_round" type="file" name="image" /><br><br>
<input class="box_round f_submit" type="submit" name="Submit" value="Bytt bilde" />
</form>
</div>';
}
else
echo '<div class="box_square"><div class="form_error">Du må være logget inn for å se denne siden!<br><br>Logg inn <a href="">her</a>.</div></div>';
die ();
?>
I was hoping someone had the time to explain the process and stuff.
Thx in advance!