Jump to content

[SOLVED] image height or width


onthespot

Recommended Posts

Hello. The following is my code for uploading a profile photo.

It ensures the width is 150 and then makes the height in proportion to that.

This works really well when the photo is wider than it is high.

However for photos that are higher than wide, I need the code to change the height to 200, and make the width proportional.

 

<?
$profile = $_SESSION['username'];

if (isset ($_FILES['new_image']))
	{

		$imagename = $profile . '.jpg';
		$source = $_FILES['new_image']['tmp_name'];
		$source2 = $_FILES['new_image']['name'];
		$target = "images/users/".$imagename;
		$file = explode('.', $source2);
		$ext = strtolower(end($file));

		if ($ext != 'jpg' && $ext != 'jpeg')
		{
			echo 'Your file must be a jpeg, yours is: ' . $ext;

		}
		else
		{
			move_uploaded_file($source, $target);

			$imagepath = $imagename;
			$save = "images/users/" . $imagepath; 
			$file = "images/users/" . $imagepath; 

			list($width, $height) = getimagesize($file) ;

			$modwidth = 150;

			$diff = $width / $modwidth;

			$modheight = $height / $diff;
			$tn = imagecreatetruecolor($modwidth, $modheight) ;
			$image = imagecreatefromjpeg($file) ;
			imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

			imagejpeg($tn, $save, 100) ;

			echo "".$_SESSION['username'].", you have changed your profile picture";
			mysql_query($query);
		}

	}

	?>

 

That is my code, I need to some help with altering this to ensure that when a user uploades a photo, it is resized based on the the height or width depending on which is largest. Thankyou for reading :D

Link to comment
https://forums.phpfreaks.com/topic/169605-solved-image-height-or-width/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.