Jump to content

Create thumbs upon upload


Recommended Posts

Having a form where user can upload image files (currently just one) I need to test for a maximum height and width and decrease size if it exceeds the limit. In addtion a thumbnail also needs to be created and both put into separate folders. I found few functions, which can resize an image, but am not sure how to put the whole thing into my file. The relevant code currently is:

// check for img1if (!isset($_FILES['img1']['name']) OR empty($_FILES['img1']['name'])) {		$errors['img1'] = '\'Image 1\' is a required field';$img1 = FALSE;} else {$ext = explode('.',$_FILES['img1']['name']);$ext = $ext[count($ext)-1];if (!in_array($ext, $allowed)) {$errors['img1'] = '\'Image 1\' accepts only jpg and gif';$img1 = FALSE;} else {$img1 = $user_id . '-1.' . $ext;}}if (isset($_FILES['img1'])) { 	// validate the input		global $ext;		if (in_array($ext, $allowed)) {			// move the file over			if(move_uploaded_file($_FILES['img1']['tmp_name'], "{$images}/{$img1}")) {			echo '<p>the file has been uploaded ok</p>';			} else {			echo '<p>the file upload did not work</p>';			}			// add the data			$query = "INSERT INTO imgTest (user_id, img1) VALUES ('$user_id', '$img1')";					$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());			if (mysqli_affected_rows($dbc) == 1) { // If it ran ok       			echo "db insertion went ok";			} else { // if it did not run ok				echo '<p>Your data could not be uploaded due to a system error. We apologise for any inconvenience.</p>'; 			}				}  else { // if one of the data tests failed		echo '<p>You forgot to upload required data or there is a problem with the given input. Please amend the highlighted fields.</p>';	}	}} else // Please upload your data message etc

Link to post
Share on other sites

You can output images manipulated with the GD functions to files with image[type]() - e.g.

imagejpeg($image, "file.jpg");

Link to post
Share on other sites
You can output images manipulated with the GD functions to files with image[type]() - e.g.
imagejpeg($image, "file.jpg");

Saw that, but as I do not know beforehand what image type folks upload am not sure in how to approach (have an array of allowed files)...Kurt
Link to post
Share on other sites

get the extension of the uploaded file, then something like:switch(strtolower($ext)){case 'png':$img=imagecreatefrompng($file);break;case 'jpg':case 'jpeg':$img=imagecreatefromjpeg($file);break;case 'gif':$img=imagecreatefromgif($file);break;default:die('imagetype not supported');}and same at the endswitch(strtolower($ext)){case 'png':$img=imageng($img,$newName);break;case 'jpg':case 'jpeg':$img=imagejpeg($img,$newName);break;case 'gif':$img=imagegif($img,$newName);break;}

Link to post
Share on other sites
get the extension of the uploaded file, then something like:switch(strtolower($ext)){case 'png':$img=imagecreatefrompng($file);break;case 'jpg':case 'jpeg':$img=imagecreatefromjpeg($file);break;case 'gif':$img=imagecreatefromgif($file);break;default:die('imagetype not supported');}and same at the endswitch(strtolower($ext)){case 'png':$img=imageng($img,$newName);break;case 'jpg':case 'jpeg':$img=imagejpeg($img,$newName);break;case 'gif':$img=imagegif($img,$newName);break;}
Wander,Thanks for your input. That sounds like what I am after, but where would I put the functions to check for max height/width and resize? Still cannot get my head around in how to apply functions to variables. Maybe there is a complete example code somewhere of sth similar?Kurt
Link to post
Share on other sites

I don't know how much this will help. I wrote it to crop/resample uploaded images so they would fit a predetermined size; it also saves all images as jpegs. This is really out of context, but maybe it'll spark something. (And as you see, it trusts the original file ext. Oh well!)

	switch ($_FILES["file"]["type"]) {		case 'image/jpeg':		case 'image/pjpeg':			$im = @imagecreatefromjpeg($_FILES["file"]["tmp_name"]);			break;		case 'image/gif':			$im = @imagecreatefromgif($_FILES["file"]["tmp_name"]);			break;		case 'image/png':			$im = @imagecreatefrompng($_FILES["file"]["tmp_name"]);			break;		default:			$message = "Sorry. Wrong file type.";			return 0;			break;	}	 $W = imagesx ($im);	$H = imagesy ($im);	$newW = 200;	$newH = 240;	$dup = imagecreatetruecolor($newW, $newH);	if ($H/$W > 1.2) { # IMAGE IS TOO TALL		$tempH = $W * 1.2;		$TOP  = abs(floor(($H - $tempH)/2));	} else {		$tempW = $H / 1.2;		$LEFT  = abs(floor(($W - $tempW)/2));	}					$result = imagecopyresampled($dup, $im, 0, 0, $LEFT, 0, $newW, $newH, $tempW, $H);	$result = imagejpeg ($dup, $path);

Link to post
Share on other sites
I don't know how much this will help. I wrote it to crop/resample uploaded images so they would fit a predetermined size; it also saves all images as jpegs. This is really out of context, but maybe it'll spark something. (And as you see, it trusts the original file ext. Oh well!)
	switch ($_FILES["file"]["type"]) {		case 'image/jpeg':		case 'image/pjpeg':			$im = @imagecreatefromjpeg($_FILES["file"]["tmp_name"]);			break;		case 'image/gif':			$im = @imagecreatefromgif($_FILES["file"]["tmp_name"]);			break;		case 'image/png':			$im = @imagecreatefrompng($_FILES["file"]["tmp_name"]);			break;		default:			$message = "Sorry. Wrong file type.";			return 0;			break;	}	 $W = imagesx ($im);	$H = imagesy ($im);	$newW = 200;	$newH = 240;	$dup = imagecreatetruecolor($newW, $newH);	if ($H/$W > 1.2) { # IMAGE IS TOO TALL		$tempH = $W * 1.2;		$TOP  = abs(floor(($H - $tempH)/2));	} else {		$tempW = $H / 1.2;		$LEFT  = abs(floor(($W - $tempW)/2));	}					$result = imagecopyresampled($dup, $im, 0, 0, $LEFT, 0, $newW, $newH, $tempW, $H);	$result = imagejpeg ($dup, $path);

Thanks, thats great. Will test and amend it later on to see if I can modify to fullfill my tasks. Cheers,Kurt
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...