Hi
I have tried to play around with the script but with no luck. I have my website on a hosting package - does that make any difference to the script you gave me? Also on the variables, do they have to be there as the images is already being inserted to where I want it to be. I am sure something small here is wrong, but like everything you just need to know what.
Any Ideas? The script as it stands works as far as it will upload the the image insert it into the db but just won't resize - the mind boggles!!!!!!
Thanks,
Phil
Here is the script so far:
// This page allows users to upload files to the server.
// Set the page title and include the HTML header.
$page_title = 'Upload a File';
if (isset($_POST['submit'])) { // Handle the form.
require_once ('../mysql_connect.php'); // Connect to the database.
function manipImage($width=250)
{
// Get the temporary file name that php gives us.
$tempname = $_FILES['photo']['tmp_name'];
// Check to make sure it's uploaded
if(is_uploaded_file($tempname))
{
// Original dimensions
$orig_dimensions = getimagesize($tempname);
// Check to make sure we get a valid image size
if($orig_dimensions === FALSE || $orig_dimensions[2] != 2)
die("Image upload failed!!");
// Define the original dimensions (width & height)
$orig_width = $orig_dimensions[0];
$orig_height = $orig_dimensions[1];
// Create a new image from the temp to work off of
$orig_image = imagecreatefromjpeg($tempname);
// If it's tall, we need to do one more step
$image_is_tall = ($orig_height>=$orig_width);
$swidth = $width;
// Define the proportionate height
$sheight = ceil($orig_height*$swidth/$orig_width);
// If the image is tall, not wide, well rotate the image dimensions
// This is so that it stays proportionate if it's a tall image
if($image_is_tall)
{
$t = $swidth;
$swidth = $sheight;
$sheight = $t;
}
// Create a new image based on the new widths & height
$new_image = imagecreatetruecolor($swidth, $sheight);
// Now we'll copy the old image, to the new image and it will be resized
if(imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $swidth, $sheight, $orig_width, $orig_height) === FALSE)
return -1;
else
{
// Set up some variables to use
$photo_name = $_POST['photosID'].'.jpg';
$imgPath = 'family_pics/images/'.$photo_name;
$photo_dir='/philweb/public_html/'.$imgPath;
$photo_url='http://www.mysite.co.uk/'.$imgPath;
$newer_image = $photo_dir.$photo_name;
// Create a new jpeg (*.jpg) from the resized image with 100% quality
if(@imagejpeg($new_image, $newer_image, 100) === FALSE)
return false;
else
return true;
}
}
}
// Function for escaping and trimming form data.
function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.
// First name
if (!empty($_POST['first_name'])) {
$fn = escape_data($_POST['first_name']);
} else {
$fn = '';
}
// Check for a description (not required).
if (!empty($_POST['description'])) {
$d = escape_data($_POST['description']);
} else {
$d = '';
}
$picture_url = "family_pics/images/".$_FILES['upload']['name'];
// Add the record to the database.
$query = "INSERT INTO family (first_name, picture_url, description, registration_date) VALUES ('$fn', '$picture_url','$d', NOW())";
$result = @mysql_query ($query);
if ($result) {
// Create the file name.
$extension = explode ('.', $_FILES['upload']['name']);
$uid = mysql_insert_id(); // Upload ID
$filename = $_FILES['upload']['name'];
// The file
$filename = $_FILES['upload']['name'];
// Move the file over.
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../family_pics/images/$filename")) {
echo '<p>The file has been uploaded!</p>';
} else {
echo '<p><font color="red">The file size is too big!.</font></p>';
// Remove the record from the database.
$query = "DELETE FROM family WHERE upload_id = $uid";
$result = @mysql_query ($query);
}
} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
}
@mysql_close(); // Close the database connection.
} // End of the main Submit conditional.