I am trying to upload an image and create a thumbnail. It will upload but the thumbnail is not created. Here is a sample of the code. Can anyone tell me why this is happening?
$product = $_GET['type'] == 1 ? 'products/' : ''; //If working on the main image, place it in the products directory
$mime = getimagesize($_FILES['itmFile']['tmp_name']); //get information about the image
$mimetype = $mime['mime']; //store mime type in a seperate variable
if (strlen($_FILES['itmFile']['name']) > 45) //check filename length
{
$error = 'The image name must be less than 46 characters.'; //send an error
$_GET['mode'] = 'file'; //return to the file uploader
}
else if ($mimetype != 'image/png' && $mimetype != 'image/jpeg' && $mimetype != 'image/gif' && $mimetype != 'image/wbmp') //if it's not a usable format
{
$error = 'The image must be a PNG, JPEG, GIF or BMP file.'; //send an error
$_GET['mode'] = 'file'; //return to the file uploader
}
$itmFile = $_FILES['itmFile']['name']; //give the filename its own variable
$itmID = intval($_GET['itmID']); //remove extra chacters from itmID
move_uploaded_file($_FILES['itmFile']['tmp_name'], 'images/'.$product.$_FILES['itmFile']['name']); //move the file to a permanent location
if ($_GET['type'] == 1) //If working with the main image
{
$modify = $mime[0] / 150; //find out how to find the height
$height = $mime[1] / $modify; //calculate the height
$name = str_replace('.', 'thumb.', $file); //change to the thumbnail filename.
$path = 'images/'.$product.$itmFile; //put together the path
switch ($mimetype) //choose an action for the correct mime type
{
case 'image/png':
$image = imagecreatefrompng($path) or die('problem 1'); //take the image, and make it an image resource that PHP can use
break;
case 'image/gif':
$image = imagecreatefromgif($path) or die('problem 2');
break;
case 'image/jpeg':
$image = imagecreatefromjpeg($path) or die('problem 3');
break;
case 'image/wbmp':
$image = imagecreatefromwbmp($path) or die('problem 4');
break;
}
$name2 = imagecreate(150, $height); //create a blank image
imagecopyresampled($name2, $image, 0, 0, 0, 0, 150, $height, $mime[0], $mime[1]) or die('work'); //resize and resample the image
mysql_query("UPDATE items SET itmImage = '$itmFile' WHERE itmID = '$itmID'") or die(mysql_error()); //put the filename in the database
switch ($mimetype) //choose an action for the correct mime type
{
case 'image/png':
imagepng($name2, $name) or die('last step 1'); //make a new image with the modified one
break;
case 'image/gif':
imagegif($name2, $name) or die('last step 2');
break;
case 'image/jpeg':
imagejpeg($name2, $name) or die('last step 3');
break;
case 'image/wbmp':
imagewbmp($name2, $name) or die('last step 4');
break;
}
unset($_GET['itmID']); //destroy the itmID variable
$error = 'The file has been added.'; //tell the user it worked.
}