Page 1 of 1

image resizing

Posted: Fri Sep 07, 2007 5:20 pm
by invisibled
Hey guys, I have a bit of a problem.

I have this script that uploads an image to a server, and then takes the file name and throws it into a sql database.

Code: Select all

$target_path = "../_images/thumbs/";
			$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
			if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)): 
				$img = basename( $_FILES['uploadedfile']['name']);
			else: 
				$img = "portfolioPic.jpg";
				$placer = '<tr><td colspan="2" id="error" valign="top"><strong>Note:</strong> <em>Either The file you uploaded was too big, or you did not upload a file and we uploaded one for you. 2MB is the limit, so if you tried uploading an image, please shrink your file and try uploading again via the edit project page.</em> </td></tr>';
			endif;
then i can just use a normal while loop to display the images.

Code: Select all

while($data = mysql_fetch_array($mysql)):
			print'
				<tr>
					<td rowspan="2">
						<img src="../_images/thumbs/'.$data['img'].

it all works and is fine. But i want to be able to shrink the filesize of the images when they get uploaded. I've tryed integrating other re-size scripts into mine but it never works. Could anybody help me out? Thanks for reading.

Posted: Fri Sep 07, 2007 5:28 pm
by shwanky
o.O What version of php are you running?

Posted: Fri Sep 07, 2007 10:13 pm
by invisibled
5 somthing

5.2 i think

Posted: Fri Sep 07, 2007 10:53 pm
by bpat1434
[aside]Wow... totally surprised I remembered my account information for here[/aside]

I did some image resizing not long ago (maybe two years or so) for someone. This will resize the image proportionately and save it as a new file. This is working code (as I've used it to test some things) so it works for me. I will try and comment what I can:

Code: Select all

<?php
if(isset($_POST) && !empty($_FILES['photo']))
{
	// Get temporary name of the uploaded file
	$tempname = $_FILES['photo']['tmp_name'];
	$maxSize = 1024 * 1024 * 5;

	// If it's not actually uploaded, don't waste the resources....
	if(is_uploaded_file($tempname))
	{
		// Get the original dimensions of the image...
		$orig_dimensions = getimagesize($tempname);
		if($orig_dimensions === FALSE || $orig_dimensions[2] != 2)
		{
			die("Image upload failed!!");
		}

		// Put them into easier to use variables...
		$orig_width = $orig_dimensions[0];
		$orig_height = $orig_dimensions[1];

		// Create a copy of the uploaded image....
		$orig_image = imagecreatefromjpeg($tempname);

		// If the image is Portrait, define it as such
		$image_is_tall = ($orig_height>=$orig_width);

		// The max width (or height) of the image (in pixels)
		$max = 250;

		// Get the proportionate image size...
		if($image_is_tall)
		{
			$swidth = ceil(($max/$orig_height)*$orig_width);
			$sheight = $max;
		}
		else
		{
			$sheight = ceil(($max/$orig_width)*$orig_height);
			$swidth = $max;
		}

		// Create new image with the new size
		$new_image = imagecreatetruecolor($swidth, $sheight);

		// Copy the old image to the new canvas resampling it...
		if(imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $swidth, $sheight, $orig_width, $orig_height) === FALSE)
		{
			echo "Image creation Failed!!";
		}
		else
		{

			// Here is just the general storage of created image
			$photo_dir="uploads/";
			$photo_url="http://localhost/uploads/";
			$photo_name = 'photo_'.date('mdY-Hi', time()).'.jpg';
			$newer_image = $photo_dir.'/'.$photo_name;
			if(imagejpeg($new_image, $newer_image, 100) === FALSE)
			{
				echo "Creation of image failed!!";
			}
			else
			{

				// Output the image for viewing...
				$photo_url = $photo_url.$photo_name;
				echo "Image Created Successfully!!<br>";
				echo '<img src="'.$photo_url.'" alt="The New Photo!!">'; // Remove this if you don't want it.
			}
		}
	}
}

echo '
<form action="'. $_SERVER['PHP_SELF'] .'" method="post" enctype="multipart/form-data">
	<label for="iFile">Image:</label>
	<input type="file" name="photo" id="iFile" /><br />
	<input type="submit" name="submit" value="Resize!!" />
</form>';
?>
Hope that helps...