/** * Create Thumbnails * * @param str $source_file * @param int $post_id * @param str $thumb_to_generate * @param bool $overwrite_existing * @return Error string on error, TRUE on success * @todo this function is ridiculous, chop it up */ public function CreateThumbs($source_file, $post_id, $thumb_to_generate, $overwrite_existing = false) { // Debug $success = OnePanelDebug::Track('Creating thumbnails: ' . $thumb_to_generate); // Get WordPress' uploads data $wp_uploads_data = wp_upload_dir(); $upload_directory = $wp_uploads_data['path']; if (is_writable($upload_directory)) { OnePanelDebug::Info('Upload dir is writable.'); // Figure out how many thumbs we are generating $actual_thumbs_to_gen = array(); $config_thumbs = OnePanelConfig::GetThumbnailTypes(); if ($config_thumbs == false) { OnePanelDebug::Info('No additional thumbnail types passed from config.'); } // Set up catch all. if ($thumb_to_generate == 'All') { $actual_thumbs_to_gen[] = 'Thumbnail'; foreach ($config_thumbs as $key => &$thumbnail_type) { $actual_thumbs_to_gen[] = $thumbnail_type->GetCustomField(); } } else { // Just the passed thumbnail type. // TODO check that the passed ttg is in the config $actual_thumbs_to_gen[] = $thumb_to_generate; } // Create the thumbs we need. foreach ($actual_thumbs_to_gen as &$custom_field_name) { // Debug OnePanelDebug::Info('Attempting to build thumbnail for ' . $custom_field_name); // Check for an existing thumb $existing = get_post_meta($post_id, $custom_field_name); if (empty($existing)) { $existing = false; } else { OnePanelDebug::Info('Thumb already exists ' . ($overwrite_existing ? 'attempting overwrite' : 'skipping')); } // Dont do anything if overwrite is off and theres an existing thumb if ($existing != false && $overwrite_existing == false) { continue; } // Dims are set differently for the standard thumbnails if ($custom_field_name == 'Thumbnail') { $default_thumbnail_dims = OnePanelConfig::GetThumbnailSize(); $width = $default_thumbnail_dims['Width']; $height = $default_thumbnail_dims['Height']; } // Get the dims for this thumbnail type and try and resize it foreach ($config_thumbs as $key => &$config_thumb) { if ($config_thumb->GetCustomField() == $custom_field_name) { $width = $config_thumb->GetWidth(); $height = $config_thumb->GetHeight(); } } // Can we create the resized image? OnePanelDebug::Info('Attempting to create thumbnail ' . $source_file . ' ' . $width . 'x' . $height); $new_thumbnail_path = image_resize($source_file, $width, $height, true); // TODO this really shouldnt be here, if this is a html returning function if (is_wp_error($new_thumbnail_path) || $new_thumbnail_path == false) { if (is_wp_error($new_thumbnail_path)) { OnePanelDebug::Error($new_thumbnail_path->get_error_message()); } OnePanelDebug::Error('Unable to create thumbnail, moving to next iteration.'); $error = '<div class="popup_no_results"><div class="module_error_stroke">One Panel could not resize the image for ' . $custom_field_name . '. <a href="javascript:;" onclick="op_admin.Thumbnails.SwitchMode(\'tool\')">Please try another.</a></div></div>'; continue; } else { OnePanelDebug::Info('Thumbnail created successfully with path ' . $new_thumbnail_path); } // Get the url for the one we just created $new_thumbnail_url = str_replace(ABSPATH, get_option('siteurl') . '/', $new_thumbnail_path); // Add the custom field to the post if ($existing && $overwrite_existing == true) { delete_post_meta($post_id, $custom_field_name); } add_post_meta($post_id, $custom_field_name, $new_thumbnail_url); OnePanelDebug::Info('Custom field added with' . $new_thumbnail_url); } // Prepare the return value if (isset($error)) { $return = $error; } else { $return = true; } } else { // Upload path is not writable $return = '<div class="popup_no_results"><div class="module_error_stroke">The image path is not currently writable. Please chmod the directory first.</div></div>'; } $success->Affirm(); return $return; }