Upload & resizing
Posted: Sat Feb 07, 2004 9:52 am
I am using the following script to upload 2 files, one a zip the other an image file. The upload works perfectly but, i am now trying to resize the image uploaded. The image does get resized but the new file is created in the root directory instead of the uploads directory, also the orginal file is also being uploaded which is not needed. How can I change this so the the original file is deleted and the new resized image is created in the uploads directory.
Code: Select all
<?php
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
// Get the upload file array
// and upload them to a destination
// by looping through that array
// An array of allowed file types
$file_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/x-png' => 'png',
'application/x-zip-compressed' => 'zip'
);
// Get the variables from previous form
$up_files = $_FILES['filename'];
// set the while counter
$counter = 0;
// Start the file upload to server
while($counter <= count($up_files)) {
if($up_files['size'][$counter] > 0) {
// Read the mime type of current file
$filetype = $up_files['type'][$counter];
$filename = $up_files['name'][$counter];
$tempname = $up_files['tmp_name'][$counter];
if (!array_key_exists($filetype, $file_types)) {
print "<font color='#990000'>File <b>$filename</b> of type <b>$filetype</b> is not valid for upload!</font><br />";
} else {
$upload_file = $upload_dir . $filename;
// THIS UPLOADS THE FILE!!!!
move_uploaded_file($tempname, $upload_file);
// Print the uploaded file
// print "<font color='#009900'>File <b>$filename</b> uploaded...
// new location: <b>$upload_file</b></font><br /><br />";
// print_r($images);
}
}
$counter++;
}
$thumb = $up_files['name'][0];
//echo "<br>Uploaded Filename = $thumb<br>";
//echo "<br>New Path = $upload_dir$thumb";
// Set a few variables
$image = $upload_dir.$thumb;
$newimage = "new22.jpg";
$image_quality = 80;
$addborder = 1;
$max_height = 225;
$max_width = 300;
// Main code
$src_img = ImageCreateFromJpeg($image);
$orig_x = ImageSX($src_img);
$orig_y = ImageSY($src_img);
$new_y = $max_height;
$new_x = $orig_x/($orig_y/$max_height);
if ($new_x > $max_width) {
$new_x = $max_width;
$new_y = $orig_y/($orig_x/$max_width);
}
$dst_img = ImageCreateTrueColor($new_x,$new_y);
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y);
if ($addborder == 1) {
// Add border
$black = ImageColorAllocate($dst_img, 0, 0, 0);
ImageSetThickness($dst_img, 1);
ImageLine($dst_img, 0, 0, $new_x, 0, $black);
ImageLine($dst_img, 0, 0, 0, $new_y, $black);
ImageLine($dst_img, $new_x-1, 0, $new_x-1, $new_y, $black);
ImageLine($dst_img, 0, $new_y-1, $new_x, $new_y-1, $black);
}
ImageJpeg($dst_img, $newimage, $image_quality);
ImageDestroy($src_img);
ImageDestroy($dst_img);
?>