Hi,
I posted this about a week ago and Bpat responded to give me some help, since then I haven't seen him on the forum and so I am asking again for some help with my problem - I hope you don't mind.
Here is a script that uploads the image and stores it in the db, but the resizing part doesn't work, the image just displays at it's original size. Bpat said that I would need to configure the resizing part to get it to work with the other part but this is where things go wrong, you see I don't know enough php to figure our how to do this. Can anyone help with debugging the script so it will resize and upload please.
Thanks as always,
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.
//Start of Bpat's script to resize
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;
}
}
}
// end of bpat's script to resize
// 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.