4 / 3 )
return array(128, (int) ($height / $width * 128));
else
return array((int) ($width / $height * 96), 96);
}
switch($action) {
case 'delete':
wp_delete_attachment($attachment);
header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&start=$start");
die;
case 'save':
$overrides = array('action'=>'save');
$file = wp_handle_upload($_FILES['image'], $overrides);
if ( isset($file['error']) )
die($file['error'] . 'Back to Image Uploading');
$url = $file['url'];
$file = $file['file'];
$filename = basename($file);
// Construct the attachment array
$attachment = array(
'post_title' => $imgtitle ? $imgtitle : $filename,
'post_content' => $descr,
'post_status' => 'attachment',
'post_parent' => $post,
'post_mime_type' => $_FILES['image']['type'],
'guid' => $url
);
// Save the data
$id = wp_insert_attachment($attachment, $file, $post);
// Generate the attachment's postmeta.
$imagesize = getimagesize($file);
$imagedata['width'] = $imagesize['0'];
$imagedata['height'] = $imagesize['1'];
list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
$imagedata['file'] = $file;
$imagedata['thumb'] = "thumb-$filename";
add_post_meta($id, '_wp_attachment_metadata', $imagedata);
if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
$error = wp_create_thumbnail($file, 128);
elseif ( $imagedata['height'] > 96 )
$error = wp_create_thumbnail($file, 96);
}
header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&last=true");
die;
case 'upload':
$current_1 = ' class="current"';
$back = $next = false;
break;
case 'view':
// How many images do we show? How many do we query?
$num = 5;
$double = $num * 2;
if ( $post && empty($all) ) {
$and_post = "AND post_parent = '$post'";
$current_2 = ' class="current"';
} else {
$current_3 = ' class="current"';
}
if ( $last )
$start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment' AND left(post_mime_type, 5) = 'image' $and_post") - $num;
else
$start = (int) $start;
if ( $start < 0 )
$start = 0;
if ( '' == $sort )
$sort = "post_date_gmt DESC";
$images = $wpdb->get_results("SELECT ID, post_date, post_title, guid FROM $wpdb->posts WHERE post_status = 'attachment' AND left(post_mime_type, 5) = 'image' $and_post ORDER BY $sort LIMIT $start, $double", ARRAY_A);
if ( count($images) == 0 ) {
header("Location: ".basename(__FILE__)."?post=$post&action=upload");
die;
} elseif ( count($images) > $num ) {
$next = $start + count($images) - $num;
} else {
$next = false;
}
if ( $start > 0 ) {
$back = $start - $num;
if ( $back < 1 )
$back = '0';
} else {
$back = false;
}
$uwidth_sum = 0;
$images_html = '';
$images_style = '';
$images_script = '';
if ( count($images) > 0 ) {
$images = array_slice( $images, 0, $num );
$__delete = __('Delete');
$__attachment_on = __('Link to Page');
$__attachment_off = __('Link to Image');
$__thumbnail_on = __('Use Thumbnail');
$__thumbnail_off = __('Use Full Image');
$__no_thumbnail = __('No Thumbnail');
$__close = __('Close Options');
$__confirmdelete = __('Delete this photo from the server?');
$__nothumb = __('There is no thumbnail associated with this photo.');
$images_script .= "attachmenton = '$__attachment_on';\nattachmentoff = '$__attachment_off';\n";
$images_script .= "thumbnailon = '$__thumbnail_on';\nthumbnailoff = '$__thumbnail_off';\n";
foreach ( $images as $key => $image ) {
$attachment_ID = $image['ID'];
$meta = get_post_meta($attachment_ID, '_wp_attachment_metadata', true);
if (!is_array($meta)) {
$meta = get_post_meta($attachment_ID, 'imagedata', true); // Try 1.6 Alpha meta key
if (!is_array($meta)) {
continue;
} else {
add_post_meta($attachment_ID, '_wp_attachment_metadata', $meta);
}
}
$image = array_merge($image, $meta);
if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) {
$src = str_replace(basename($image['guid']), '', $image['guid']) . $image['thumb'];
$images_script .= "src".$attachment_ID."a = '$src';\nsrc".$attachment_ID."b = '".$image['guid']."';\n";
$thumb = 'true';
$thumbtext = $__thumbnail_on;
} else {
$src = $image['guid'];
$thumb = 'false';
$thumbtext = $__no_thumbnail;
}
list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']);
$height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"';
$uwidth_sum += 128;
$xpadding = (128 - $image['uwidth']) / 2;
$ypadding = (96 - $image['uheight']) / 2;
$images_style .= "#target{$attachment_ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
$href = get_attachment_link($attachment_ID);
$images_script .= "href{$attachment_ID}a = '$href';\nhref{$attachment_ID}b = '{$image['guid']}';\n";
$images_html .= "