Forum Moderators: coopster
<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://w3.org/TR//xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload an image</title>
<style type="text/css" title="text/css" media="all">
.error{
font-weight: bold;
color:#c00; }
</style>
</head>
<body>
<form enctype="multipart/form-data" action="upload_image.php" method="post">
<fieldset><legend>Select an image file</legend>
<p><b>File:</b> <input type="file" name="upload" /></p>
</fieldset>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php
//Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//check for an uploaded file
if (isset($_FILES['upload']))
{
//validate the type, jpg or png
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed))
{
//move the file over
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}"))
{
echo '<p><em>The file has been uploaded</em></p>';
}//end of move if
}
else
{
echo '<p><em>Uploaded file must be an image. File not uploaded.</em></p>';
}
}//end of isset if
//check for error
if ($_FILES['upload']['error'] > 0)
{
echo '<p class="error">The file could not be uploaded because:<strong>';
//print a message based upon the error
switch ($_FILES['upload']['error'])
{
case 1:
print 'The file exceeds the max_upload_size in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the html form';
break;
case 3:
print 'The file has only been partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
case 6:
print 'No temporary folder was available';
break;
case 7:
print 'Unable to write to the hard disk';
break;
case 8:
print 'File upload stopped';
break;
default:
print'A system error occured';
break;
}//end of switch
print '<?strong></p>';
}//end of error if
//delete the file if it still exists
if (file_exists ($_FILES['upload']['tmp_name']) && is_file ($_FILES['upload']['tmp_name']))
{
unlink ($_FILES['upload']['tmp_name']);
}
}
?>
<?php
// File and new size
$filename = 'rotate2.jpg';//1.HOW DO I MAKE THIS TAKE THE IMAGE THAT HAS BEEN UPLOADED THROUGH THE FORM?
$percent = .55;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb); I also want to resize the images before I upload them to dir and have this code for resizing