Jump to content

image upload and resize x2


triphis

Recommended Posts

Hey everyone :)

 

I have managed in the past to do simple image uploading and resizing... but now I want to extend one of my scripts to make a THUMBNAIL of the newly resized image. I am having trouble "capturing" the just-resized image and running it through the same process again. Hopefully someone here can help :) The code seen below is on a page that accepts image uploads as an array...

 

Thanks in advance!

 

foreach($_FILES['images']['tmp_name'] as $temp)
{
        if($temp != NULL)
        {
                $result = mysql_query("SELECT * FROM pictures WHERE productcode = '$productcode'") or die(mysql_error());
                $number = mysql_num_rows($result) + 1;

	if($number == 1) { $main = 1; }
	else { $main = 0; }

	$directory = '../uploads/';
	$filename = $productcode . "_" . $number . ".jpg";

	$newfile = $directory . $filename;

                $uploadedfile = $temp;
	$src = imagecreatefromjpeg($uploadedfile);
	list($width,$height)=getimagesize($uploadedfile);

                $newwidth=250;
        $newheight=304;
	$tmp=imagecreatetruecolor($newwidth,$newheight);
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

                $justresized = imagejpeg($tmp,$newfile,100);    // is this right???


	$thumbdirectory = '../uploads/thumbs/';
	$Tfilename = $productcode . "_" . $number . ".jpg";   // same name as before

	$Tnewfile = $thumbdirectory . $Tfilename;

	$uploadedfilethumb = $justresized;  // I think this is where the problem is
	$srcthumb = imagecreatefromjpeg($uploadedfilethumb);
	list($Twidth,$Theight)=getimagesize($uploadedfilethumb);

		$Tnewwidth=100;
		$Tnewheight=100;
		$tmpthumb=imagecreatetruecolor($Tnewwidth,$Tnewheight);
		imagecopyresampled($tmpthumb,$srcthumb,0,0,0,0,$Tnewwidth,$Tnewheight,$Twidth,$Theight);

	if (imagejpeg($tmpthumb,$Tnewfile,100))
	{
		echo "The image was uploaded, and the thumbnail was created!";

		$entry = "INSERT INTO pictures(id,productcode,image,main) VALUES(NULL,'$productcode','$filename','$main')";
		mysql_query($entry) or die(mysql_error());

		imagedestroy($src);
		imagedestroy($tmp);
	}
       }
}

 

 

Also, if anyone can write a better confirmation-checker (ie: that both images were uploaded successfully) that would be great! Right now, the confirmation message is based only off the thumb... Which kinda makes sense since the thumb is created from the first one, but I think there must be a better way!

 

Thanks again!

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.