Jump to content

Recommended Posts

Hi again

 

I have an image upload script that is resizing every image that a user uploads.

What I would like to do is have a check box added to the upload form that a user can check to resize the image if they want to or leave it unchecked to just upload the image.

 

So if the box is checked the original image gets uploaded and a resized image is created as well

If the box is not checked just the original image uploaded.

 

I tried adding a check box but every image still was resized - checked or not - Im assiming because it was in the foreach loop so I dont know what to do.

 

This is what I have right now

 

FORM CODE
<b>File [<?php echo $x; ?>]:</b><br> <input type="file" name="upload[]" size="10"/>


PROCESS CODE
if (!(isset($_POST['folder']))) {
echo"<H1><span style=\"color: #ff1111;\">PROCESS ERROR</span></H1><br>We did not receive your information to process.<br>Please go <a href=\"img_manager.php\">back</a> and input your information again.";
exit(); // Quit the script.
include ('./includes/footer.html');
}

foreach ($_POST['folder'] as $p) {

			$pa = $p;
			}

			$query = "SELECT path FROM img_folder WHERE folder_id=$pa";
			$result25 = mysql_query ($query);
			while ($row = mysql_fetch_array($result25, MYSQL_ASSOC)) {
			$resultpath = $row['path'];
			}
			echo"<p>Return to the <a href=\"img_manager.php\">Image Manager</a> </p><br>";
if (!is_dir("images/$user_id/$resultpath")) {
  $oldumask = umask(0);
  mkdir("images/$user_id/$resultpath", 0777);
  umask($oldumask);
}
$num = $_POST['num'];
//start_process
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
//echo '<pre>' . print_r ($_FILES, 1) . '</pre>';   	
 echo'<fieldset><legend>Upload Results:</legend>
<table border="0" CELLSPACING="0" CELLPADDING="10" width="85%" align="center">
<tr>';

$numcols = 2; // how many columns to display
$numcolsprinted = 0; // no of columns so far

foreach ($_FILES['upload']['tmp_name'] as $key => $upload) {//begin foreach
if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}echo "<td valign=\"top\">";
$file_num = $key+1;
$filedir = 'images/' .$user_id.'/'.$resultpath.'/'; // the directory for the original image
$mode = '0777';
$userfile_name = $key.$_FILES['upload']['name'][$key];
$userfile_name = ereg_replace(' ', '_', $userfile_name); 
$userfile_tmp = $_FILES['upload']['tmp_name'][$key];
$userfile_size = $_FILES['upload']['size'][$key];
$userfile_type = $_FILES['upload']['type'][$key];	
$size = 400; // Image View
$thumb_prefix = 'th_'; // the prefix to be added to the original name
$thumbname = $thumb_prefix.$userfile_name;
$thumb_img = $filedir.$thumb_prefix.$userfile_name;
$add= $filedir.$userfile_name;
//Check for an uploaded file.
if (isset($_FILES['upload']['name'][$key]) && ($_FILES['upload']['error'][$key] != 4)){
    
	//if there is a file validate it
	$allowed = array ('image/png', 'image/x-png', 'image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg');
	if (in_array($_FILES['upload']['type'][$key], $allowed)) {
	   $orig_img = $filedir.$userfile_name;
	   	if (move_uploaded_file($_FILES['upload']['tmp_name'][$key], $orig_img)) {
		   chmod ($orig_img, octdec($mode));
		   
		   if ($userfile_type == "image/pjpeg" OR $userfile_type == "image/jpeg" OR $userfile_type == "image/jpg"){
chmod ($add, octdec($mode));
$sizes = getimagesize($add);
 $aspect_ratio = $sizes[1]/$sizes[0];

 if ($sizes[0] <= $size)
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];
	}else{
		$new_width = $size;
		$new_height = ($aspect_ratio*$size);
	}

	$destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
	$srcimg=ImageCreateFromJPEG($add) or die('Problem In opening Source Image');
	imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
	ImageJPEG($destimg,$thumb_img,75) or die('Problem In saving');
	imagedestroy($destimg);

}

if ($userfile_type == "image/gif"){
chmod ($add, octdec($mode));
$sizes = getimagesize($add);
 $aspect_ratio = $sizes[1]/$sizes[0];
 //start resize image
 if ($sizes[0] <= $size)
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];
	}else{
		$new_width = $size;
		$new_height = ($aspect_ratio*$size);
	}
	//
	$destimg=ImageCreate($new_width,$new_height) or die('Problem In Creating image');
	$srcimg=ImageCreateFromGIF($add) or die('Problem In opening Source Image');
	$colourBlack = imagecolorallocate($destimg, 0, 0, 0);
        imagecolortransparent($destimg, $colourBlack);
	imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
	ImageGIF($destimg,$thumb_img) or die('Problem In saving');
	imagedestroy($destimg);

}

