/**
 * View function.
 *
 * @param type $params
 */
function wpcf_fields_image_view($params)
{
    global $wpcf;
    $output = '';
    $alt = false;
    $title = false;
    $class = array();
    $style = array();
    // Get image data
    $image_data = wpcf_fields_image_get_data($params['field_value']);
    // Error
    if (!empty($image_data['error'])) {
        return '__wpcf_skip_empty';
    }
    // Set alt
    if (isset($params['alt'])) {
        $alt = $params['alt'];
    }
    // Set title
    if (isset($params['title'])) {
        $title = $params['title'];
    }
    // Set attachment class
    if (!empty($params['size'])) {
        $class[] = 'attachment-' . $params['size'];
    }
    // Set align class
    if (!empty($params['align']) && $params['align'] != 'none') {
        $class[] = 'align' . $params['align'];
    }
    if (!empty($params['class'])) {
        $class[] = $params['class'];
    }
    if (!empty($params['style'])) {
        $style[] = $params['style'];
    }
    // Compatibility with old parameters
    $old_param = isset($params['proportional']) && $params['proportional'] == 'true' ? 'proportional' : 'crop';
    $resize = isset($params['resize']) ? $params['resize'] : $old_param;
    // Pre-configured size (use WP function) IF NOT CROPPED
    if ($resize == 'crop' && $image_data['is_attachment'] && !empty($params['size'])) {
        //print_r('is_attachment');
        if (isset($params['url']) && $params['url'] == 'true') {
            //print_r('is_url');
            $image_url = wp_get_attachment_image_src($image_data['is_attachment'], $params['size']);
            if (!empty($image_url[0])) {
                $output = $image_url[0];
            } else {
                $output = $params['field_value'];
            }
            // TODO This is current fix, should be re-designed
            $wpcf->__images_wrap_fix[md5(serialize($params))] = $output;
        } else {
            //print_r('is_not_url');
            $output = wp_get_attachment_image($image_data['is_attachment'], $params['size'], false, array('class' => implode(' ', $class), 'style' => implode(' ', $style), 'alt' => $alt, 'title' => $title));
        }
    } else {
        // Custom size
        //print_r('custom_size');
        $width = !empty($params['width']) ? intval($params['width']) : null;
        $height = !empty($params['height']) ? intval($params['height']) : null;
        //////////////////////////
        // If width and height are not set then check the size parameter.
        // This handles the case when the image is not an attachment.
        if (empty($width) && empty($height) && !empty($params['size'])) {
            //print_r('no_width_no_height_and_size');
            switch ($params['size']) {
                case 'thumbnail':
                    $width = get_option('thumbnail_size_w');
                    $height = get_option('thumbnail_size_h');
                    if (empty($params['proportional'])) {
                        $crop = get_option('thumbnail_crop');
                    }
                    break;
                case 'medium':
                    $width = get_option('medium_size_w');
                    $height = get_option('medium_size_h');
                    break;
                case 'large':
                    $width = get_option('large_size_w');
                    $height = get_option('large_size_h');
                    break;
                default:
                    global $_wp_additional_image_sizes;
                    if (isset($_wp_additional_image_sizes[$params['size']]) && is_array($_wp_additional_image_sizes[$params['size']])) {
                        extract($_wp_additional_image_sizes[$params['size']]);
                    }
            }
        }
        // Check if image is outsider and require $width and $height
        if ((!empty($width) || !empty($height)) && !$image_data['is_outsider']) {
            // Resize args
            $args = array('resize' => $resize, 'padding_color' => isset($params['padding_color']) ? $params['padding_color'] : '#FFF', 'width' => $width, 'height' => $height, 'return' => 'object', 'suppress_errors' => false, 'clear_cache' => false);
            WPCF_Loader::loadView('image');
            $__resized_image = types_image_resize($image_data['fullabspath'], $args);
            if (is_wp_error($__resized_image)) {
                $resized_image = $params['field_value'];
            } else {
                $resized_image = $__resized_image->url;
                $image_abspath = $__resized_image->path;
                if (wpcf_get_settings('add_resized_images_to_library') && !wpcf_image_is_attachment($__resized_image->url) && $image_data['is_in_upload_path']) {
                    global $post;
                    wpcf_image_add_to_library($post, $image_abspath);
                }
            }
        } else {
            $resized_image = $params['field_value'];
        }
        if (isset($params['url']) && $params['url'] == 'true') {
            // TODO This is current fix, should be re-designed
            $wpcf->__images_wrap_fix[md5(serialize($params))] = $resized_image;
            return $resized_image;
        }
        $output = sprintf('<img alt="%s" ', $output .= $alt !== false ? esc_attr($alt) : '');
        if ($title !== false) {
            $output .= sprintf(' title="%s"', esc_attr($title));
        }
        $output .= !empty($params['onload']) ? ' onload="' . esc_attr($params['onload']) . '"' : '';
        $output .= !empty($class) ? ' class="' . esc_attr(implode(' ', $class)) . '"' : '';
        $output .= !empty($style) ? ' style="' . esc_attr(implode(' ', $style)) . '"' : '';
        $output .= sprintf(' src="%s" />', esc_attr($resized_image));
    }
    return $output;
}
Пример #2
0
/**
 * API call.
 * 
 * @param type $img
 * @param type $args
 * @return type
 */
function types_image_crop($img, $args = array())
{
    return types_image_resize($img, $args);
}