/** * processes one uploaded file. Creates folders, moves files * * @global type $UNC_GALLERY * @param type $i * @param type $overwrite * @return boolean */ function unc_uploads_process_file($i, $overwrite) { global $UNC_GALLERY; if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace(__FUNCTION__, func_get_args()); } $action = false; //$_FILES(1) { // ["userImage"]=> array(5) { // ["name"]=> array(1) { [0]=> string(23) "2013-11-02 21.00.38.jpg" } // ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } // ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phptgNK2k" } // ["error"]=> array(1) { [0]=> int(0) } // ["size"]=> array(1) { [0]=> int(213485) } // } //} // the FILE array from the server if (isset($UNC_GALLERY['import'])) { $type = 'import'; $F = $UNC_GALLERY['import']; } else { $type = 'upload'; $F = $UNC_GALLERY['upload_files']; } // get the current path of the temp name if ($type == 'upload' && is_uploaded_file($F['tmp_name'][$i])) { $sourcePath = $F['tmp_name'][$i]; } else { if ($type == 'import' && is_file($F['tmp_name'][$i])) { $sourcePath = $F['tmp_name'][$i]; } else { return array('date' => false, 'action' => "Cannot find uploaded file {$F['tmp_name'][$i]}!"); } } // is there an error with the file? if ($F["error"][$i] > 0) { return array('date' => false, 'action' => "Unable to read the file, upload cancelled of file " . $F['name'][$i]); } // if there is an imagesize, we have a valid image $image_check = getimagesize($F['tmp_name'][$i]); if (!$image_check) { return array('date' => false, 'action' => "Not image file, upload cancelled of file " . $F['name'][$i]); } // let's set variables for the currently uploaded file so we do not have to get the same data twice. $UNC_GALLERY['upload_file_info'] = array('image_size' => $image_check, 'temp_name' => $F['tmp_name'][$i], 'type' => $type); $original_width = $image_check[0]; $original_height = $image_check[1]; // let's make sure the image is not 0-size if ($original_width == 0 || $original_height == 0) { echo unc_display_errormsg("Image size {$F['name'][$i]} = 0"); return false; } // let's shrink only if we need to if ($original_width == $UNC_GALLERY['thumbnail_height'] && $original_height == $UNC_GALLERY['thumbnail_height']) { return array('date' => false, 'action' => "Image size {$F['name'][$i]} is smaller than thumbnail!"); } // get imagetype $exif_imagetype = $image_check[2]; if (!$exif_imagetype) { return array('date' => false, 'action' => "Could not determine image type of file " . $F['name'][$i] . ", upload cancelled!"); } $UNC_GALLERY['upload_file_info']['exif_imagetype'] = $exif_imagetype; // get mime-type and check if it's in the list of valid ones $mime_type = image_type_to_mime_type($exif_imagetype); if (!isset($mime_type, $UNC_GALLERY['valid_filetypes'])) { return array('date' => false, 'action' => "Invalid file type :" . $F["type"][$i]); } else { // get extension for optional resize $extension = $UNC_GALLERY['valid_filetypes'][$mime_type]; } $UNC_GALLERY['upload_file_info']['extension'] = $extension; // we set the new filename of the image including extension so there is no guessing $file_no_ext = pathinfo($F['name'][$i], PATHINFO_FILENAME); $target_filename = $file_no_ext . "." . $extension; $UNC_GALLERY['upload_file_info']['target_filename'] = $target_filename; // we need the exif date to know when the image was taken $date_str = unc_image_date($sourcePath); if (!$date_str) { return array('date' => false, 'action' => "Cannot read EXIF or IPCT of file {$sourcePath}"); } $UNC_GALLERY['upload_file_info']['date_str'] = $date_str; $date_check = date_create($date_str); if (!$date_check) { return array('date' => false, 'action' => "'{$date_str}' is invalid date in EXIF or IPCT"); } // echo "File date is $date_str"; // create all the by-day folders $date_obj = unc_date_folder_create($date_str); // if it failed return back if (!$date_obj) { return array('date' => false, 'action' => "Could not create date folders!"); } // get the upload directory $dirPath = $UNC_GALLERY['upload_path']; // let's make the path with system-specific dir. separators $format = implode("/", array('Y', 'm', 'd')); $date_str_folder = $date_obj->format($format); // echo "Folder date is $date_str_folder<br>"; $target_subfolder = $dirPath . "/" . $UNC_GALLERY['photos'] . "/" . $date_str_folder; $thumb_subfolder = $dirPath . "/" . $UNC_GALLERY['thumbnails'] . "/" . $date_str_folder; $new_path = $target_subfolder . "/" . $target_filename; $new_thumb_path = $thumb_subfolder . "/" . $target_filename; // act on overwrite options if ($overwrite == 'new' && file_exists($new_path)) { return array('date' => false, 'action' => "skipped file {$target_filename}, already exists<br>"); } else { if ($overwrite == 'existing' && !file_exists($new_path)) { return array('date' => false, 'action' => "skipped file {$target_filename}, is new<br>"); } else { if ($overwrite == 'existing' && file_exists($new_path)) { unlink($new_path); $action = 'overwritten'; } else { if ($overwrite == 'all' && file_exists($new_path)) { unlink($new_path); $action = 'overwritten'; } } } } // finally, move the file if ($UNC_GALLERY['picture_long_edge'] > 0) { $resize_check = unc_import_image_resize($F['tmp_name'][$i], $new_path, $UNC_GALLERY['picture_long_edge'], $extension, $UNC_GALLERY['image_quality'], 'max_height'); if (!$resize_check) { return array('date' => false, 'action' => "Could not resize {$F['name'][$i]} from {$F['tmp_name'][$i]} to {$new_path}"); } } else { if ($type == 'upload') { $rename_chk = move_uploaded_file($F['tmp_name'][$i], $new_path); } else { // import $rename_chk = copy($F['tmp_name'][$i], $new_path); } if (!$rename_chk) { return array('date' => false, 'action' => "Could not move {$F['name'][$i]} from {$F['tmp_name'][$i]} to {$new_path}"); } } // chmod file to make sure it cannot be executed $check_chmod = chmod($new_path, 0644); if (!$check_chmod) { return array('date' => false, 'action' => "Could not chmod 644 file {$new_path}"); } // now make the thumbnail $thumb_format = $UNC_GALLERY['thumbnail_format']; $check = unc_import_image_resize($new_path, $new_thumb_path, $UNC_GALLERY['thumbnail_height'], $UNC_GALLERY['thumbnail_ext'], $UNC_GALLERY['thumbnail_quality'], $thumb_format); if (!$check) { return array('date' => false, 'action' => "Could not create the thumbnail for {$F['tmp_name'][$i]} / {$new_thumb_path}!"); } else { if (!$action) { $action = 'written'; } } $check_xmp = unc_image_info_write($new_path); if (!$check_xmp) { return array('date' => false, 'action' => "Could not write XMP/IPCT/EXIF data to file"); } return array('date' => $date_str, 'action' => $target_filename . ": " . $action); }
/** * function to re-build all thumbnails * @global type $UNC_GALLERY */ function unc_gallery_admin_rebuild_thumbs() { global $UNC_GALLERY; if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace(__FUNCTION__, func_get_args()); } ob_clean(); if (!current_user_can('manage_options') || !is_admin()) { echo "Cannot rebuild Thumbs, you are not admin!"; wp_die(); } $dirPath = $UNC_GALLERY['upload_path']; // cleanup empty folders first unc_tools_folder_delete_empty($dirPath); $thumb_root = $dirPath . "/" . $UNC_GALLERY['thumbnails']; // iterate all image folders $photo_folder = $dirPath . "/" . $UNC_GALLERY['photos']; // delete all thumbnails unc_tools_recurse_files($thumb_root, 'unlink', 'rmdir'); $process_id = filter_input(INPUT_POST, 'process_id'); unc_tools_progress_update($process_id, "Cleared existing thumbnails"); $target_folders = unc_tools_recurse_folders($photo_folder); unc_tools_progress_update($process_id, "Got a list of all folders"); if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace('target folders:', $target_folders); } // create thumbnaisl foreach ($target_folders as $date => $folder) { // construct the thumb folder where we put the thumbnails $thumb_folder = $thumb_root . "/" . $date; $text = "Processing {$date}: "; unc_date_folder_create($date); // enumerate all the files in the source folder foreach (glob($folder . "/*") as $image_file) { if (!is_dir($image_file)) { $filename = basename($image_file); $thumb_filename = $thumb_folder . "/" . $filename; unc_import_image_resize($image_file, $thumb_filename, $UNC_GALLERY['thumbnail_height'], $UNC_GALLERY['thumbnail_ext'], $UNC_GALLERY['thumbnail_quality'], $UNC_GALLERY['thumbnail_format']); $text .= "."; } } unc_tools_progress_update($process_id, $text); } unc_tools_progress_update($process_id, "Done!"); wp_die(); }