Hello,
Here is an OLD script I made for uploading and storing photos. It has everything you need, you will just need to pick through it and then duplicate the resizing part... or copy the original image before resizing it.
Hope this helps
<HTML>
<HEAD>
<TITLE>Uploading Photo...</TITLE>
<STYLE TYPE="text/css">
<!--
H5 {
font-family: "Courier New", Courier, mono;
font-size: 10px;
color: #000000;
}
-->
</STYLE>
</HEAD>
<BODY>
<?
///////////////////////////////////////////////////////////////////////////////////////
// This script is copyright Xeno Jacobyte Designs and shall not be used by any //
// parties without first obtaining explicit permission from Xeno Jacobyte Designs //
// //
// © 2005 XJDesigns http://www.xjdesigns.co.nz //
///////////////////////////////////////////////////////////////////////////////////////
// GET MAX FILE SIZE
$max_size = $_POST['MAX_FILE_SIZE'];
// ECHO START CONSOLE
echo "
<H5>Welcome to XJImage 0.4. Starting process of uploading your image.<BR>
Please stand by...</H5>
<H5>Current user name: $_SERVER[PHP_AUTH_USER]<BR>
Current Date: " . date("d-m-y") . "<BR>
Formatting date...</H5>
<H5>Checking file integrety...</H5>
<H5>";
/*// TEST FOR FILE UPLOAD ATTACK
if (!is_uploaded_file($file)) {
echo "!! ERROR WITH UPLOAD: Possible file upload attack, shutting down !!</H5>";
exit;
}
*/
// TEST EXISTS
echo "File Name: $file_name<BR>Temp Name: $file<BR>";
if ($file == "none") {
echo "!! ERROR WITH UPLOAD: No file to upload !!</H5>";
exit;
}
echo "File ok. Uploading file...</H5>";
// TEST TO SEE IF ZIP FILE WAS UPLOADED AND ACT
if (strstr($file_name, ".zip")) {
// COPY TO ZIP FILE TMP FOLDER
$new_file = "/home/xjdesign/public_html/sos/photos" . $file . "/temp.zip";
$folder = "/home/xjdesign/public_html/sos/photos" . $file . "/";
mkdir($folder);
copy($file, $new_file);
chdir($folder);
shell_exec("unzip temp.zip");
unlink("temp.zip");
$dir = opendir($folder);
while ($name = readdir($dir)) {
if (strstr($name, ".jp") || strstr($name, ".gif")) {
make_img($folder . $name);
unlink($name);
} else {
echo "Ignoring $name - not a supported image<BR>";
if ($name != "." && $name != "..") {
unlink($name);
}
}
}
closedir($dir);
rmdir($folder);
} else {
make_img($file);
}
function make_img($file) {
echo "<H5>";
global $conn;
global $ok;
// TEST TO SEE IF IMAGE IN FILE BOX MEETS CRITERIA
echo "<BR>Image Info...<BR>";
// TEST IMAGE FORMAT
echo "Current File: $file<BR>";
// GET IMAGE SIZE AND DETAILS
$img_info = getimagesize($file);
// TEST WHAT TYPE OF IMAGE IT IS AND ACT
switch ($img_info[2]) {
case "2":
$img = imagecreatefromjpeg($file);
break;
case "1":
$img = imagecreatefromgif($file);
break;
default:
echo "!! ERROR WITH IMAGE TRANSFER: Not supported type - only gif and jpeg</H5>";
return;
}
// TEST FOR RESIZING
if ($img_info[0] > 640) {
$ratio = $img_info[1]/$img_info[0];
$new_w = 640;
$new_h = 640 * $ratio;
} else if ($img_info[1] > 480) {
$ratio = $img_info[0]/$img_info[1];
$new_h = 480;
$new_w = 480 * $ratio;
} else {
$new_w = $img_info[0];
$new_h = $img_info[1];
}
// OUTPUT IMAGE
$new_img = imagecreatetruecolor($new_w, $new_h) or die("new image error");
// RESIZE LOADED IMAGE
imagecopyresized($new_img, $img, 0, 0, 0, 0, $new_w, $new_h, $img_info[0], $img_info[1]) or die("copy image");
imagejpeg($new_img, $file);
// UPLOAD FILE
/*
FOLLOWING ARE THE COLUMNS IN THE PHOTO ALBUM TABLE, onslowimg (ALL REQUIRED)
- photo_id INT 5 AUTO-INCREMENT PRIMARY KEY
- comments TEXT
- date DATE
- event VAR 5
- views INT 5
- okeyed INT 1
*/
// GET LAST ID NUMBER
$get_last_id = "SELECT photo_id FROM sos_photo ORDER BY photo_id DESC LIMIT 1";
$cmd_last_id = mysql_query($get_last_id, $conn) or error("getting last id");
if (mysql_num_rows($cmd_last_id) != "0") {
$last_id = mysql_result($cmd_last_id, "0", "photo_id");
$id = $last_id + 1;
} else {
$id = 1;
}
// CHECK WHETHER TO AUTO OK
if (isset($_POST['cm_pass']) && $_POST['cm_user'] && $_POST['cm_pass'] == "3856sos" && $_POST['cm_user'] == "sos@xjdesigns.co.nz") {
$ok = "1";
} else {
$ok = "0";
}
// CHECK FOR DATE AND GET IF NECESSARY
echo "event: $_POST[event]<BR>";
if (!empty($_POST['event'])) {
$get_date_cmd = "SELECT date FROM sos_events where event_id = '$_POST[event]'";
$get_date = mysql_query($get_date_cmd, $conn) or error("getting event date");
$date = mysql_result($get_date, 0, "date") or error("getting date value");
$add_img = "INSERT INTO sos_photo VALUES ('$id', '$_POST[comments]', '$date', '$_POST[event]', '0', '$ok')";
} else {
// FORMAT DATE
$date = explode("/", $_POST['date']);
$add_img = "INSERT INTO sos_photo VALUES ('$id', '$_POST[comments]', '$date[2]-$date[1]-$date[0]', '$_POST[event]', '0', '$ok')";
}
// INSERT DATA INTO DATABASE
$cmd_img = mysql_query($add_img, $conn) or error("adding image to database");
if ($cmd_img) {
echo mysql_affected_rows() . " photo added to database.<BR>";
}
// COPY PHOTO
$img_loc = "/home/xjdesign/public_html/sos/photos/$id.jpg";
copy($file, $img_loc);
//if (!copy($file, $img_loc)) {
// echo "<H5>!! ERROR WITH UPLOAD: Photo could not be copied into directory !!</H5>";
// require_once ("photo board add.php");
// exit;
//}
echo "Your photo has been uploaded to the server.<BR>
It will not show up until it has been looked at first.";
// SHOW PHOTO
echo "<IMG SRC=\"photos/$id.jpg\">";
echo "</H5>";
}
// END
if ($ok == "0") {
mail("photo_sos@xjdesigns.co.nz", "New Photo Added", "A new photo has been added to the photo board.\n The Comments are: $_POST[comment] \n http://www.xjdesigns.co.nz/sos/photos/$id.jpg to look at");
}
echo "<H5>Sending Alert</H5>";
echo "<H5>XJImage process complete.<BR>
Click <A HREF=\"gallery.php\">here</a> to go to the photo board page.</H5>";
?>
</BODY>
</HTML>