Upload Image - create thumb


#1

Hi Chaps..

I have a working script to upload, trim and add to db. Im wish to also to add a thumb of the same cropped image (at the same ratio) however after trying so many combinations i cant get it to work.

<?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 20343 * 30124; #200kb
$nw = 900; 
$nh = 1080;
$nwt = 450; 
$nht = 540;
?>
<?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);
								$stamp = imagecreatefrompng('watermarks.png');

$marge_right = 20;
$marge_bottom = 10;
$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);	
					
					
				} else {
					echo 'unknown problem!';
				} 
		} else {
			echo 'file is too small or large';
		}
	} else {
		echo 'file not set';
	}
} else {
	echo 'bad request!';
}

?>

Using the $nw/$nh values all is fine and adding the $path value to the database.If i wish to add a thumb image based on the $nwt/$nht (same ratio) how do i go about it? Ive tried duplicating parts of the code to include the new values, but i keep making a mess of it.

Thanks in advance


#2

Have a read through this thread from just before Christmas - this deals with creating copies of an uploaded image in multiple different sizes.

While it's true that the poster was having some problems, the code might be enough for you to see what you're doing wrong.

If it doesn't help, post the code you've tried and some detail on what's going wrong with it and someone will spot what's causing an issue.


#3

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.