Example #1
0
/**
 * Returns the image URL for a given slide ID ( $post_id ) that belongs to
 * a given slider id( $slider_id ). Will do all size checking and will return
 * an array with the image details or false if image isn't found.
 * 
 * @param int $post_id - ID of slide
 * @param int $slider_id - ID of slider
 * @return array( 'url' => URL of image, 'width' => image width, 'height' => image height, 'id' => image ID if found in media gallery )
 */
function get_the_fa_slide_image_url($post_id = false, $slider_id = false)
{
    // set the post ID
    if (!$post_id) {
        global $post;
        if ($post) {
            $post_id = $post->ID;
        } else {
            _doing_it_wrong('get_the_fa_image_url()', __('Use this function inside a slide loop or pass the slide (post) ID to the function.', 'fapro'), '3.0');
        }
    } else {
        $post = get_post($post_id);
    }
    // get the slider ID
    if (!$slider_id) {
        global $fa_slider;
        if ($fa_slider) {
            $slider_id = $fa_slider->ID;
        } else {
            _doing_it_wrong('get_the_fa_image_url()', __('Use this function inside a slide loop or pass the slider ID to the function.', 'fapro'), '3.0');
        }
    }
    if (!$post || !$post_id || !$slider_id) {
        return false;
    }
    // get slider options and check if image is visible
    $slider_options = fa_get_slider_options($slider_id, 'content_image');
    if (!$slider_options['show']) {
        return;
    }
    // slide options
    $options = fa_get_slide_options($post_id);
    // get the attached image ID
    $image_id = get_the_fa_image_id($post_id);
    // try to make the image url
    $image_url = false;
    if ('wp' == $slider_options['sizing'] && $image_id) {
        $wp_attachment = wp_get_attachment_image_src($image_id, $slider_options['wp_size']);
        if ($wp_attachment) {
            $image_url = $wp_attachment[0];
        }
    } else {
        if ('custom' == $slider_options['sizing'] && $image_id) {
            $image_url = fa_get_custom_image_size($image_id, $slider_options['width'], $slider_options['height']);
        }
    }
    // last options, check on slide settings if an image URL is set
    if (!$image_url) {
        if (isset($options['temp_image_url']) && !empty($options['temp_image_url'])) {
            $image_url = $options['temp_image_url'];
        }
    }
    // if no image URL was detected, stop
    if (!$image_url) {
        return false;
    }
    $width = $height = false;
    // get the image size
    if ('custom' == $slider_options['sizing']) {
        $width = absint($slider_options['width']);
        $height = absint($slider_options['height']);
    } elseif ('wp' == $slider_options['sizing']) {
        if (isset($wp_attachment) && $wp_attachment) {
            $width = $wp_attachment[1];
            $height = $wp_attachment[2];
        }
    }
    // create the response
    return array('url' => $image_url, 'width' => $width, 'height' => $height, 'id' => $image_id);
}
Example #2
0
/**
 * Returns a custom image size for the image attached to a given slide post.
 * @param object $post - a given slide post
 * @param array $size - image width ( as: array('width' => W, 'height' => H) ) or string ( ie: thumbnail )
 * @return string - the image URL
 */
function fa_get_custom_image($post, $size = array('width' => 0, 'height' => 0))
{
    if (!$post) {
        return;
    }
    // slide can be attachment image, in this case the image ID is the actual post ID
    if ('attachment' == $post->post_type) {
        $image_id = $post->ID;
    } else {
        $image_id = get_the_fa_image_id($post->ID);
        if (!$image_id) {
            global $fa_slider;
            if ($fa_slider) {
                $slider_options = fa_get_slider_options($fa_slider->ID, 'content_image');
            }
            if (!$image_id) {
                return false;
            }
        }
    }
    if (is_string($size)) {
        $imgsz = get_intermediate_image_sizes();
        if (in_array($size, $imgsz)) {
            $width = get_option($size . '_size_w');
            $height = get_option($size . '_size_h');
        }
    } elseif (is_array($size)) {
        extract($size, EXTR_SKIP);
    } else {
        return;
    }
    $image_url = fa_get_custom_image_size($image_id, $width, $height);
    return $image_url;
}