Example #1
0
/**
 * Hook WP Thumb into the WordPress image functions
 *
 * Usage `the_post_thumbnail( 'width=100&height=200&crop=1' );`
 *
 * @param null  $null
 * @param int   $id
 * @param array $args
 * @return null
 */
function wpthumb_post_image($null, $id, $args)
{
    // check if $args is a WP Thumb argument list, or native WordPress one
    // wp thumb looks like this: 'width=300&height=120&crop=1'
    // native looks like 'thumbnail'
    if (is_string($args) && !strpos((string) $args, '=')) {
        $original_args = $args;
        if ($original_args === ($args = apply_filters('wpthumb_create_args_from_size', $args))) {
            return $null;
        }
    }
    $args = wp_parse_args($args);
    if (!empty($args[0])) {
        $args['width'] = $args[0];
    }
    if (!empty($args[1])) {
        $args['height'] = $args[1];
    }
    if (!empty($args['crop']) && $args['crop'] && empty($args['crop_from_position'])) {
        $args['crop_from_position'] = get_post_meta($id, 'wpthumb_crop_pos', true);
    }
    if (empty($path)) {
        $path = get_attached_file($id);
    }
    $path = apply_filters('wpthumb_post_image_path', $path, $id, $args);
    $args = apply_filters('wpthumb_post_image_args', $args, $id);
    $image = new WP_Thumb($path, $args);
    $args = $image->getArgs();
    extract($args);
    if (!$image->errored()) {
        $image_src = $image->returnImage();
        $crop = (bool) empty($crop) ? false : $crop;
        if (!$image->errored() && ($image_meta = @getimagesize($image->getCacheFilePath()))) {
            $html_width = $image_meta[0];
            $html_height = $image_meta[1];
        } else {
            $html_width = $html_height = false;
        }
    } else {
        $html_width = $width;
        $html_height = $height;
        $image_src = $image->getFileURL();
    }
    return array($image_src, $html_width, $html_height, true);
}
Example #2
0
/**
 * Hook WP Thumb into the WordPress image functions
 *
 * Usage `the_post_thumbnail( 'width=100&height=200&crop=1' );`
 *
 * @param null $null
 * @param int $id
 * @param array $args
 * @return null
 */
function wpthumb_post_image($null, $id, $args)
{
    // check if $args is a WP Thumb argument list, or native WordPress one
    // wp thumb looks like this: 'width=300&height=120&crop=1'
    // native looks like 'thumbnail'...|array( 300, 300 )
    if (is_string($args) && !strpos((string) $args, '=')) {
        global $_wp_additional_image_sizes;
        // Convert keyword sizes to heights & widths.
        if ($args == 'thumbnail') {
            $new_args = array('width' => get_option('thumbnail_size_w'), 'height' => get_option('thumbnail_size_h'), 'crop' => get_option('thumbnail_crop'));
        } elseif ($args == 'medium') {
            $new_args = array('width' => get_option('medium_size_w'), 'height' => get_option('medium_size_h'));
        } elseif ($args == 'large') {
            $new_args = array('width' => get_option('large_size_w'), 'height' => get_option('large_size_h'));
        } elseif (!empty($_wp_additional_image_sizes) && array_key_exists($args, $_wp_additional_image_sizes)) {
            $new_args = array('width' => $_wp_additional_image_sizes[$args]['width'], 'height' => $_wp_additional_image_sizes[$args]['height'], 'crop' => $_wp_additional_image_sizes[$args]['crop'], 'image_size' => $args);
        } elseif ($args != ($new_filter_args = apply_filters('wpthumb_create_args_from_size', $args))) {
            $new_args = $new_filter_args;
        } else {
            $new_args = null;
        }
        if (!$new_args) {
            return $null;
        }
        $args = $new_args;
    }
    $args = wp_parse_args($args);
    if (!empty($args[0])) {
        $args['width'] = $args[0];
    }
    if (!empty($args[1])) {
        $args['height'] = $args[1];
    }
    if (!empty($args['crop']) && $args['crop'] && empty($args['crop_from_position'])) {
        $args['crop_from_position'] = get_post_meta($id, 'wpthumb_crop_pos', true);
    }
    if (empty($path)) {
        $path = get_attached_file($id);
    }
    $path = apply_filters('wpthumb_post_image_path', $path, $id, $args);
    $args = apply_filters('wpthumb_post_image_args', $args, $id);
    $image = new WP_Thumb($path, $args);
    $args = $image->getArgs();
    extract($args);
    if (!$image->errored()) {
        $image_src = $image->returnImage();
        $crop = (bool) empty($crop) ? false : $crop;
        if (!$image->errored() && ($image_meta = @getimagesize($image->getCacheFilePath()))) {
            $html_width = $image_meta[0];
            $html_height = $image_meta[1];
        } else {
            $html_width = $html_height = false;
        }
    } else {
        $html_width = $width;
        $html_height = $height;
        $image_src = $image->getFileURL();
    }
    return array($image_src, $html_width, $html_height, true);
}