Jump to content

Resizing an Image on upload


coolphpdude

Recommended Posts

Hi there, i have previously posted a request for help on this matter but i am still struggling so i have launched a new topic on it. I want my user to upload a picture regardless of size, i want the original pic to be resized and also a smaller thumbnail created. i have been trying to work through some code provided to me previously but im still stuck. I have posted my code below.

 

I have taken out any irrelevant crap. Thanks for your help!

 

<?php
session_start();

// Take the sessions into variables
$user_session_id =$_SESSION['user_session_id'];
$user_session_password =$_SESSION['user_session_password'];

$upload=$_POST["upload"];


// ********************************* code given when i previously requested help
function resampleimage($maxsize, $sourcefile, $destination, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
$image_type = strrchr($sourcefile, ".");

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst, $destination, 100);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}




// *** Page Body - START ***

	if(!$user_rows) 
	{ 
			do_error("No results found $user_session_id"); 
	} 

	else 
	{

		if ((($_FILES["uploadedfile"]["type"] == "image/pjpeg")
		|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
		|| ($_FILES["uploadedfile"]["type"] == "image/png")
		|| ($_FILES["uploadedfile"]["type"] == "image/bmp"))
		&& ($_FILES["uploadedfile"]["size"] < 2000000))
		{

			// Where the file is going to be placed
			$base = "uploads/$user_session_id/";
			$target = "uploads/$user_session_id/";

			/* Add the original filename to our target path.  
			Result is "uploads/filename.extension" */
			$target = $target . basename( $_FILES['upload']['name']); 
			$_FILES['uploadedfile']['tmp_name']; 

			if (file_exists($target))
			{

			// make the random file name
			$rand = md5(rand() * time());
			$target = $base_path . $rand . basename( $_FILES['upload']['name']);

				if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
				{
				$sql="UPDATE user SET user_picture='$target' WHERE user_id='$user_session_id'";
				      // Save full size image with max width/height of 400
				  resampleimage("400", $target, $target, 0);
				  // Save thumb image with max width/height of 100
				  resampleimage("100", $target, $target, 0);

					if (mysql_query($sql,$db)) 
					{
						echo "The file ".  basename( $_FILES['upload']['name'])." has been uploaded";
					}
					else
					{
						do_error("Error - New user, $user_id could not be added");
					}
				} 
				else
				{
				echo "There was an error uploading the file, please try again!";
				}

			}
			else
			{
				if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
				{
				$sql="UPDATE user SET user_picture='$target' WHERE user_id='$user_session_id'";
				      // Save full size image with max width/height of 400
				  resampleimage("400", $target, $target, 0);
				  // Save thumb image with max width/height of 100
				  resampleimage("100", $target, $target, 0);

					if (mysql_query($sql,$db)) 
					{
						echo "The file ".  basename( $_FILES['upload']['name'])." has been uploaded";
					}
					else
					{
						do_error("Error - New user, $user_id could not be added");
					}
				} 
				else
				{
				echo "There was an error uploading the file, please try again!";
				}
			}
		}
		else
		{
		echo "invalid file type";
		}

	}


// *********************** No Results **************************

function  do_error($error) 
{
echo "USer not found";
}


echo "</html>";
?>

Link to comment
https://forums.phpfreaks.com/topic/101035-resizing-an-image-on-upload/
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.