Jump to content

Image resize not working in multiple upload script


NickG21

Recommended Posts

Hey everyone, below is a script that I got online and have modified to be used for up to 5 images, instead of just 1.  The script is supposed to take the uploaded file, resize it to 350px wide x (whatever heights end up), and also create a thumbnail that is 50x50px, then store into a database.

The script works great with everything except resizing the initial image to 350px.  when using this by itself, the script worked great but now i cant figure out the resize for multiple images.  if anyone could help me out it would be greatly appreciated.

Thanks in advance,

NickG

 

P.S. I've commented everything that needs to be looked at so you dont need to sieve through everything to find it.

 

<?php
include('header.inc.php');
include('dbinfo.inc.php');

error_reporting(0);

$change="";
$abc="";
$max_img_no=5; //Maximum images to be set here	

define ("MAX_SIZE","40000");
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;
  
if($_SERVER["REQUEST_METHOD"] == "POST"){

	foreach($_POST as $key => $value){
		$$key = $value;
		//echo "Key is: " . $key . '<br/>';
		//echo "Value is: " . $value . '<br/>';
	}
//Error check for open fields
if((empty($PropTitle)) || (empty($PropDesc)) || (empty($PropPrice))){
	$error++;	
	echo "There were unfilled areas, only the picture is optional, all other areas must be filled out.";

}else{
	$PropTitle = mysql_real_escape_string($PropTitle);
	$PropDesc = mysql_real_escape_string($PropDesc);
		$x = 1;
		$image1 = $_FILES["file"]["name"][$x];
		//echo "image 1 is" . $image1;
		$InsPropSQL = "INSERT into tblProperty(PropID, PropTitle, PropDesc, PropPrice, PropImage)values('','$PropTitle','$PropDesc','$PropPrice','$image1')";
		//echo $InsPropSQL;
		$rsInsPropSQL = mysql_query($InsPropSQL);

	//Select last row in tblProperty after inserted, to get newest Property ID
		$query = mysql_query("SELECT * FROM tblProperty WHERE PropID = (SELECT MAX(PropID) FROM tblProperty)");
			while($row=mysql_fetch_array($query)){
				$_SESSION['PropID'] = $row['PropID'];
				//echo $_SESSION['PropID'];
		}
	//Loop through the files that were chosen to be uploaded, resize them, upload and insert into other DB
for($i=1; $i<=$max_img_no; $i++){
		$image = $_FILES["file"]["name"][$i];
			//echo $image . '<br/>';
 		$filename = stripslashes($_FILES['file']['name'][$i]);
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);

		if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
		 			$change='<div class="msgdiv">Unknown Image extension </div> ';
		 			$errors=1;

		 		}else{

		 			$size=filesize($_FILES['file']['tmp_name'][$i]);

		if ($size > MAX_SIZE*1024){

			$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
			$errors=1;
		}


		if($extension=="jpg" || $extension=="jpeg" ){

			$uploadedfile = $_FILES['file']['tmp_name'][$i];
			$src = imagecreatefromjpeg($uploadedfile);

		}else if($extension=="png"){

			$uploadedfile = $_FILES['file']['tmp_name'][$i];
			$src = imagecreatefrompng($uploadedfile);

		}else{

			$src = imagecreatefromgif($uploadedfile);
		}
		//echo $src;
		list($width,$height)=getimagesize($uploadedfile);

		//set the newwidth for the initial image as well as newwidth1 for the thumbnail image
			$newwidth=350;
			$newheight=($height/$width)*$newwidth;
			$tmp=imagecreatetruecolor($newwidth,$newheight);


			$newwidth1=50;
			$newheight1=($height/$width)*$newwidth1;
			$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

			imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

		//set the directories to be uploaded to as well as the filename to be stored in DB
			$filename = "../images/". $_FILES['file']['name'][$i];
			$ImageName = $_FILES['file']['name'][$i];

			$filename1 = "../images/small/small". $_FILES['file']['name'][$i];
			$ImageName1 =  $_FILES['file']['name'][$i];



		imagejpeg($tmp,$filename,100);

		imagejpeg($tmp1,$filename1,100);
		//destroy the temporary image and src then loop through
		imagedestroy($src);
		imagedestroy($tmp);
		imagedestroy($tmp1);
				}
		$uploadedfile = $_FILES['file']['tmp_name'][$i];	
	//if the file successfully uploaded, then insert new row into Property Images table
	if(move_uploaded_file($uploadedfile, $filename)){

		$InsImgSQL = "INSERT into tblImage(ImgID,ImgPropID,ImgName,ImgThumb) value('','$_SESSION[PropID]','$ImageName','small$ImageName')";
		//echo $InsImgSQL;
		$rsInsImgSQL = mysql_query($InsImgSQL);
	}
}
}
}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{
	// mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
	echo ' <div class="msgdiv">Image Uploaded Successfully!</div>';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.