Jump to content

How to resize uploaded image size from original to a default size?


$php_mysql$

Recommended Posts

could someone help in how to resize a uploaded image size, say if someone is uploading a 1mb photo to my server, i wish for it to become 100kb and also resize its width and height?

 

here is my code

 

//////////////////////////////////////uploader
else if($action=="uploader")
{



echo "Upload your picture and copy the link <br/>after uploading to user it at gallery.<br/><br/>";

echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"index.php?action=uploaded&sid=$sid\">";
echo "Choose Picture: <br />";
echo "<input name=\"uploaded\" type=\"file\" /><br /><br />";
echo "<input type=\"submit\" value=\"Upload\" />";
echo "</form><br/>";


echo "<p align=\"center\">";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}


//////////////////////////////////////uploader
else if($action=="uploaded")
{



$blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4");
foreach ($blacklist as $item) {
if(preg_match("/$item\$/i", $_FILES['uploaded']['name'])) {
echo "<p align=\"center\">";
echo "Oops sorry we do not allow those files.<br/>";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
exit;
}
}

$target = "../images/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if (file_exists("../images/" . $_FILES["uploaded"]["name"]))
      {
echo "<p align=\"center\">";
      echo $_FILES["file"]["name"] . "Oops file name already exists<br/> kindly rename your picture and upload again. <br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";   
}else{

//This is our size condition
if ($uploaded_size > 25600){

echo "Your file is too large. We have a 25kb limit.<br/>";
$ok=0;
}

$types = array('image/jpeg', 'image/gif', 'image/png');

if (in_array($_FILES['uploaded']['type'], $types)) {
// file is okay continue
} else {
$ok=0;
}


//Here we check that $ok was not set to 0 by an error
if ($ok==0){
echo "<p align=\"center\">";
Echo "Sorry your file was not uploaded.<br/> It may be the wrong filetype. <br/>We only allow JPG, GIF, and PNG filetypes.<br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}

//If everything is ok we try to upload it
else{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "<p align=\"center\">";

echo "The file ". basename( $_FILES['uploadedfile']['name']). " Picture uploaded successfully.<br/><br/><b>$target <br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}
else{
echo "<p align=\"center\">";

echo "Sorry, there was a problem uploading your file.<br/>";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}
}
}
}

Link to comment
Share on other sites

1. take the function I supplied you above and place it near the top of your script

 

2. at the point where you have successfully uploaded the image, implement the function adjusting the variables explained on the page I linked above.

 

3. In your post you talk about 1mb and 100k, yet your code only allows a 25k upload; which do you really want?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.