Thank you for using Additional image sizes!Go here to add image sizes and to regenerate copies of previously uploaded images.
';
delete_option('ais_activated');
}
}
/**
* This function is hooked to intermediate_image_sizes and makes sure that the sizes defined
* by this plugin get added to the blog options. Because of this, WP will automatically
* use the sizes defined by this plugin in various situations
*
* @package ais
* @param array $sizes
* @return array
* @uses function ais_get_user_sizes
*/
function ais_intermediate_image_sizes($sizes) {
$ais_sizes = ais_get_user_sizes();
if (!empty($ais_sizes)) {
foreach ($ais_sizes as $key => $value) {
$sizes[] = $key;
update_option("{$key}_size_w", $value['size_w']);
update_option("{$key}_size_h", $value['size_h']);
update_option("{$key}_crop", $value['crop']);
}
}
return $sizes;
}
/**
* This function gets the user defined image sizes from the blog options
*
* @return array
*/
function ais_get_user_sizes() {
return get_option('ais_sizes');
}
/**
* This function returns WordPress' predefined image sizes
*
* @return array
*/
function ais_get_reserved_sizes() {
return array('thumbnail' => 'thumbnail', 'medium' => 'medium', 'large' => 'large');
}
/**
* This function is almost a replica of image_size_input_fields() which is found in
* wp-admin/includes/media.php. It is hooked onto attachment_fields_to_edit. Unfortunately
* WordPress doesn't provide an easy way to filter the data in the function
* image_attachment_fields_to_edit itself, so we append the html from image_size_input_field().
*
* @package ais
* @param string $form_fields
* @param object $post
* @return string
*/
function ais_attachment_fields($form_fields, $post) {
$size_names = array();
$ais_user_sizes = ais_get_user_sizes();
if (is_array($ais_user_sizes)) {
foreach($ais_user_sizes as $key => $value) {
$size_names[$key] = $key;
}
}
foreach ( $size_names as $size => $name ) {
$downsize = image_downsize($post->ID, $size);
// is this size selectable?
$enabled = ( $downsize[3] || 'full' == $size );
$css_id = "image-size-{$size}-{$post->ID}";
// if this size is the default but that's not available, don't select it
if ( $checked && !$enabled )
$checked = '';
// if $checked was not specified, default to the first available size that's bigger than a thumbnail
if ( !$checked && $enabled && 'thumbnail' != $size )
$checked = $size;
$html = "
ID][image-size]' id='{$css_id}' value='{$size}'".( $checked == $size ? " checked='checked'" : '') ." />";
$html .= "";
// only show the dimensions if that choice is available
if ( $enabled )
$html .= " ";
$html .= '
';
$out .= $html;
}
$form_fields['image-size']['html'] .= $out;
return $form_fields;
}
/**
* This echoes the HTML for the admin page
*/
function ais_display_admin() {
$messages = ais_handle_post();
?>
Additional image sizes
Something(s) went wrong:
That went quite well:
Regenerate intermediate size images
Creating an additional image size means that for any new image you upload, a copy of this additional size will be created. If you'd like, this plugin can also create such a copy for any existing images. Click the button below to do this:
$_POST['ais_size_w'],
'size_h' => $_POST['ais_size_h'],
'crop' => $_POST['ais_size_crop']
);
update_option('ais_user_sizes', $ais_user_sizes);
$messages['succes'][] = 'An additional image size named ' . $_POST['ais_size_name'] . ' was added.';
}
// Did the user want to delete any sizes? Then let's do so.
if (isset($_POST['ais_size_delete'])) {
// If none of size_name, height and width were set, but delete IS set, the
// user just wanted to delete something. No need to bug him with validation errors.
if (isset($messages['errors']['width_height']) && isset($messages['errors']['size_name'])) {
// But if crop was set, maybe the user wanted to add a size anyway?
if (isset($_POST['ais_size_crop']) && $_POST['ais_size_crop'] == 1) { /* Do nothing */ }
else unset($messages['errors']);
}
foreach ($_POST['ais_size_delete'] as $delete) {
// $delete holds the size name
unset($ais_user_sizes[$delete]);
$messages['succes'][] = "The size named $delete was deleted";
}
update_option('ais_user_sizes', $ais_user_sizes);
$ais_user_sizes = get_option('ais_sizes');
}
// When no new size was entered and no existing size was deleted we don't have to send any
// validation errors.
if (isset($messages['errors']['width_height']) && isset($messages['errors']['size_name']) && !isset($_POST['ais_size_delete'])) {
if (isset($_POST['ais_size_crop']) && $_POST['ais_size_crop'] == 1) {
// Crop was set, maybe the user wanted to add a size anyway?
}
else {
unset($messages['errors']);
$messages['errors'][] = 'Nothing added, nothing deleted.';
}
}
return $messages;
}
/**
* This function looks for images that don't have copies of the sizes defined by this plugin, and
* then tries to make those copies. It returns an array of messages divided into error messages and
* succes messages. It only makes copies for 10 images at a time.
*
* @package ais
* @return array
* @uses function ais_get_images
*/
function ais_regenerate_images() {
// This can take a while, so we'll keep track of execution time to exit prematurely when
// it gets too high.
$start = strtotime('now');
$max_execution_time = ini_get('max_execution_time');
// Let's divide that max_execution_time by 2 just to be on the safe side (we don't know
// when WordPress started to run so $now is off as well).
$max_execution_time = $max_execution_time / 2;
$messages = array();
$images = ais_get_images();
$sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium', 'large'));
$basedir = wp_upload_dir();
$basedir = $basedir['basedir'];
foreach ($images as $image) {
$metadata = wp_get_attachment_metadata($image->ID);
$file = get_post_meta($image->ID, '_wp_attached_file', true);
foreach ($sizes as $size) {
$now = strtotime('now');
$current_execution_time = $now - $start;
if ($max_execution_time - $current_execution_time < 2) {
break;
}
if (!isset($metadata['sizes'][$size])) {
$image_path = $basedir . '/' . $file;
$result = image_make_intermediate_size(
$image_path, get_option("{$size}_size_w"),
get_option("{$size}_size_h"),
get_option("{$size}_crop")
);
if ($result) {
$metadata['sizes'][$size] = array(
'file' => $result['file'], 'width' => $result['width'], 'height' => $result['height']
);
wp_update_attachment_metadata($image->ID, $metadata);
$messages['succes'][] = 'Resized ' . $image->post_title . ' to size ' . $size . '.';
}
else {
$result = image_resize(
$image_path, get_option("{$size}_size_w"),
get_option("{$size}_size_h"),
get_option("{$size}_crop")
);
if (is_array($result->errors)) {
foreach ($result->errors as $key => $value) {
$messages['errors'][] = $key . ': ' . $value[0];
}
}
}
}
else {
// $messages['succes'][] = $size . ' already exists for ' . $image->post_title . '. Skipped.';
}
}
$now = strtotime('now');
$current_execution_time = $now - $start;
if ($max_execution_time - $current_execution_time < 2) {
$messages['succes'][] = 'Not quite finished yet. We had to stop the script midway because it had been running for too long. Just press the button again to continue where we left off.';
break;
}
}
if (empty($messages)) $messages['succes'][] = 'All is well, no new copies needed to be created.';
return $messages;
}
/**
* Gets an array of images uploaded on the blog. Returns false when no result was found
*
* @return array | bool
*/
function ais_get_images() {
/* Get attachments */
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if (empty($attachments)) return false;
else return $attachments;
}
?>