| 16 | | The actual function (documented as far as possible). |
| 17 | | |
| 18 | | {{{ |
| 19 | | /** |
| 20 | | * Default image |
| 21 | | * |
| 22 | | * Builds an default <img> for use in themes or plugins before any other images are added. |
| 23 | | * Resizes & crops the image using the built-in (retireved via `get_intermediate_image_sizes();`) |
| 24 | | * or custom image (added via `add_image_size();`) sizes. |
| 25 | | * |
| 26 | | * Retrieves calculated resize dimension @uses image_resize_dimensions(); |
| 27 | | * Builds the width and height string @uses image_hwstring(); |
| 28 | | * |
| 29 | | * @param $args (array) |
| 30 | | * string $url URl to the given default image. |
| 31 | | * string $size Optional. Default is 'medium'. |
| 32 | | * string (optional) $alt Image Description for the alt attribute. |
| 33 | | * string (optional) $title Image Description for the title attribute. |
| 34 | | * string (optional) $align Part of the class name for aligning the image. |
| 35 | | * string (optional) $echo Wheter to return or echo the $image |
| 36 | | * @return string HTML IMG element for given image attachment |
| 37 | | */ |
| 38 | | function wp_default_img( $attr = array( 'url', 'size' => 'medium', 'classes' => false, 'alt' => false, 'title' => false, 'align' => 'none', 'echo' => true ) ) |
| 39 | | { |
| 40 | | // Sizes registered via add_image_size(); |
| 41 | | global $_wp_additional_image_sizes; |
| 42 | | |
| 43 | | if ( 'thumb' === $attr['size'] ) |
| 44 | | $attr['size'] = 'thumbnail'; |
| 45 | | |
| 46 | | // Size in built in sizes - call size setting from DB |
| 47 | | # behavoir in here dependent on @link http://core.trac.wordpress.org/ticket/18947 |
| 48 | | if ( ! in_array( $attr['size'], array_keys( $_wp_additional_image_sizes ) ) ) |
| 49 | | { |
| 50 | | $sizes = get_intermediate_image_sizes(); |
| 51 | | // Get option - gladly autoloaded/can use wp_cache_get(); |
| 52 | | $size_data['width'] = intval( get_option( "{$attr['size']}_size_w") ); |
| 53 | | $size_data['height']= intval( get_option( "{$attr['size']}_size_h") ); |
| 54 | | // Not sure how this will behave if cropped is false (autoloaded option not added) |
| 55 | | $size_data['crop'] = get_option( "{$attr['size']}_crop" ) ? get_option( "{$attr['size']}_crop" ) : false; |
| 56 | | } |
| 57 | | // Size array from global registered additional/custom sizes array |
| 58 | | else |
| 59 | | { |
| 60 | | $size_data = $_wp_additional_image_sizes[ $attr['size'] ]; |
| 61 | | } |
| 62 | | |
| 63 | | // Retrieve image width & height |
| 64 | | $img_info = @getimagesize( $attr['url'] ); |
| 65 | | |
| 66 | | // Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped |
| 67 | | $end_sizes = image_resize_dimensions( $img_info[0], $img_info[1], $size_data['width'], $size_data['height'], $size_data['crop'] ); |
| 68 | | |
| 69 | | // defaults to px units - can't get changed, as applying units is not possible |
| 70 | | $hwstring = ' '.trim( image_hwstring( $end_sizes[4], $end_sizes[5] ) ); |
| 71 | | |
| 72 | | // Attributes: |
| 73 | | // Not made required as users tend to do funky things (...and lock screen readers out) |
| 74 | | $attr['alt'] = $attr['alt'] ? ' alt="'.esc_attr( $attr['alt'] )."'" : ''; |
| 75 | | |
| 76 | | if ( ! $attr['title'] ) |
| 77 | | $attr['title'] = sprintf( __('default image of type: %1$s'), explode( "/", $img_info['mime'] ) ); |
| 78 | | $attr['title'] = $attr['title'] ? ' title="'.esc_attr( $attr['title'] )."'" : ''; |
| 79 | | |
| 80 | | $attr['classes'] = "wp-img-default ".esc_attr( $attr['classes'] ? $attr['classes'] : '' ); |
| 81 | | $attr['align'] = $attr['align'] ? "align".esc_attr( $attr['align'] ) : ''; |
| 82 | | $attr['size'] = "size-".esc_attr( $attr['size'] ); |
| 83 | | |
| 84 | | // Allow filtering of the default attributes |
| 85 | | $attributes = apply_filters( 'wp_default_img_attr', $attr ); |
| 86 | | |
| 87 | | // Build class attribute, considering that maybe some attribute was unset via the filter |
| 88 | | $classes = ' class="'; |
| 89 | | $classes .= 'wp-img-default'.esc_attr( $attr['classes'] ? ' '.$attr['classes'] : '' ); |
| 90 | | $classes .= $attr['align'] ? ' '.esc_attr( $attr['align'] ) : ''; |
| 91 | | $classes .= $attr['size'] ? ' '.esc_attr( $attr['size'] ).'" ' : '" '; |
| 92 | | |
| 93 | | $image = "<img src='{$url}'{$hwstring}{$classes}{$alt}{$title} />"; |
| 94 | | $image = apply_filters( 'wp_default_img', $image ); |
| 95 | | |
| 96 | | if ( ! $attr['echo'] ) |
| 97 | | return $image; |
| 98 | | |
| 99 | | return print $image; |
| 100 | | } |
| 101 | | }}} |
| 102 | | |