Hi

I have found this script to resize the picture, and what I am trying to do is resize the picture before it is uploaded and stored on the server and db. Below are the two scripts, the first one uploads the files and stores them, but the second one I got from here http://uk2.php.net/imagecopyresampled to resize the image before it is uploaded. I have tried for a while now to configure the second script to work with the first with no joy. Anyone have any ideas please?

Thanks as always,

Phil

// 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 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'];


	// 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.

and the second one is:

// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);

    Here's a function I created a while ago for someone in a similar situation.

    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 = 'images/webimages/internal'.$photo_name;
    		$photo_dir='/username/public_html/'.$imgPath;
    		$photo_url='http://www.mydomain.com/'.$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;
    	}
    }
    }

    That will properly make an image smaller / larger to the specified size. The default (obviously) is either 250 pixels wide or 250 pixels high. Hope that helps.

    The trick to this would be to combine the scripts so that you first manipulate the image as you want, and then you run your mySQL stuff.

      bpat

      Ok thanks for that, I will give it a try. Be back in a few day to say if it worked, thanks very much.

      Phil

        4 days later

        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.
          Write a Reply...