Topic: Multiple images with thumbnails (Read 992 times)
I crack Photoshop!
Posts: 3
22 credits Members referred : 0
« on: Oct 26, 2006, 02:00:37 AM »
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 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>
I crack Photoshop!
Posts: 3
22 credits Members referred : 0
« Reply #2 on: Oct 26, 2006, 10:40:38 AM »
ok did this just b4 the form section:
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") { foreach ($_FILES as $key => $val) { $foto_upload->the_temp_file = $_FILES[$key]['tmp_name']; $foto_upload->the_file = $val; $foto_upload->http_error = $_FILES[$key]['error']; $foto_upload->replace = "y"; $foto_upload->do_filename_check = "n"; if ($foto_upload->upload()) { $foto_upload->process_image(false, true, true, 80); $foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto" } } }
and now get an error (Notice: Array to string conversion.....) regard wht appears to be here...
function get_extension($from_file) { $ext = strtolower(strrchr($from_file,".")); return $ext; } in the upload_class.php which i changed nothing in
« Last Edit: Oct 26, 2006, 11:24:44 AM by KillinSprE »
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6440
39464 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Oct 26, 2006, 11:39:16 AM »
I have the php code always before all html, try this the error look to me that you form is wrong... post this here too. and check how I use html and php in files