Jump to content

Upload a photo - get a fullsize and a thumbnail


El Chupacodra

Recommended Posts

Still working on the same project.

I had a function to upload a photo and you could use it as a profile picture.

Now I wanted to expand on it and decided you can upload more than one photo, creating a gallery.

I figured I could use the same script to create two images (one original size and one thumbnail for browsing) and went about it in two different ways, both failed miserably.

First I figured my upload code could take the indata and make TWO photos at once with different name.

I guess it can only keep on image in mind at once - or I did something else wrong.

I used the same code to create $user.jpg and $user_tn.jpg and move them to the user's image folder.

 

So try number two was to make two folders for the photo, one named after the user (for thumb) and another in a folder named big.

That isn't working for me either.

 

Anyone who likes to chime in with ideas or facepalms is very welcome.

 

Here is the generic code I set out with:

 

if ($view == $user) {
mkdir("grafik/users/$user");
mkdir("grafik/users/$user/big/"); 

if (isset($_FILES['image']['name']))
{
$saveto = "grafik/users/$user/big/$user.jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
$typeok = TRUE;

switch($_FILES['image']['type'])
{
	case "image/gif": $src = imagecreatefromgif($saveto); break;
	case "image/jpeg": 
	case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
	case "image/png": $src = imagecreatefrompng($saveto); break;
	default: $typeok = FALSE; break;
}

if ($typeok)
{
	list($w, $h) = getimagesize($saveto);
	$max = 200;
	$tw = $w;
	$th = $h;

	if ($w > $h && $max < $w)
	{
		$th = $max / $w * $h;
		$tw = $max; }
	elseif ($h > $w && $max < $h)
	{
		$tw = $max / $h *$w;
		$th = $max;}
	elseif ($max < $w)
	{
		$tw = $th = $max;}
	$tmp = imagecreatetruecolor($tw,$th);
	imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
	imageconvolution($tmp, array(
							array(-1, -1, -1),
							array(-1, 16, -1),
							array(-1, -1, -1)
							), 8, 0);
	imagejpeg($tmp, $saveto);
	imagedestroy($tmp);
	imagedestroy($src);	
}
}
echo <<<_END
<div id='main'>
<form method='post' action='profile.php' enctype='multipart/form-data'>
Image: <input type='file' name='image' size='14' maxlength='32' />
<input type='submit' value='Save Profile' /> 
</form>
</div>
_END;
}

 

It is the page in minimalist form and it still contains the big folder version.

Thanks for putting up with my nooby questions.

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.