I am having a problem with an upload/resize script.
I found this in one of the threads on the topic in our forum.
http://phpbuilder.com/board/showthread.php?t=10316743&highlight=image+upload+resize
$propertyname = $_POST['propertyname'];
$message1 = NULL;
$message2 = NULL;
$message3 = NULL;
$idir = "../images/full/"; // Path To Images Directory
$tdir = "../images/thumbnails/"; // Path To Thumbnails Directory
$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "125"; // Maximum Height For Thumbnail Images
if(isset($_POST['propertyname'])) {
if ($_FILES['picture']['error'] == UPLOAD_ERR_OK) {
require_once('../Connections/RogerSolem.php');
$query = "SELECT propertyname FROM rentals ORDER BY propertyname DESC LIMIT 1";
$result = @mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$propertyname = $row[0];
$extension = explode ('.', $_FILES['picture']['name']);
$ext = $extension[1];
$uid = mysql_insert_id();
$url = $uid.'.'.$ext; // Set $url To Equal The Filename For Later Use
$size = ($_FILES['picture']['size'] / 1024);
if ($_FILES['picture']['size'] < 131072) {
if ($_FILES['picture']['type'] == "image/jpg" || $_FILES['picture']['type'] == "image/jpeg" || $_FILES['picture']['type'] == "image/pjpeg") { //Handle JPEG
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
}
else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
if ($_FILES['picture']['type'] == "image/gif") { //Handle GIF
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefromgif("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagegif($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
} else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
if($_FILES['picture']['type'] == "image/png" || $_FILES['picture']['type'] == "image/x-png") { //Handle PNG
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefrompng("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagepng($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
} else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
$message1 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> Incorrect file type <font color=\"#8DA900\">.$ext</font>.
Please upload .jpeg, .gif , or .png files only."; // Error Message If Filetype Is Wrong
}
}
}
} else {
$message3 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> File size <font color=\"#8DA900\">$size KB</font>
is too large. Size Limit: 128KB.<br><br>"; // Error Message If File is over the size limit
}
}
}
$query3 = "UPDATE rentals SET image = '..images/full/$url' WHERE propertyname = '$propertyname'";
$result3 = @mysql_query($query3);
$query6 = "UPDATE rentals SET thumbnail = '../images/thumbnails/$url' WHERE propertyname = '$propertyname'";
$result6 = @mysql_query($query6);