Jump to content

Upload/resize image - create thumb on the fly


barney0o0

Recommended Posts

HI Chaps, i need someone to hold my hand with this...i have a working script to upload a image (below)...what id like it to do is also create a small image of the cropped image...ive been messing around by simply duplicating the original values, but i just cant get it to work...im not very knowledgeable :)

 

On the initial page (to browse for the file), i have hidden inputs

		<input type="hidden" id="x" name="x" />
		<input type="hidden" id="y" name="y" />
		<input type="hidden" id="w" name="w" />
		<input type="hidden" id="h" name="h" />

The its passed onto another page for processing. The $nw/$nh values produce the large image. I would like to add another two values that would create a smaller image (at the same ratio)

  <?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 20343 * 30124; #200kb
$nw = 1600; # image with # height
$nh = 627;

?>
  <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	if ( isset($_FILES['image']) ) {
		if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
			$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
			if (in_array($ext, $valid_exts)) {
					$path = '../mobili/' . uniqid() . '.' . $ext;
					$size = getimagesize($_FILES['image']['tmp_name']);
					
					$x = (int) $_POST['x'];
					$y = (int) $_POST['y'];
					$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
					$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];
					$data = file_get_contents($_FILES['image']['tmp_name']);
					$vImg = imagecreatefromstring($data);
					$dstImg = imagecreatetruecolor($nw, $nh);
					imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
					imagejpeg($dstImg, $path);
					imagedestroy($dstImg);
					
					
				} else {
					echo 'unknown problem!';
				} 
		} else {
			echo 'file is too small or large';
		}
	} else {
		echo 'file not set';
	}
} else {
	echo 'bad request!';
}

	?>

The $path value is added to the database.

 

 

 

I hope someone can help, ive spent an afternoon trying many things (badly!)...thanks in advance

 

 

Link to comment
Share on other sites

This

$dstImg = imagecreatetruecolor($nw, $nh);
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
imagejpeg($dstImg, $path);
imagedestroy($dstImg);
is where the first image is created. If you want another but at different dimensions then set up two more variables for the size - maybe $tw and $th? - then fill them in to a copy of the above code:

$dstImg = imagecreatetruecolor(??, ??);
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, ??, ??, $w, $h);
imagejpeg($dstImg, ???);
imagedestroy($dstImg);
(You also need to figure out the new path too.)
Link to comment
Share on other sites

Would i simply go through the first bit of processing > destroy the image, then do the same again but with different size variables?...if that is the case, do i need to change the strings $dstImg to say $distImg2 to save conflict or am i misread the flow of the code..thanks for your help

Link to comment
Share on other sites

OK, got it sorted :)...thanks requinix...i was working at it too thinking i couldn't reuse some of the strings due to conflicts...heres the working script with both large/small images and watermark on the large image

  <?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 20343 * 30124; #200kb
$nw = 1600; # image with # height
$nh = 627;
	$nw2 = 800; # image with # height
$nh2 = 313;

?>
  <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	if ( isset($_FILES['image']) ) {
		if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
			$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
			if (in_array($ext, $valid_exts)) {
				$path = '../mobili/' . uniqid() . '.' . $ext;
				$path2 = '../mobili2/' . uniqid() . '.' . $ext;
					
				
				$size = getimagesize($_FILES['image']['tmp_name']);
					
					$x = (int) $_POST['x'];
					$y = (int) $_POST['y'];
					$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
					$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];
					$data = file_get_contents($_FILES['image']['tmp_name']);
					
				
				$vImg = imagecreatefromstring($data);
					$dstImg = imagecreatetruecolor($nw, $nh);
					imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
				
													
$stamp = imagecreatefrompng('watermark.png');

$marge_right = 30;
$marge_bottom = 25;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

imagecopy($dstImg, $stamp, imagesx($dstImg) - $sx - $marge_right, imagesy($dstImg) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));		
				
imagejpeg($dstImg, $path);
imagedestroy($dstImg);	
				
				
				
				
				
$dstImg = imagecreatetruecolor($nw2, $nh2);
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw2, $nh2, $w, $h);
imagejpeg($dstImg, $path2);
imagedestroy($dstImg);
				
				
				
				
				
				
				
				
				
				
				
				
				
					
				} else {
					echo 'unknown problem!';
				} 
		} else {
			echo 'file is too small or large';
		}
	} else {
		echo 'file not set';
	}
} else {
	echo 'bad request!';
}

	?>
Link to comment
Share on other sites

If no one reinvented the wheel from time to time we'd be driving around with a granite disk at each corner :)

 

After a quick look, it doesnt incorporate to click/drag to select part of an image you want to resize.....which the one i use does

 

Your posted code doesn't imply that, since that would be taken care of in JavaScript or some other system on the client-side.  Your PHP side is much less robust and perhaps less feature-ful than SLIR, based on the observation here.

 

 

If no one reinvented the wheel from time to time we'd be driving around with a granite disk at each corner :)

 

Hmm ... other than initial outlay and its effect on gas mileage ....

Link to comment
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.