Jump to content


Image upload resize


3 replies to this topic

#1 Dawiss

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 16 August 2008 - 01:53 AM

Ok I have made a program where user browses an image and submits it, program gets the image uploads it and resizes it.. Yes everything is working and in the end it shows me a new image in 700x550px but what I want is to in the end to upload the new image to diffrent location.. Any ideas how to do it?
<?

$image = $_FILES['upload']['name'];
$tmpimage = $_FILES['upload']['tmp_name'];

$target_path = "img/";
$target_path = $target_path . basename($image);
if(move_uploaded_file($tmpimage, $target_path)) {
	$text = "Image ".  basename($image). " wiith name $image added";
	} 
	
$image = "img/$image";		
$size = getimagesize($image);

$w = $size[0];
$h = $size[1];

$NewW = 700;
$NewH = 550;

$new = imagecreatetruecolor($NewW, $NewH);
$file = imagecreatefromjpeg($image);
imagecopyresampled($new, $file, 0, 0, 0, 0, $NewW, $NewH, $w, $h);

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

?>


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 16 August 2008 - 05:57 AM

When you do the imagejpeg() function, if you leave the 2nd paramater empty then it will output the image, but if you supply a file location as the 2nd param then it will save the image there.

#3 Arsenal19

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 17 August 2008 - 02:04 AM

You are not really resizing the image. you are just moving it to a location then trying to output the new resized image?

If thats what you intended to do then you going in the right direction. If you just wanted to resize the image for good on your server then try this.

All you have to do is supply a $maxWidth, $maxHeight, and the $_FILES['variable']. In my code it uses $file because its passed into a function. It will determine which way to resize the image.

		if($file['error'] == 0)
		{
			list($width,$height,$type) = getimagesize($file['tmp_name']);
			if($type == '2')
			{
				$w = $maxWidth;;
				$h = $maxHeight;
				$sw = $width;
				$sh = $height;
				if($width > $maxWidth or $height > $maxHeight)
				{
						$hx = (100 / ($sw / $w)) * .01;
						$hx = @round ($sh * $hx);
				
						$wx = (100 / ($sh / $h)) * .01;
						$wx = @round ($sw * $wx);
				
						if ($hx < $h) {
							$h = (100 / ($sw / $w)) * .01;
							$h = @round ($sh * $h);
						} else {
							$w = (100 / ($sh / $h)) * .01;
							$w = @round ($sw * $w);
						}
						$nWidth = $w;
						$nHeight = $h;
				}
				else
				{
					$nWidth = $maxWidth;
					$nHeight = $maxHeight;
				}
				$src = imagecreatefromjpeg($file['tmp_name']);
				$tmp = imagecreatetruecolor($nWidth,$nHeight);
				imagecopyresampled($tmp,$src,0,0,0,0,$nWidth,$nHeight,$width,$height);
				imagejpeg($tmp,$file['tmp_name'],100);
				imagedestroy($src);
				imagedestroy($tmp);
			}
		}

Let me know if this helps you out.

Arsenal19

Edited by Arsenal19, 17 August 2008 - 02:06 AM.


#4 Dawiss

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 17 August 2008 - 03:45 AM

thanks for the help, the problem was I didnt put the second parameter in imagejpeg() function. thanks for the help, baisicly this program is part of program which gets image resizes it uploads and then make crop off it in diffrent directory.. Thanks for help, agan :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users