<?php
if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000)
{
$target_folder = 'images/'; // This is the folder to which the images will be saved
$upload_image = $target_folder.basename($_FILES['user_image']['name']); // The new file location
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image))
{
$newname =$_FILES['user_image']['name']; // Get the supplied image name and sanitize it
$thumbnail = $target_folder."_thumbnail".$newname; // Set the thumbnail name
$actual = $target_folder.$newname; // Set the actual image name
// Get new sizes
list($width, $height) = getimagesize($upload_image);
$newwidth = 150; // This can be a set value or a percentage of original size ($width)
$newheight = 150; // This can be a set value or a percentage of original size ($height)
// Load the images
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($upload_image);
// Resize the $thumb image.
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Save the new file to the location specified by $thumbnail
imagejpeg($thumb, $thumbnail, 100);
// Rename the original
//note, this only needs to be done if a new filename is supplied in the form (As it is in this example).
// note 2, if you don't mind starting the mysql connection earlier than the file move in order to sanitize $_POST['image_name'], $upload_image can use
// $newname from the beginning instead of $_FILES['user_image']['name'] in line 12.
// Now we will try and insert the details into the database with the new actual and thumbnail files, and with the new caption...
// We carry out the mysql query as part of an IF statement to evaluate if it worked properly.
// ...it did work properly. Tell the user and do anything else you particularly feel like doing.
?>
<p>Image Successfully uploaded!<br />Actual image: <?=$actual;?><br />Thumbnail image: <?=$thumbnail;?><br />Caption: <?=$caption;?></p>
<?php
}
else
{
// There was a problem with the file and it wasn't moved to the server.
?>
<p>Error processing file. It may be too large.</p>
<?php
}
}
else
{
// The file was the wrong format or size
?>
<p>Error processing file. It is the wrong format (.jpeg only) or too big.</p>
<?php
}
?>
<form enctype="multipart/form-data" action="" method="POST">
What would you like to call the image?: <input type="text" name="image_name" id="image_name" />
<br />
What caption should the image have?: <input type="text" name="image_caption" id="image_caption" />
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
Pick an Image from your computer: <input name="user_image" type="file" />
<br />
<input type="submit" value="Upload" />
</form>
if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000)
{
$target_folder = 'images/'; // This is the folder to which the images will be saved
$upload_image = $target_folder.basename($_FILES['user_image']['name']); // The new file location
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image))
{
$newname =$_FILES['user_image']['name']; // Get the supplied image name and sanitize it
$thumbnail = $target_folder."_thumbnail".$newname; // Set the thumbnail name
$actual = $target_folder.$newname; // Set the actual image name
// Get new sizes
list($width, $height) = getimagesize($upload_image);
$newwidth = 150; // This can be a set value or a percentage of original size ($width)
$newheight = 150; // This can be a set value or a percentage of original size ($height)
// Load the images
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($upload_image);
// Resize the $thumb image.
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Save the new file to the location specified by $thumbnail
imagejpeg($thumb, $thumbnail, 100);
// Rename the original
//note, this only needs to be done if a new filename is supplied in the form (As it is in this example).
// note 2, if you don't mind starting the mysql connection earlier than the file move in order to sanitize $_POST['image_name'], $upload_image can use
// $newname from the beginning instead of $_FILES['user_image']['name'] in line 12.
// Now we will try and insert the details into the database with the new actual and thumbnail files, and with the new caption...
// We carry out the mysql query as part of an IF statement to evaluate if it worked properly.
// ...it did work properly. Tell the user and do anything else you particularly feel like doing.
?>
<p>Image Successfully uploaded!<br />Actual image: <?=$actual;?><br />Thumbnail image: <?=$thumbnail;?><br />Caption: <?=$caption;?></p>
<?php
}
else
{
// There was a problem with the file and it wasn't moved to the server.
?>
<p>Error processing file. It may be too large.</p>
<?php
}
}
else
{
// The file was the wrong format or size
?>
<p>Error processing file. It is the wrong format (.jpeg only) or too big.</p>
<?php
}
?>
<form enctype="multipart/form-data" action="" method="POST">
What would you like to call the image?: <input type="text" name="image_name" id="image_name" />
<br />
What caption should the image have?: <input type="text" name="image_caption" id="image_caption" />
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
Pick an Image from your computer: <input name="user_image" type="file" />
<br />
<input type="submit" value="Upload" />
</form>
No comments:
Post a Comment
Thank you for your Comment....