Beispiel #1
0
/**
 * Retrieve URL for an attachment thumbnail.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID
 * @return string|bool False on failure. Thumbnail URL on success.
 */
function nxt_get_attachment_thumb_url($post_id = 0)
{
    $post_id = (int) $post_id;
    if (!($post =& get_post($post_id))) {
        return false;
    }
    if (!($url = nxt_get_attachment_url($post->ID))) {
        return false;
    }
    $sized = image_downsize($post_id, 'thumbnail');
    if ($sized) {
        return $sized[0];
    }
    if (!($thumb = nxt_get_attachment_thumb_file($post->ID))) {
        return false;
    }
    $url = str_replace(basename($url), basename($thumb), $url);
    return apply_filters('nxt_get_attachment_thumb_url', $url, $post->ID);
}
Beispiel #2
0
/**
 * Scale an image to fit a particular size (such as 'thumb' or 'medium').
 *
 * Array with image url, width, height, and whether is intermediate size, in
 * that order is returned on success is returned. $is_intermediate is true if
 * $url is a resized image, false if it is the original.
 *
 * The URL might be the original image, or it might be a resized version. This
 * function won't create a new resized copy, it will just return an already
 * resized one if it exists.
 *
 * A plugin may use the 'image_downsize' filter to hook into and offer image
 * resizing services for images. The hook must return an array with the same
 * elements that are returned in the function. The first element being the URL
 * to the new image that was resized.
 *
 * @since 2.5.0
 * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
 *		resize services.
 *
 * @param int $id Attachment ID for image.
 * @param array|string $size Optional, default is 'medium'. Size of image, either array or string.
 * @return bool|array False on failure, array on success.
 */
function image_downsize($id, $size = 'medium')
{
    if (!nxt_attachment_is_image($id)) {
        return false;
    }
    $img_url = nxt_get_attachment_url($id);
    $meta = nxt_get_attachment_metadata($id);
    $width = $height = 0;
    $is_intermediate = false;
    $img_url_basename = nxt_basename($img_url);
    // plugins can use this to provide resize services
    if ($out = apply_filters('image_downsize', false, $id, $size)) {
        return $out;
    }
    // try for a new style intermediate size
    if ($intermediate = image_get_intermediate_size($id, $size)) {
        $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
        $width = $intermediate['width'];
        $height = $intermediate['height'];
        $is_intermediate = true;
    } elseif ($size == 'thumbnail') {
        // fall back to the old thumbnail
        if (($thumb_file = nxt_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) {
            $img_url = str_replace($img_url_basename, nxt_basename($thumb_file), $img_url);
            $width = $info[0];
            $height = $info[1];
            $is_intermediate = true;
        }
    }
    if (!$width && !$height && isset($meta['width'], $meta['height'])) {
        // any other type: use the real image
        $width = $meta['width'];
        $height = $meta['height'];
    }
    if ($img_url) {
        // we have the actual image size, but might need to further constrain it if content_width is narrower
        list($width, $height) = image_constrain_size_for_editor($width, $height, $size);
        return array($img_url, $width, $height, $is_intermediate);
    }
    return false;
}