KillinSprE
Thu 26 October 2006, 01:00 am GMT +0200
Love the script, thanks. I used the single file upload and multiple file upload and like both...but is there a way to combine the features so I get thumbnails from the multiple file uploads? I've tried combiing the functions of both foto_upload.php and multiple_upload_example.php to get the script to create a thumb for each of the multiple file uploads with no luck. End result I'm hoping for is that a user can upload 10 pix at once (which works of course), rename each (which also works already) and create a thumbnail of each with same name saved to a thumb directory.
EG.
user uploads a.jpg,b.jpg,c.jpg,d.jpg
script does following:
saves a.jpg as PHOTOS/USER/*A's timestamp*.jpg
saves thumbnail of a.jpg as PHOTOS/USER/thumb/*A's timestamp*.jpg
saves b.jpg as PHOTOS/USER/*B's timestamp*.jpg
saves thumbnail of b.jpg as PHOTOS/USER/thumb/*B's timestamp*.jpg
saves c.jpg as PHOTOS/USER/*C's timestamp*.jpg
saves thumbnail of c.jpg as PHOTOS/USER/thumb/*C's timestamp*.jpg
saves d.jpg as PHOTOS/USER/*D's timestamp*.jpg
saves thumbnail of d.jpg as PHOTOS/USER/thumb/*D's timestamp*.jpg
Here's what I've got so far that isnt being cooperative (clicking submit DOES bring it back to this script so ignore the ACTION in the form)
<?
function UploadPhotos($id)
{
include "include/upload_class.php";
set_time_limit(60);
$userdir=10000+$id;
//error_reporting(E_ALL);
$max_size = 1024*100; // the max. size for uploading
class muli_files extends file_upload {
var $number_of_files = 0;
var $names_array;
var $tmp_names_array;
var $error_array;
var $wrong_extensions = 0;
var $bad_filenames = 0;
var $x_size;
var $y_size;
var $x_max_size = 300;
var $y_max_size = 200;
var $x_max_thumb_size = 110;
var $y_max_thumb_size = 88;
var $thumb_folder;
var $foto_folder;
var $larger_dim;
var $larger_curr_value;
var $larger_dim_value;
var $larger_dim_thumb_value;
var $use_image_magick = true;
function process_image($landscape_only = false, $create_thumb = false, $delete_tmp_file = false, $compression = 85)
{
$filename = $this->upload_dir.$this->file_copy;
$this->check_dir($this->thumb_folder); // run these checks to create not existing directories
$this->check_dir($this->foto_folder); // the upload dir is created during the file upload (if not already exists)
$thumb = $this->thumb_folder.$this->file_copy;
$foto = $this->foto_folder.$this->file_copy;
if ($landscape_only) {
$this->get_img_size($filename);
if ($this->y_size > $this->x_size) {
$this->img_rotate($filename, $compression);
}
}
$this->check_dimensions($filename); // check which size is longer then the max value
if ($this->larger_curr_value > $this->larger_dim_value) {
$this->thumbs($filename, $foto, $this->larger_dim_value, $compression);
} else {
copy($filename, $foto);
}
if ($create_thumb) {
if ($this->larger_curr_value > $this->larger_dim_thumb_value) {
$this->thumbs($filename, $thumb, $this->larger_dim_thumb_value, $compression); // finally resize the image
} else {
copy($filename, $thumb);
}
}
if ($delete_tmp_file) $this->del_temp_file($filename); // note if you delete the tmp file the check if a file exists will not work
}
function get_img_size($file)
{
$img_size = getimagesize($file);
$this->x_size = $img_size[0];
$this->y_size = $img_size[1];
}
function check_dimensions($filename)
{
$this->get_img_size($filename);
$x_check = $this->x_size - $this->x_max_size;
$y_check = $this->y_size - $this->y_max_size;
if ($x_check < $y_check) {
$this->larger_dim = "y";
$this->larger_curr_value = $this->y_size;
$this->larger_dim_value = $this->y_max_size;
$this->larger_dim_thumb_value = $this->y_max_thumb_size;
} else {
$this->larger_dim = "x";
$this->larger_curr_value = $this->x_size;
$this->larger_dim_value = $this->x_max_size;
$this->larger_dim_thumb_value = $this->x_max_thumb_size;
}
}
function img_rotate($wr_file, $comp)
{
$new_x = $this->y_size;
$new_y = $this->x_size;
if ($this->use_image_magick) {
exec(sprintf("mogrify -rotate 90 -quality %d %s", $comp, $wr_file));
} else {
$src_img = imagecreatefromjpeg($wr_file);
$rot_img = imagerotate($src_img, 90, 0);
$new_img = imagecreatetruecolor($new_x, $new_y);
imageantialias($new_img, TRUE);
imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);
imagejpeg($new_img, $this->upload_dir.$this->file_copy, $comp);
}
}
function thumbs($file_name_src, $file_name_dest, $target_size, $quality = 80)
{
//print_r(func_get_args());
$size = getimagesize($file_name_src);
if ($this->larger_dim == "x") {
$w = number_format($target_size, 0, ',', '');
$h = number_format(($size[1]/$size[0])*$target_size,0,',','');
} else {
$h = number_format($target_size, 0, ',', '');
$w = number_format(($size[0]/$size[1])*$target_size,0,',','');
}
if ($this->use_image_magick) {
exec(sprintf("convert %s -resize %dx%d -quality %d %s", $file_name_src, $w, $h, $quality, $file_name_dest));
} else {
$dest = imagecreatetruecolor($w, $h);
imageantialias($dest, TRUE);
$src = imagecreatefromjpeg($file_name_src);
imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagejpeg($dest, $file_name_dest, $quality);
}
}
function extra_text($msg_num) {
switch ($this->language) {
case "de":
// add you translations here
break;
default:
$extra_msg[1] = "Error for: <b>".$this->the_file."</b>";
$extra_msg[2] = "You have tried to upload ".$this->wrong_extensions." files with a bad extension, the following extensions are allowed: <b>".$this->ext_string."</b>";
$extra_msg[3] = "Select at least on file.";
$extra_msg[4] = "Select the file(s) for upload.";
$extra_msg[5] = "You have tried to upload <b>".$this->bad_filenames." files</b> with invalid characters inside the filename.";
}
return $extra_msg[$msg_num];
}
// this method checkes the number of files for upload
// this example works with one or more files
function count_files() {
foreach ($this->names_array as $test) {
if ($test != "") {
$this->number_of_files++;
}
}
if ($this->number_of_files > 0) {
return true;
} else {
return false;
}
}
function upload_multi_files () {
$this->message = "";
if ($this->count_files()) {
foreach ($this->names_array as $key => $value) {
if ($value != "") {
$this->the_file = $value;
$new_name = $this->set_file_name();
if ($this->check_file_name($new_name)) {
if ($this->validateExtension()) {
$this->file_copy = $new_name;
$this->the_temp_file = $this->tmp_names_array[$key];
if (is_uploaded_file($this->the_temp_file)) {
if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
$this->message[] = $this->error_text($this->error_array[$key]);
if ($this->rename_file) $this->message[] = $this->error_text(16);
sleep(1); // wait a seconds to get an new timestamp (if rename is set)
}
} else {
$this->message[] = $this->extra_text(1);
$this->message[] = $this->error_text($this->error_array[$key]);
}
} else {
$this->wrong_extensions++;
}
} else {
$this->bad_filenames++;
}
}
}
if ($this->bad_filenames > 0) $this->message[] = $this->extra_text(5);
if ($this->wrong_extensions > 0) {
$this->show_extensions();
$this->message[] = $this->extra_text(2);
}
} else {
$this->message[] = $this->extra_text(3);
}
}
}
$multi_upload = new muli_files;
$multi_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/userpics/"; // "files" is the folder for the uploaded files (you have to create this folder)
$multi_upload->foto_folder = $multi_upload->upload_dir. $userdir . "/";
$multi_upload->thumb_folder = $multi_upload->foto_folder. "thumb/";
$multi_upload->extensions = array(".png", ".jpg", ".gif"); // specify the allowed extensions here
$multi_upload->message[] = $multi_upload->extra_text(4); // a different standard message for multiple files
$multi_upload->rename_file = true; // set to "true" if you want to rename all files with a timestamp value
$multi_upload->x_max_size = 300;
$multi_upload->y_max_size = 200;
$multi_upload->x_max_thumb_size = 120;
$multi_upload->y_max_thumb_size = 150;
$multi_upload->do_filename_check = "y"; // check filename ...
if(isset($_POST['Submit'])) {
$multi_upload->process_image(false, true, true, 80);
$multi_upload->tmp_names_array = $_FILES['upload']['tmp_name'];
$multi_upload->names_array = $_FILES['upload']['name'];
$multi_upload->error_array = $_FILES['upload']['error'];
$multi_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$multi_upload->upload_multi_files();
$multi_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>(multiple) upload example</title>
<style type="text/css">
<!--
label {
width: 80px;
}
input {
margin-bottom:3px;
margin-left:5px;
}
-->
</style>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="main.php?menu_select=MyGallery&display=4">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">
<label for="upload[]">File 1:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 2:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 3:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 4:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 5:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 6:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 7:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 8:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 9:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 10:</label>
<input type="file" name="upload[]" size="30"><br>
<!-- Add here more file fields if you need. -->
Replace files?
<input type="checkbox" name="replace" value="y">
<input type="submit" name="Submit" value="Submit">
</form>
<p><?php echo $multi_upload->show_error_string(); ?></p>
</body>
</html><?
}
?>
EG.
user uploads a.jpg,b.jpg,c.jpg,d.jpg
script does following:
saves a.jpg as PHOTOS/USER/*A's timestamp*.jpg
saves thumbnail of a.jpg as PHOTOS/USER/thumb/*A's timestamp*.jpg
saves b.jpg as PHOTOS/USER/*B's timestamp*.jpg
saves thumbnail of b.jpg as PHOTOS/USER/thumb/*B's timestamp*.jpg
saves c.jpg as PHOTOS/USER/*C's timestamp*.jpg
saves thumbnail of c.jpg as PHOTOS/USER/thumb/*C's timestamp*.jpg
saves d.jpg as PHOTOS/USER/*D's timestamp*.jpg
saves thumbnail of d.jpg as PHOTOS/USER/thumb/*D's timestamp*.jpg
Here's what I've got so far that isnt being cooperative (clicking submit DOES bring it back to this script so ignore the ACTION in the form)
<?
function UploadPhotos($id)
{
include "include/upload_class.php";
set_time_limit(60);
$userdir=10000+$id;
//error_reporting(E_ALL);
$max_size = 1024*100; // the max. size for uploading
class muli_files extends file_upload {
var $number_of_files = 0;
var $names_array;
var $tmp_names_array;
var $error_array;
var $wrong_extensions = 0;
var $bad_filenames = 0;
var $x_size;
var $y_size;
var $x_max_size = 300;
var $y_max_size = 200;
var $x_max_thumb_size = 110;
var $y_max_thumb_size = 88;
var $thumb_folder;
var $foto_folder;
var $larger_dim;
var $larger_curr_value;
var $larger_dim_value;
var $larger_dim_thumb_value;
var $use_image_magick = true;
function process_image($landscape_only = false, $create_thumb = false, $delete_tmp_file = false, $compression = 85)
{
$filename = $this->upload_dir.$this->file_copy;
$this->check_dir($this->thumb_folder); // run these checks to create not existing directories
$this->check_dir($this->foto_folder); // the upload dir is created during the file upload (if not already exists)
$thumb = $this->thumb_folder.$this->file_copy;
$foto = $this->foto_folder.$this->file_copy;
if ($landscape_only) {
$this->get_img_size($filename);
if ($this->y_size > $this->x_size) {
$this->img_rotate($filename, $compression);
}
}
$this->check_dimensions($filename); // check which size is longer then the max value
if ($this->larger_curr_value > $this->larger_dim_value) {
$this->thumbs($filename, $foto, $this->larger_dim_value, $compression);
} else {
copy($filename, $foto);
}
if ($create_thumb) {
if ($this->larger_curr_value > $this->larger_dim_thumb_value) {
$this->thumbs($filename, $thumb, $this->larger_dim_thumb_value, $compression); // finally resize the image
} else {
copy($filename, $thumb);
}
}
if ($delete_tmp_file) $this->del_temp_file($filename); // note if you delete the tmp file the check if a file exists will not work
}
function get_img_size($file)
{
$img_size = getimagesize($file);
$this->x_size = $img_size[0];
$this->y_size = $img_size[1];
}
function check_dimensions($filename)
{
$this->get_img_size($filename);
$x_check = $this->x_size - $this->x_max_size;
$y_check = $this->y_size - $this->y_max_size;
if ($x_check < $y_check) {
$this->larger_dim = "y";
$this->larger_curr_value = $this->y_size;
$this->larger_dim_value = $this->y_max_size;
$this->larger_dim_thumb_value = $this->y_max_thumb_size;
} else {
$this->larger_dim = "x";
$this->larger_curr_value = $this->x_size;
$this->larger_dim_value = $this->x_max_size;
$this->larger_dim_thumb_value = $this->x_max_thumb_size;
}
}
function img_rotate($wr_file, $comp)
{
$new_x = $this->y_size;
$new_y = $this->x_size;
if ($this->use_image_magick) {
exec(sprintf("mogrify -rotate 90 -quality %d %s", $comp, $wr_file));
} else {
$src_img = imagecreatefromjpeg($wr_file);
$rot_img = imagerotate($src_img, 90, 0);
$new_img = imagecreatetruecolor($new_x, $new_y);
imageantialias($new_img, TRUE);
imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);
imagejpeg($new_img, $this->upload_dir.$this->file_copy, $comp);
}
}
function thumbs($file_name_src, $file_name_dest, $target_size, $quality = 80)
{
//print_r(func_get_args());
$size = getimagesize($file_name_src);
if ($this->larger_dim == "x") {
$w = number_format($target_size, 0, ',', '');
$h = number_format(($size[1]/$size[0])*$target_size,0,',','');
} else {
$h = number_format($target_size, 0, ',', '');
$w = number_format(($size[0]/$size[1])*$target_size,0,',','');
}
if ($this->use_image_magick) {
exec(sprintf("convert %s -resize %dx%d -quality %d %s", $file_name_src, $w, $h, $quality, $file_name_dest));
} else {
$dest = imagecreatetruecolor($w, $h);
imageantialias($dest, TRUE);
$src = imagecreatefromjpeg($file_name_src);
imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
imagejpeg($dest, $file_name_dest, $quality);
}
}
function extra_text($msg_num) {
switch ($this->language) {
case "de":
// add you translations here
break;
default:
$extra_msg[1] = "Error for: <b>".$this->the_file."</b>";
$extra_msg[2] = "You have tried to upload ".$this->wrong_extensions." files with a bad extension, the following extensions are allowed: <b>".$this->ext_string."</b>";
$extra_msg[3] = "Select at least on file.";
$extra_msg[4] = "Select the file(s) for upload.";
$extra_msg[5] = "You have tried to upload <b>".$this->bad_filenames." files</b> with invalid characters inside the filename.";
}
return $extra_msg[$msg_num];
}
// this method checkes the number of files for upload
// this example works with one or more files
function count_files() {
foreach ($this->names_array as $test) {
if ($test != "") {
$this->number_of_files++;
}
}
if ($this->number_of_files > 0) {
return true;
} else {
return false;
}
}
function upload_multi_files () {
$this->message = "";
if ($this->count_files()) {
foreach ($this->names_array as $key => $value) {
if ($value != "") {
$this->the_file = $value;
$new_name = $this->set_file_name();
if ($this->check_file_name($new_name)) {
if ($this->validateExtension()) {
$this->file_copy = $new_name;
$this->the_temp_file = $this->tmp_names_array[$key];
if (is_uploaded_file($this->the_temp_file)) {
if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
$this->message[] = $this->error_text($this->error_array[$key]);
if ($this->rename_file) $this->message[] = $this->error_text(16);
sleep(1); // wait a seconds to get an new timestamp (if rename is set)
}
} else {
$this->message[] = $this->extra_text(1);
$this->message[] = $this->error_text($this->error_array[$key]);
}
} else {
$this->wrong_extensions++;
}
} else {
$this->bad_filenames++;
}
}
}
if ($this->bad_filenames > 0) $this->message[] = $this->extra_text(5);
if ($this->wrong_extensions > 0) {
$this->show_extensions();
$this->message[] = $this->extra_text(2);
}
} else {
$this->message[] = $this->extra_text(3);
}
}
}
$multi_upload = new muli_files;
$multi_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/userpics/"; // "files" is the folder for the uploaded files (you have to create this folder)
$multi_upload->foto_folder = $multi_upload->upload_dir. $userdir . "/";
$multi_upload->thumb_folder = $multi_upload->foto_folder. "thumb/";
$multi_upload->extensions = array(".png", ".jpg", ".gif"); // specify the allowed extensions here
$multi_upload->message[] = $multi_upload->extra_text(4); // a different standard message for multiple files
$multi_upload->rename_file = true; // set to "true" if you want to rename all files with a timestamp value
$multi_upload->x_max_size = 300;
$multi_upload->y_max_size = 200;
$multi_upload->x_max_thumb_size = 120;
$multi_upload->y_max_thumb_size = 150;
$multi_upload->do_filename_check = "y"; // check filename ...
if(isset($_POST['Submit'])) {
$multi_upload->process_image(false, true, true, 80);
$multi_upload->tmp_names_array = $_FILES['upload']['tmp_name'];
$multi_upload->names_array = $_FILES['upload']['name'];
$multi_upload->error_array = $_FILES['upload']['error'];
$multi_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$multi_upload->upload_multi_files();
$multi_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>(multiple) upload example</title>
<style type="text/css">
<!--
label {
width: 80px;
}
input {
margin-bottom:3px;
margin-left:5px;
}
-->
</style>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="main.php?menu_select=MyGallery&display=4">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">
<label for="upload[]">File 1:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 2:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 3:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 4:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 5:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 6:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 7:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 8:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 9:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 10:</label>
<input type="file" name="upload[]" size="30"><br>
<!-- Add here more file fields if you need. -->
Replace files?
<input type="checkbox" name="replace" value="y">
<input type="submit" name="Submit" value="Submit">
</form>
<p><?php echo $multi_upload->show_error_string(); ?></p>
</body>
</html><?
}
?>