My goal is to get the script to upload a photo, resize it to maximum width 400px, create a thumbnail of maximum width 135px, then insert each filename into the database. The errors I get are:
Here is the code:
<?php
session_start();
require_once('Connections/DBConnect.php');
if (!function_exists("GetSQLValueString"))
{
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType)
{
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING']))
{
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadphoto"))
{
if (array_key_exists('upload', $_POST))
{
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
// begin by assuming the file is unacceptable
$typeOK = false;
// check that file is of an permitted MIME type
foreach ($permitted as $type)
{
if ($type == $_FILES['image']['type'])
{
$typeOK = true;
break;
}
}
if ($typeOK)
{
switch($_FILES['image']['error'])
{
case 0:
// define constants
define('PHOTO_DIR', 'C:/wamp/www/test/photos/');
define('MAX_PHOTO_WIDTH', 400);
define('MAX_PHOTO_HEIGHT', 750);
define('THUMB_DIR', 'C:/wamp/www/test/photos/thumbs/');
define('MAX_THUMB_WIDTH', 135);
define('MAX_THUMB_HEIGHT',200);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name']))
{
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original photo
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio for the new large photo
if ($width <= MAX_PHOTO_WIDTH && $height <= MAX_PHOTO_HEIGHT)
{
$photo_ratio = 1;
}
elseif ($width > $height)
{
$photo_ratio = MAX_PHOTO_WIDTH/$width;
}
else
{
$photo_ratio = MAX_PHOTO_HEIGHT/$height;
}
// calculate the scaling ratio for the thumbnail
if ($width <= MAX_THUMB_WIDTH && $height <= MAX_THUMB_HEIGHT)
{
$thumb_ratio = 1;
}
elseif ($width > $height)
{
$thumb_ratio = MAX_THUMB_WIDTH/$width;
}
else
{
$thumb_ratio = MAX_THUMB_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
// move the temporary file to the upload folder
$photo_moved = move_uploaded_file($original, PHOTO_DIR.$_FILES['image']['name']);
$thumb_moved = move_uploaded_file($original, THUMB_DIR.$_FILES['image']['name']);
if ($photo_moved && $thumb_moved)
{
$result = $_FILES['image']['name'].' successfully uploaded; ';
$photo_original = PHOTO_DIR.$_FILES['image']['name'];
$thumb_original = THUMB_DIR.$_FILES['image']['name'];
}
else
{
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
// create an image resource for the original
switch($type)
{
case 1:
$photo_source = @ imagecreatefromgif($photo_original);
$thumb_source = @ imagecreatefromgif(thumb_original);
if ((!$photo_source) || (!$thumb_source))
{
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$photo_source = imagecreatefromjpeg($photo_original);
$thumb_source = imagecreatefromjpeg($thumb_original);
break;
case 3:
$photo_source = imagecreatefrompng($photo_original);
$thumb_source = imagecreatefrompng($thumb_original);
break;
default:
$photo_source && $thumb_source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if ((!$photo_source) || (!$thumb_source))
{
$result = 'Problem copying original photo.';
}
else
{
// calculate the dimensions of the new large photo
$photo_width = round($width * $ratio);
$photo_height = round($height * $ratio);
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the new large photo
$photo = imagecreatetruecolor($photo_width, $photo_height);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
//create the resized new large photo
imagecopyresampled($photo, $source, 0, 0, 0, 0, $photo_width, $photo_height, $width, $height);
// create the resized thumbnail
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type)
{
case 1:
if (function_exists('imagegif'))
{
$photo_success = imagegif($photo, PHOTO_DIR.$name.'.gif');
$thumb_success = imagegif($thumb, THUMB_DIR.$name.'_thumb.gif');
$photo_name = $name.'.gif';
$thumb_name = $name.'_thumb.gif';
}
else
{
$photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 50);
$thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.gif', 50);
$photo_name = $name.'.jpg';
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
$photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 100);
$thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.jpg', 100);
$photo_name = $name.'.jpg';
$thumb_name = $name.'_thb.jpg';
break;
case 3:
$photo_success = imagepng($photo, PHOTO_DIR.$name.'.png');
$thumb_success = imagepng($thumb, THUMB_DIR.$name.'_thumb.png');
$photo_name = $name.'.png';
$thumb_name = $name.'_thumb.png';
}
if ($photo_success && $thumb_success)
{
$insertSQL = sprintf("INSERT INTO tblmembergalleryphoto (galleryid, photoname, thumbname)
VALUES (%s, %s, %s)",
GetSQLValueString($_SESSION['galleryid'], "int"),
GetSQLValueString($photo_name, "text"),
GetSQLValueString($thumb_name, "text"));
mysql_select_db($database_conndb, $conndb);
$Result1 = mysql_query($insertSQL, $conndb) or die(mysql_error());
$result .= "$photo_name and $thumb_name created and uploaded successfully. Add another photo or Click <a href='home.php'>here</a> to go back to the administrator home page.";
}
else
{
$result .= 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($photo_source);
imagedestroy($thumb_source);
imagedestroy($photo);
imagedestroy($thumb);
}
}
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4)
{
$result = 'No file selected';
}
else
{
$result = "$file cannot be uploaded. Acceptable file types: gif, jpg, png.";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Upload</title>
</head>
<body>
<div align="center">
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo "<p>$result</p>";
}
?>
</div>
<div align="center">
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadphoto" id="uploadphoto">
<p>
<label for="image">Select an Image to Upload to the Gallery:</label>
<br />
<input type="file" name="image" id="image" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload" />
<input type="hidden" name="MM_insert" value="uploadphoto" />
</p>
</form>
</div>
</body>
</html>