if ($userfile_type =="image/png" OR $userfile_type =="image/x-png"){
chmod ($add, octdec($mode));
$sizes = getimagesize($add);
 $aspect_ratio = $sizes[1]/$sizes[0];
 //start resize image
 if ($sizes[0] <= $size)
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];
	}else{
		$new_width = $size;
		$new_height = ($aspect_ratio*$size);
	}
	//copied
	$destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
	$srcimg=ImageCreateFromPNG($add) or die('Problem In opening Source Image');
	$colourBlack = imagecolorallocate($destimg, 0, 0, 0);
        imagecolortransparent($destimg, $colourBlack);
	imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
	ImagePNG($destimg,$thumb_img) or die('Problem In saving');
	imagedestroy($destimg);

}
		   if (!empty($userfile_name)) {
		   $userfile_name = ereg_replace(' ', '_', $userfile_name); 
		   $i = escape_data($userfile_name);
		   $i = escape_data($userfile_name);
		   // Check for terms.
		   if (!empty($userfile_size)) {
		   $s = escape_data($userfile_size);
 		   // Make the query.
		   $query = "INSERT INTO img (user_id, img_name, img_size) VALUES ('$user_id', '$i', '$s')";
		   $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		   //make assoc
		   $uid = mysql_insert_id(); // Get the url ID.
		   if ($uid > 0) { // New article has been added.
		   $query = 'INSERT INTO img_assoc (id, folder_id, user_id) VALUES ';
		   foreach ($_POST['folder'] as $v) {
			$query .= "($uid, $v, $user_id), ";
			}
			$query = substr ($query, 0, -2); // Chop off the last comma and space.

			$result = mysql_query ($query); // Run the query.
		   }
		   $t = escape_data($thumbname);
		   $ts = filesize($thumb_img);
		   $query2 = "INSERT INTO img (user_id, img_name, img_size) VALUES ('$user_id', '$t', '$ts')";
		   $result2 = mysql_query ($query2) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		   $uid2 = mysql_insert_id(); // Get the url ID.

		   if ($uid2 > 0) { // New article has been added.
		   $query3 = 'INSERT INTO img_assoc (id, folder_id, user_id) VALUES ';
		   foreach ($_POST['folder'] as $v) {
			$query3 .= "($uid2, $v, $user_id), ";
			}
			$query3 = substr ($query3, 0, -2); // Chop off the last comma and space.

			$result3 = mysql_query ($query3); // Run the query.
		   }
		   
		   if (mysql_affected_rows() == 4)
		   mysql_close(); // Close the database connection.
		   }
		   }
		   echo '<span class="ulpass">SUCCESS</span><br>File ['.$file_num.']:<br><span class="ulinfo">Was uploaded!</span><br><a href="'.$filedir.''.$userfile_name.'" target="_blank"><img src="sized.php?image='.$filedir.''.$userfile_name.'&width=100&height=150" border="0"></a>';

		} else { // Couldn't move the file over.

			echo '<span class="error">Upload failed</span><br>File ['.$file_num.'] <br /><span class="ulinfocause">We are experiencing technical difficulties.  Please try again later.</span>';			

		} // End of move... IF.
	//if its not allowed
	}else{
	echo '<span class="error">Upload failed</span><br>  File ['.$file_num.']<br> <span class="ulinfocause">was not uploaded because:<br />';
	// Print a message based upon the error.
			switch ($_FILES['upload']['error'][$key]) {
				case 1:
					print 'It exceeded the servers max file size.';
					break;
					continue $key;
				case 2:
					print 'It exceeded the acceptable file size.<br />Please edit your original file size and try again.';
					break;
					continue $key;
				case 3:
					print 'It was only partially uploaded.<br />Please try again.';
					break;
					continue $key;
				case 4:
					print 'It was an empty field on the upload form.';
					break;
					continue $key;
				case 6:
					print 'It could not be loaded to a folder.';
					break;
					continue $key;
				case ($allowed = FALSE):
					print 'It was the wrong file type.<br />Please try again with an acceptable file type located on the upload page.';
					unlink ($_FILES['upload']['tmp_name'][$key]); // Delete the file.
					break;
					continue $key;
				default:
					print 'This feature is currently unavailable<br />Please check back soon.';
					break;

			} // End of switch.

			print '</span>';	

	}//endof not valid echo



	//end validation
	}

	//no uploaded file
	else{
	echo'Form input # ['.$file_num.']<br><span class="ulinfocause"> did not contain any data to upload.<br />If you meant to add a file there you will need to upload it again.</span>';
	}
//start table code bottom
		echo"</td>\n";
	$numcolsprinted++;


	}//end foreach
// bump up row counter

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

print "<TD> </TD>\n";
}
//stop table code bottom
echo'</tr>
</table>
</fieldset>	';		


} // End of the submitted conditional.
else{
$selnum .= 'photogal.php';
header("Location: $selnum");
exit(); // Quit the script.
}
//stop_process

 

Any ideas/ help appreciated.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/42214-resize-only-selected-images/
Share on other sites

I didn't really do much more than skim through your code, but use the checkbox in a form with method post, then check that variable using $_POST['resize'] ('resize' being whatever the name of your checkbox is), try this:

 

if(!empty($_POST['resize']) {

  //do resize code

  } else {

  //do not resize code

  }

 

No guarantees though, sorry I can't be more help.

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.