0) { // positive for pixellated, zero or negative for smooth
if ($quality > 90) { // create intermediate size
$i_factor = 1 - ($quality - floor($quality)); // 90.3333 becomes sharp factor .6666
if ($i_factor == 0) { $i_factor = 1/2; }
$interm_h = floor(($dst_h + $src_h) * $i_factor);
$interm_w = floor(($dst_w + $src_w) * $i_factor);
$interm_image = imagecreatetruecolor($interm_w, $interm_h);
imagecopyresized( $interm_image, $from_image,
$dst_x * $i_factor, $dst_y * $i_factor, $src_x * $i_factor, $src_y * $i_factor,
$interm_w, $interm_h, $src_w, $src_h);
imagecopyresampled( $to_image, $interm_image,
$dst_x, $dst_y, $dst_x * $i_factor, $dst_y * $i_factor,
$dst_w, $dst_h, $interm_w, $interm_h);
imagedestroy($interm_image);
} else {
imagecopyresized( $to_image, $from_image, $dst_x, $dst_y, $src_x, $src_y,
$dst_w, $dst_h, $src_w, $src_h);
}
} else {
imagecopyresampled( $to_image, $from_image, $dst_x, $dst_y, $src_x, $src_y,
$dst_w, $dst_h, $src_w, $src_h);
}
imagedestroy($from_image);
imagejpeg($to_image, $to_file_path, $quality); // Creates file
imagedestroy($to_image);
$attr['pic_'.$prefix] = $to_file;
$attr['pic_'.$prefix.'_path'] = $to_file_path;
$attr['pic_'.$prefix.'_url'] = $to_file_url;
}
}
/* ********************** */
function get_images_from_folder($attr) {
global $post;
$wp_dir = wp_upload_dir();
$pics_info = array();
if (substr($attr['display'],0,1) == '/') {
// Display images from a folder. NOTE: Absolute paths OK and are handled properly by path_join()
$folder = substr($attr['display'],1);
$dir_name = path_join(path_join($wp_dir['basedir'],$folder),'*.{jpg,jpeg,JPG,JPEG,png,PNG,gif,GIF}');
$files = glob($dir_name, GLOB_BRACE);
// Remove local path prefix (folder part remains)
$sorted_files = array();
foreach ($files as $afile) {
$sorted_files[] = str_replace(trailingslashit($wp_dir['basedir']),
'',$afile);
}
// Sort names ('order' is ASC or DESC)
if (strtolower($attr['order']) == 'desc') {
rsort($sorted_files, SORT_STRING);
} elseif (strtolower($attr['order']) == 'rand') {
shuffle($sorted_files);
} else {
sort($sorted_files, SORT_STRING);
}
// Select names from 'include' parameter and build pic_info
if ($attr['include'] != '') {
// split on commas. for each word, first match exact filename; then match suffix.
$included_files = array();
$include_list = explode(',',$attr['include']);
foreach ($include_list as $ifile) {
foreach ($sorted_files as &$afile) {
if (!strlen($afile)) continue; // already used this file
$info = pathinfo($afile);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($afile, ".$ext");
if (is_numeric($ifile)) {
// for "include=7" this will match 'file7.jpg' and '7.jpg' but not 'file17.jpg' or '17.jpg'
$match_string = "#^(.*?\\D)?0*$ifile(-\d+x\d+)?\.\w+\z#";
} else {
$match_string = "#$ifile(-\d+x\d+)?\.\w+\z#"; // match text at end of filename
}
$suffix_match = (preg_match($match_string,$afile));
// Match exact filename, or suffix
if ($ifile == $afile || $suffix_match) {
$included_files[] = $afile;
$afile = ''; // do not consider this file again
}
}
}
$sorted_files = $included_files;
}
$pic_size_info = array(); // Sizes available for each picture
$fullsize_pics = array(); // List of full-size images
$selected_pics = array();
foreach ($sorted_files as $afile) {
if (preg_match('#^(.*?)(?:-(\d+x\d+))?\.\w+\Z#',$afile,$filebits)) {
if ($filebits[2] != '') { // resized image
$pic_size_info[$filebits[1]][$filebits[2]] = $afile;
} else {
$fullsize_pics[$filebits[1]] = $afile;
$selected_pics[] = $filebits[1];
}
}
}
$full_width = get_option('large_size_w'); $full_height = get_option('large_size_h');
// For each full size image:
// I. Find or create thumbnail:
// if cropping, look for exact cropped size
// else get its size and call wp_constrain_dimensions and look for exactly that size
// II. Find or create constrained full-size image
//
foreach ($selected_pics as $apic_key) {
$pic_info = array();
$pic_info['pic_full'] = $fullsize_pics[$apic_key];
$pic_info['pic_full_path'] = trailingslashit($wp_dir['basedir']) . $pic_info['pic_full'];
$pic_info['pic_full_url'] = trailingslashit($wp_dir['baseurl']) . $pic_info['pic_full'];
$orig_size = getimagesize($pic_info['pic_full_path']);
$pic_info['fullwidth'] = $orig_size[0];
$pic_info['fullheight'] = $orig_size[1];
// "Full size" images are actually constrained to size chosen in Admin screen.
// This means huge off-the-camera will actually be resized to, for example, 1024 width.
if (($orig_size[0] > $full_width) || ($orig_size[1] > $full_height)) {
$new_full_size = wp_constrain_dimensions($orig_size[0],$orig_size[1],$full_width,$full_height);
$pic_info['fullwidth'] = $new_full_size[0];
$pic_info['fullheight'] = $new_full_size[1];
$full_size = $new_full_size[0] . 'x' . $new_full_size[1];
$pic_info['crop'] = 0; // always scale these images, never crop
if ($pic_size_info[$apic_key][$full_size] == '') {
// properly sized full image does not exist; create it
resize_crop($pic_info, 'full'); // modifies ['pic_full'], creates ['pic_full_url'] in $pic_info
} else {
$pic_info['pic_full'] = $pic_size_info[$apic_key][$full_size];
$pic_info['pic_full_path'] = trailingslashit($wp_dir['basedir']) . $pic_info['pic_full'];
$pic_info['pic_full_url'] = trailingslashit($wp_dir['baseurl']) . $pic_info['pic_full'];
}
}
// Find or create thumbnail
if ($pic_info['fullwidth'] <= $attr['width'] && $pic_info['fullheight'] <= $attr['height']) {
// requested full size image already qualifies as a thumbnail
$pic_info['thumbwidth'] = $pic_info['fullwidth'];
$pic_info['thumbheight'] = $pic_info['fullheight'];
$pic_info['pic_thumb'] = $pic_info['pic_full'];
} else {
$size_params = image_resize_dimensions($pic_info['fullwidth'], $pic_info['fullheight'],
$attr['width'], $attr['height'], $attr['crop']);
$pic_info['thumbwidth'] = $size_params[4]; // new width and height, whether cropped or scaled-to-fit
$pic_info['thumbheight'] = $size_params[5];
$thumb_size = $pic_info['thumbwidth']. 'x' . $pic_info['thumbheight'];
$pic_info['pic_thumb'] = $pic_size_info[$apic_key][$thumb_size];
}
$pic_info['sharp'] = $attr['sharp'];
if ($pic_info['pic_thumb'] == '') {
// desired thumbnail does not exist; create it
$pic_info['crop'] = $attr['crop'];
resize_crop($pic_info, 'thumb'); // creates ['pic_thumb'], ['pic_thumb_url'] in $pic_info
} else {
$pic_info['pic_thumb_url'] = trailingslashit($wp_dir['baseurl']) . $pic_info['pic_thumb'];
}
$pic_info['image'] = $pic_info['pic_thumb']; // Copy thumbnail properties into image properties
$pic_info['image_url'] = $pic_info['pic_thumb_url'];
$pic_info['width'] = $pic_info['thumbwidth'];
$pic_info['height'] = $pic_info['thumbheight'];
$pic_info['linkto'] = $attr['linkto'];
$pic_info['class'] = $attr['class'].'-image';
$pics_info[] = $pic_info;
}
}
return($pics_info);
}
/* ********************** */
function create_images_for($attr, $pic_fullsize) {
$info = pathinfo($pic_fullsize);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($pic_fullsize, ".$ext");
// Get attachment image from the folder where it was uploaded.
$attr['display'] = "/$dir";
$attr['include'] = $name;
$pic_info = get_images_from_folder($attr);
if (is_array($pic_info)) {
return $pic_info[0]; // should only be one image found
}
return 0; // no image found
}
/* ********************** */
function pic_info_for($attr, $id) {
$attached_pic = get_attached_file($id);
$pic_info['id'] = $id;
if ($attached_pic == '') return; // cannot find the attachment
$pic_info = create_images_for($attr, $attached_pic);
if (is_array($pic_info)) {
$post_info = get_post($id); // the attachment's post
$pic_info['caption'] = $post_info->post_excerpt; // Attachment: Caption
$pic_info['description'] = $post_info->post_content; // Attachment: Description
$pic_info['title'] = $post_info->post_title; // Attachment: Title
$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
$pic_info['attpage'] = get_attachment_link($id);
if(count($alt)) $pic_info['alt_text'] = $alt; // Attachment: Alternate Text
return $pic_info;
}
return;
}
/* ********************** */
function get_images_attached($attr, $pid, $limit) {
global $post;
$wp_dir = wp_upload_dir();
$pics_info = array();
if ($pid == 0) {
$pid = $post->ID;
}
$order = strtolower($attr['order']) == 'desc' ? 'desc' : 'asc';
// NOTE: Possibly use $attr['orderby'] directly at risk of breaking backwards compability
$orderby = strtolower($attr['order']) == 'rand' ? 'rand' : 'menu_order';
// use post_status=inherit to disable finding attachments that are set to draft or private
$attachments = get_children(array('post_parent' => $pid, 'post_status' => 'inherit',
'numberposts' => $limit >= 1 ? $limit : -1,
'post_type' => 'attachment', 'post_mime_type' => 'image',
'post_status' => 'inherit', 'orderby' => $orderby,
'order' => $order));
if (empty ($attachments)) return $pics_info;
foreach ($attachments as $id => $attach_info) {
if ($attach_info->menu_order < -100) continue; // permit disabling images via menu_order
$pic_info = pic_info_for($attr,$id);
if (is_array($pic_info)) {
$pics_info[] = $pic_info;
}
}
return $pics_info;
}
/* ********************** */
function get_attachments($attr, $pid, $limit) {
global $post;
if (function_exists('attachments_get_attachments')) {
if ($pid == 0) {
$pid = $post->ID;
}
$attachments = attachments_get_attachments($pid);
$pic_info = array();
// Title, caption override?
foreach ($attachments as $attach_info) {
$pic_info = pic_info_for($attr,$attach_info['id']);
if (is_array($pic_info)) {
$pics_info[] = $pic_info;
}
}
return $pics_info;
}
}
/* ********************** */
function get_selected_thumbnail ($attr, $pid) {
$pics_info = array();
$tid = 0;
if (function_exists('get_post_thumbnail_id')) { /* since 2.9.0 */
$tid = get_post_thumbnail_id($pid);
}
if ($tid) {
$pic_info = pic_info_for($attr, $tid);
if (is_array($pic_info)) {
$pics_info[] = $pic_info;
return $pics_info;
}
}
return;
}
/* ********************** */
function get_pics_info($attr, $pages) {
$wp_dir = wp_upload_dir(); // ['basedir'] is local path, ['baseurl'] as seen from browser
$disp_pages = array();
$picpages_only = ($attr['display'] == 'list') ? 0 : $attr['pics_only'];
foreach ($pages as $page) {
$pic_info = array();
$ximg = get_post_meta($page->ID, 'subpage_thumb', 1);
if ($ximg != '') { // Specified exact thumbnail image
if ( preg_match( '|^https?://|i', $ximg ) ) {
$pic_info['image_url'] = $ximg; // as explicit URL
} else {
// local file... assume full-size picture given, and automagically create thumbnail
$pic_info = create_images_for($attr, $ximg);
}
} else { // Use selected thumbnail; or first attached image, if any
$pics = get_selected_thumbnail($attr, $page->ID);
if (!is_array($pics)) {
$pics = get_images_attached($attr, $page->ID, 1);
}
if (is_array($pics)) {
$pic_info = $pics[0]; // should be exactly one
}
}
if ((!$picpages_only) || $pic_info['image_url'] != '') {
$pic_info['linkto'] = strlen($attr['linkto'])?$attr['linkto']:'page';
$pic_info['page'] = $page;
switch($pic_info['linkto']) {
case 'pic':
$pic_info['permalink'] = $pic_info['pic_full_url'];
break;
default:
$pic_info['permalink'] = get_permalink($page->ID);
}
$pic_info['excerpt'] = get_post_meta($page->ID, 'subpage_excerpt', 1);
if ($pic_info['excerpt'] == '') $pic_info['excerpt'] = $page->post_excerpt;
$pic_info['title'] = get_post_meta($page->ID, 'subpage_title', 1);
if ($pic_info['title'] == '') $pic_info['title'] = $page->post_title;
$disp_pages[] = $pic_info;
}
}
return $disp_pages;
}
/* ********************** */
function get_subpages ($attr) {
global $post;
$child_pages = explode(',',$attr['postid']);
$my_children = 0;
if ($attr['siblings']) {
if ($post->post_parent) { // children of our parent
$child_pages = array($post->post_parent);
}
if (!$attr['self']) { // add ourselves to exception list
$attr['exclude'] = $post->ID . ($attr['exclude'] !== '' ? ','.$attr['exclude'] : '');
}
} else {
if (!$child_pages[0]) {
$show_on_front = get_option('show_on_front');
$page_on_front = get_option('page_on_front');
if ($show_on_front == 'page' && $page_on_front == $post->ID ) {
$child_pages = array(0, $post->ID); // children of "Main Page (no parent)" and us explicitly
if (!$attr['self']) { // except ourself (will the "real" homepage please stand up)
$attr['exclude'] = $post->ID . ($attr['exclude'] !== '' ? ','.$attr['exclude'] : '');
}
} else {
$child_pages = array($post->ID);
}
$my_children = 1;
}
}
$pages = array();
foreach ($child_pages as $child_of) {
$query = "child_of=$child_of&echo=0&title_li=0&sort_column=" . $attr['orderby'];
if (!$attr['family']) { // Only children of this page
$query .= "&hierarchical=0&parent=".$child_of;
}
if (strlen ($attr['exclude'])) {
$query .= "&exclude=" . $attr['exclude'];
}
$these_pages = & get_pages($query);
if (count ($these_pages)) {
foreach ($these_pages as $subpage) {
array_push($pages, $subpage);
}
} else {
// If specified a different page with no subpages, use that page alone.
// i.e., If listing "my" subpages, and I haven't any, don't list "me."
if ($my_children == 0) {
$these_pages = get_pages("include=$child_of&echo=0&title_li=0");
array_splice($pages, count($pages), 0, $these_pages);
}
}
}
if (count($pages) == 0) {
return;
}
if (strtolower($attr['order']) =='rand') {
shuffle($pages);
} elseif (strtolower($attr['order']) =='desc') {
$pages = array_reverse($pages);
}
return get_pics_info($attr, $pages);
}
/* ********************** */
function get_selposts($attr) {
$postids = $attr['postid'];
if (preg_match('#^(\w+)\s*:\s*(.*?)\s*$#', $postids, $selection)) {
$type = strtolower($selection[1]);
$value = $selection[2];
$numeric_value = preg_match('#^[-0-9,]+#', $value); // '5,-3' counts as all numeric here
if ($type == 'tag') {
$query = "tag=$value";
} elseif ($type == 'author') {
$query = $numeric_value ? "author=$value" : "author_name=$value";
} else { // assume $type == 'category')
$query = $numeric_value ? "cat=$value" : "category_name=$value";
}
} else {
$query = "include=$postids";
}
if (preg_match('#^posts\s*:\s*(.*)#', $attr['display'], $value)) {
$query .= "&post_type=$value[1]"; // custom post type
}
if ($attr['count']) { $query .= '&numberposts=' . $attr['count']; }
if ($attr['start']) { $query .= '&offset=' . $attr['start']; }
// possible ordering values (date, author, title,...) listed in http://codex.wordpress.org/Template_Tags/query_posts
if (substr(strtolower($attr['orderby']),0,5) == 'meta:') {
$query .= "&meta_key=" . substr($attr['orderby'],5);
$attr['orderby']='meta_value';
}
if (strtolower($attr['order']) == 'rand') {
$attr['orderby'] = 'rand'; // for backwards compatibility
$attr['order'] = '';
}
if (strtolower($attr['orderby']) == 'menu_order') { // useless for posts
$attr['orderby'] = 'post_date'; // a sensible default, and backwards compatible
if (strtoupper($attr['order']) == $attr['order']) { // default order? override
$attr['order'] = 'desc';
}
}
if (strtolower($attr['orderby']) == 'postmash') { // PostMash plugin actually uses post menu_order
$attr['orderby'] = 'menu_order';
}
if ($attr['order']) { $query .= '&order=' . $attr['order']; }
if ($attr['orderby']) { $query .= '&orderby=' . $attr['orderby']; }
$these_posts = get_posts($query);
if (count($these_posts) == 0) {
return;
}
return get_pics_info($attr, $these_posts);
}
/* ********************** */
function prepare_picture (&$pic) {
$alt_text = strlen($pic['alt_text']) ? $pic['alt_text'] : $pic['title'];
$pic['content'] = '';
if ($pic['permalink'] == '') {
if ($pic['linkto'] == 'pic') {
$pic['permalink'] = $pic['pic_full_url']; // link to fullsize image
} elseif ($pic['linkto'] == 'attpage') {
$pic['permalink'] = $pic['attpage'];
}
}
}
/* ********************** */
function create_output($attr, $pic_info) {
if ($attr['start'] > 0) {
$pic_info = array_slice($pic_info, $attr['start']);
}
if ($attr['count'] > 0) {
$pic_info = array_slice($pic_info, 0, $attr['count']);
}
if (!is_array($pic_info)) { // nothing to do
return '';
}
$total_pages = 1;
// Pagination: Break candidate images, selected above, into pages.
if ($attr['paged'] > 0) {
$total_pages = ceil(count($pic_info) / $attr['paged']);
$cur_page = 1;
global $wp_query; // For pagination
if( isset( $wp_query->query_vars['paged'] )) {
// no page number, or page 1, gives offset 0.
$cur_page = max(1, $wp_query->query_vars['page'] );
}
// Now select only current page.
$pic_info = array_slice($pic_info, ($cur_page - 1) * $attr['paged'], $attr['paged']);
}
$html = '';
$class = $attr['class'];
if ($attr['display'] == 'list' || $attr['list']) { // Produce list output
$html = '
' . $pic['excerpt'] . "
\n"; } $html .= "' . $pic['excerpt'] . "
\n"; } $html .= "'; // Possibly permit override of 'next_text', 'prev_text', etc. - see /wp-includes/general_template.php $paginate_args = array('base' => get_permalink() . '%_%', 'total' => $total_pages, 'current' => $cur_page, 'show_all' => 1); $mybase = get_permalink(); if (strpos($mybase,'?') !== FALSE) { $paginate_args['format'] = '&page=%#%'; } // append rather than start arg $html .= paginate_links($paginate_args); $html .= '
'; } return $html; } /* ********************** */ add_shortcode('autonav','autonav_wl_shortcode'); function autonav_wl_shortcode($attr) { global $post; // NOTE: This function can be added as a filter to override the standard Gallery shortcode. // In that case, this function may return an empty string to restore default behavior. $options = get_option('autonav_wl'); // Default values come from saved configuration $attr['order'] = strtolower($attr['order']); // so we can make 'desc' default for posts, regardless of option setting $attr = (shortcode_atts($options, $attr)); // display can be: 'images' or 'list' (for child pages), '/folder' for images from directory, // or the default 'attached' for table of attached images if (in_array($attr['size'],get_intermediate_image_sizes())) { $size_list = image_constrain_size_for_editor(4000,4000,$attr['size']); // 4000 forces constraints $attr['size'] = $size_list[0].'x'.$size_list[1]; } elseif (substr($attr['size'],0,5) == 'size_') { $attr['size'] = $attr[$attr['size']]; // e.g., size_small --> 150x120 } if (!preg_match('#(\d+)x(\d+)#',$attr['size'],$size)) { if ($attr['columns'] <= $attr['col_large']) { $attr['size'] = $attr['size_large']; } elseif ($attr['columns'] >= $attr['col_small']) { $attr['size'] = $attr['size_small']; } else { $attr['size'] = $attr['size_med']; } } if (!preg_match('#(\d+)x(\d+)#',$attr['size'],$sizebits)) { return 'Incorrect size specified: '. $attr['size']; } else { $attr['width'] = $sizebits[1]; $attr['height'] = $sizebits[2]; } $display_options = explode(',', $attr['display']); $attr['display'] = array_shift($display_options); // Mode specific defaults if (substr($attr['display'],0,6) == 'attach') { $attr['linkto'] = 'pic'; } // process options foreach ($display_options as $o) { if (strpos($o, 'title') !== false) $attr['titles'] = 1; if (strpos($o, 'excerpt') !== false) $attr['excerpt'] = 1; if (strpos($o, 'thumb') !== false) $attr['show_thumb'] = 1; if (strpos($o, 'siblings') !== false) $attr['siblings'] = 1; if (strpos($o, 'family') !== false) $attr['family'] = 1; if (strpos($o, 'self') !== false) $attr['self'] = 1; if (strpos($o, 'list') !== false) $attr['list'] = 1; if (strpos($o, 'image') !== false) $attr['linkto'] = 'pic'; if (strpos($o, 'page') !== false) $attr['linkto'] = 'attpage'; } if (!strlen($attr['class']) ) { $attr['class'] = 'subpages'; } if (($attr['display'] == 'list') || ($attr['display'] == 'images')) { $pic_info = get_subpages($attr); } elseif (substr($attr['display'],0,6) == 'attach') { $post_id = $attr['postid']; if (!$post_id) { $post_id = $post->ID; } if ($attr['display'] == 'attachments') { $pic_info = get_attachments($attr, $post_id, 0); } else { $pic_info = get_images_attached($attr, $post_id, 0); } } elseif (substr($attr['display'], 0, 5) == 'posts') { $pic_info = get_selposts($attr); $attr['start'] = 0; // start,count handled inside get_selposts query $attr['count'] = 0; } else { $attr['linkto'] = 'pic'; $pic_info = get_images_from_folder($attr); } $html = create_output($attr, $pic_info); return $html; } /* ********************** */ // This goes into table wp_options as follows: // // option_id =
Current Defaults
Further Information |