Beispiel #1
0
/**
 * Get Image Source.
 *
 * Return a uri to a custom image size.
 *
 * If size doesn't exist, attempt to create a resized version.
 * The output of this function should be escaped before printing to the browser.
 *
 * @param     int       Image ID.
 * @return    string    URI of custom image on success; emtpy string otherwise.
 *
 * @access    private.
 * @since     2010-10-28
 */
function taxonomy_image_plugin_get_image_src($id)
{
    $detail = taxonomy_image_plugin_detail_image_size();
    /* Return url to custom intermediate size if it exists. */
    $img = image_get_intermediate_size($id, $detail['name']);
    if (isset($img['url'])) {
        return $img['url'];
    }
    /* Detail image does not exist, attempt to create it. */
    $wp_upload_dir = wp_upload_dir();
    if (isset($wp_upload_dir['basedir'])) {
        /* Create path to original uploaded image. */
        $path = trailingslashit($wp_upload_dir['basedir']) . get_post_meta($id, '_wp_attached_file', true);
        if (is_file($path)) {
            /* Attempt to create a new downsized version of the original image. */
            $new = image_resize($path, $detail['size'][0], $detail['size'][1], $detail['size'][2]);
            /* Image creation successful. Generate and cache image metadata. Return url. */
            if (!is_wp_error($new)) {
                $meta = wp_generate_attachment_metadata($id, $path);
                wp_update_attachment_metadata($id, $meta);
                $img = image_get_intermediate_size($id, $detail['name']);
                if (isset($img['url'])) {
                    return $img['url'];
                }
            }
        }
    }
    /* Custom intermediate size cannot be created, try for thumbnail. */
    $img = image_get_intermediate_size($id, 'thumbnail');
    if (isset($img['url'])) {
        return $img['url'];
    }
    /* Thumbnail cannot be found, try fullsize. */
    $url = wp_get_attachment_url($id);
    if (!empty($url)) {
        return $url;
    }
    /**
     * No image can be found.
     * This is most likely caused by a user deleting an attachment before deleting it's association with a taxonomy.
     * If we are in the administration panels:
     * - Delete the association.
     * - Return uri to default.png.
     */
    if (is_admin()) {
        $assoc = taxonomy_image_plugin_get_associations();
        foreach ($assoc as $term => $img) {
            if ($img === $id) {
                unset($assoc[$term]);
            }
        }
        update_option('taxonomy_image_plugin', $assoc);
        return taxonomy_image_plugin_url('default.png');
    }
    /*
     * No image can be found.
     * Return path to blank-image.png.
     */
    return taxonomy_image_plugin_url('blank.png');
}
/**
 * Get Image Source.
 *
 * Return a uri to a custom image size.
 *
 * If size doesn't exist, attempt to create a resized version.
 * The output of this function should be escaped before printing to the browser.
 *
 * @param     int       Image ID.
 * @return    string    URI of custom image on success; emtpy string otherwise.
 *
 * @access    private.
 * @since     2010-10-28
 */
function taxonomy_image_plugin_get_image_src($id)
{
    $detail = taxonomy_image_plugin_detail_image_size();
    /* Return url to custom intermediate size if it exists. */
    $img = image_get_intermediate_size($id, $detail['name']);
    if (isset($img['url'])) {
        return $img['url'];
    }
    /* Detail image does not exist, attempt to create it. */
    $fullsizepath = get_attached_file($id);
    if (false != $fullsizepath && file_exists($fullsizepath)) {
        $metadata = wp_generate_attachment_metadata($id, $fullsizepath);
        if (!is_wp_error($metadata) && !empty($metadata)) {
            wp_update_attachment_metadata($id, $metadata);
        }
        /* Let's try the custom intermediate size again after the regeneration */
        $img = image_get_intermediate_size($id, $detail['name']);
        if (isset($img['url'])) {
            return $img['url'];
        }
    }
    /* Allow third party code to provide a proper detail image url */
    $url = apply_filters('taxonomy_image_detail_image_url', '', $id);
    if (!empty($url)) {
        return $url;
    }
    /* Custom intermediate size cannot be created, try for thumbnail. */
    $img = image_get_intermediate_size($id, 'thumbnail');
    if (isset($img['url'])) {
        return $img['url'];
    }
    /* Thumbnail cannot be found, try fullsize. */
    $url = wp_get_attachment_url($id);
    if (!empty($url)) {
        return $url;
    }
    /*
     * No image can be found.
     * This is most likely caused by a user deleting an attachment before deleting it's association with a taxonomy.
     * If we are in the administration panels:
     * - Delete the association.
     * - Return uri to default.png.
     */
    if (is_admin()) {
        $assoc = taxonomy_image_plugin_get_associations();
        foreach ($assoc as $term => $img) {
            if ($img === $id) {
                unset($assoc[$term]);
            }
        }
        update_option('taxonomy_image_plugin', $assoc);
        return taxonomy_image_plugin_url('default.png');
    }
    /*
     * No image can be found.
     * Return path to blank-image.png.
     */
    return taxonomy_image_plugin_url('blank.png');
}