3 * WordPress API for media display.
9 * Scale down the default size of an image.
11 * This is so that the image is a better fit for the editor and theme.
13 * The $size parameter accepts either an array or a string. The supported string
14 * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
15 * 128 width and 96 height in pixels. Also supported for the string value is
16 * 'medium' and 'full'. The 'full' isn't actually supported, but any value other
17 * than the supported will result in the content_width size or 500 if that is
20 * Finally, there is a filter named, 'editor_max_image_size' that will be called
21 * on the calculated array for width and height, respectively. The second
22 * parameter will be the value that was in the $size parameter. The returned
23 * type for the hook is an array with the width as the first element and the
24 * height as the second element.
27 * @uses wp_constrain_dimensions() This function passes the widths and the heights.
29 * @param int $width Width of the image
30 * @param int $height Height of the image
31 * @param string|array $size Size of what the result image should be.
32 * @return array Width and height of what the result image should resize to.
34 function image_constrain_size_for_editor($width, $height, $size = 'medium') {
35 global $content_width;
37 if ( is_array($size) ) {
38 $max_width = $size[0];
39 $max_height = $size[1];
41 elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
42 $max_width = intval(get_option('thumbnail_size_w'));
43 $max_height = intval(get_option('thumbnail_size_h'));
44 // last chance thumbnail size defaults
45 if ( !$max_width && !$max_height ) {
50 elseif ( $size == 'medium' ) {
51 $max_width = intval(get_option('medium_size_w'));
52 $max_height = intval(get_option('medium_size_h'));
53 // if no width is set, default to the theme content width if available
55 elseif ( $size == 'large' ) {
56 // we're inserting a large size image into the editor. if it's a really
57 // big image we'll scale it down to fit reasonably within the editor
58 // itself, and within the theme's content width if it's known. the user
59 // can resize it in the editor if they wish.
60 $max_width = intval(get_option('large_size_w'));
61 $max_height = intval(get_option('large_size_h'));
62 if ( intval($content_width) > 0 )
63 $max_width = min( intval($content_width), $max_width );
65 // $size == 'full' has no constraint
68 $max_height = $height;
71 list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );
73 return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
77 * Retrieve width and height attributes using given width and height values.
79 * Both attributes are required in the sense that both parameters must have a
80 * value, but are optional in that if you set them to false or null, then they
81 * will not be added to the returned string.
83 * You can set the value using a string, but it will only take numeric values.
84 * If you wish to put 'px' after the numbers, then it will be stripped out of
89 * @param int|string $width Optional. Width attribute value.
90 * @param int|string $height Optional. Height attribute value.
91 * @return string HTML attributes for width and, or height.
93 function image_hwstring($width, $height) {
96 $out .= 'width="'.intval($width).'" ';
98 $out .= 'height="'.intval($height).'" ';
103 * Scale an image to fit a particular size (such as 'thumb' or 'medium').
105 * Array with image url, width, height, and whether is intermediate size, in
106 * that order is returned on success is returned. $is_intermediate is true if
107 * $url is a resized image, false if it is the original.
109 * The URL might be the original image, or it might be a resized version. This
110 * function won't create a new resized copy, it will just return an already
111 * resized one if it exists.
113 * A plugin may use the 'image_downsize' filter to hook into and offer image
114 * resizing services for images. The hook must return an array with the same
115 * elements that are returned in the function. The first element being the URL
116 * to the new image that was resized.
119 * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
122 * @param int $id Attachment ID for image.
123 * @param string $size Optional, default is 'medium'. Size of image, can be 'thumbnail'.
124 * @return bool|array False on failure, array on success.
126 function image_downsize($id, $size = 'medium') {
128 if ( !wp_attachment_is_image($id) )
131 $img_url = wp_get_attachment_url($id);
132 $meta = wp_get_attachment_metadata($id);
133 $width = $height = 0;
134 $is_intermediate = false;
136 // plugins can use this to provide resize services
137 if ( $out = apply_filters('image_downsize', false, $id, $size) )
140 // try for a new style intermediate size
141 if ( $intermediate = image_get_intermediate_size($id, $size) ) {
142 $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
143 $width = $intermediate['width'];
144 $height = $intermediate['height'];
145 $is_intermediate = true;
147 elseif ( $size == 'thumbnail' ) {
148 // fall back to the old thumbnail
149 if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
150 $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
153 $is_intermediate = true;
156 if ( !$width && !$height && isset($meta['width'], $meta['height']) ) {
157 // any other type: use the real image
158 $width = $meta['width'];
159 $height = $meta['height'];
163 // we have the actual image size, but might need to further constrain it if content_width is narrower
164 list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
166 return array( $img_url, $width, $height, $is_intermediate );
173 * An <img src /> tag for an image attachment, scaling it down if requested.
175 * The filter 'get_image_tag_class' allows for changing the class name for the
176 * image without having to use regular expressions on the HTML content. The
177 * parameters are: what WordPress will use for the class, the Attachment ID,
178 * image align value, and the size the image should be.
180 * The second filter 'get_image_tag' has the HTML content, which can then be
181 * further manipulated by a plugin to change all attribute values and even HTML
186 * @uses apply_filters() The 'get_image_tag_class' filter is the IMG element
188 * @uses apply_filters() The 'get_image_tag' filter is the full IMG element with
191 * @param int $id Attachment ID.
192 * @param string $alt Image Description for the alt attribute.
193 * @param string $title Image Description for the title attribute.
194 * @param string $align Part of the class name for aligning the image.
195 * @param string $size Optional. Default is 'medium'.
196 * @return string HTML IMG element for given image attachment
198 function get_image_tag($id, $alt, $title, $align, $size='medium') {
200 list( $img_src, $width, $height ) = image_downsize($id, $size);
201 $hwstring = image_hwstring($width, $height);
203 $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id;
204 $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
206 $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />';
208 $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
214 * Calculates the new dimentions for a downsampled image.
216 * Same as {@link wp_shrink_dimensions()}, except the max parameters are
217 * optional. If either width or height are empty, no constraint is applied on
222 * @param int $current_width Current width of the image.
223 * @param int $current_height Current height of the image.
224 * @param int $max_width Optional. Maximum wanted width.
225 * @param int $max_height Optional. Maximum wanted height.
226 * @return array First item is the width, the second item is the height.
228 function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
229 if ( !$max_width and !$max_height )
230 return array( $current_width, $current_height );
232 $width_ratio = $height_ratio = 1.0;
234 if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width )
235 $width_ratio = $max_width / $current_width;
237 if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height )
238 $height_ratio = $max_height / $current_height;
240 // the smaller ratio is the one we need to fit it to the constraining box
241 $ratio = min( $width_ratio, $height_ratio );
243 return array( intval($current_width * $ratio), intval($current_height * $ratio) );
247 * Retrieve calculated resized dimensions for use in imagecopyresampled().
249 * Calculate dimensions and coordinates for a resized image that fits within a
250 * specified width and height. If $crop is true, the largest matching central
251 * portion of the image will be cropped out and resized to the required size.
255 * @param int $orig_w Original width.
256 * @param int $orig_h Original height.
257 * @param int $dest_w New width.
258 * @param int $dest_h New height.
259 * @param bool $crop Optional, default is false. Whether to crop image or resize.
260 * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function.
262 function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
264 if ($orig_w <= 0 || $orig_h <= 0)
266 // at least one of dest_w or dest_h must be specific
267 if ($dest_w <= 0 && $dest_h <= 0)
271 // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
272 $aspect_ratio = $orig_w / $orig_h;
273 $new_w = min($dest_w, $orig_w);
274 $new_h = min($dest_h, $orig_h);
276 $new_w = intval($new_h * $aspect_ratio);
279 $new_h = intval($new_w / $aspect_ratio);
282 $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
284 $crop_w = ceil($new_w / $size_ratio);
285 $crop_h = ceil($new_h / $size_ratio);
287 $s_x = floor(($orig_w - $crop_w)/2);
288 $s_y = floor(($orig_h - $crop_h)/2);
291 // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
298 list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
301 // if the resulting image would be the same size or larger we don't want to resize it
302 if ($new_w >= $orig_w && $new_h >= $orig_h)
305 // the return array matches the parameters to imagecopyresampled()
306 // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
307 return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h);
312 * Scale down an image to fit a particular size and save a new copy of the image.
314 * The PNG transparency will be preserved using the function, as well as the
315 * image type. If the file going in is PNG, then the resized image is going to
316 * be PNG. The only supported image types are PNG, GIF, and JPEG.
318 * Some functionality requires API to exist, so some PHP version may lose out
319 * support. This is not the fault of WordPress (where functionality is
320 * downgraded, not actual defects), but of your PHP version.
324 * @param string $file Image file path.
325 * @param int $max_w Maximum width to resize to.
326 * @param int $max_h Maximum height to resize to.
327 * @param bool $crop Optional. Whether to crop image or resize.
328 * @param string $suffix Optional. File Suffix.
329 * @param string $dest_path Optional. New image file path.
330 * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
331 * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
333 function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
335 $image = wp_load_image( $file );
336 if ( !is_resource( $image ) )
337 return new WP_Error('error_loading_image', $image);
339 list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
340 $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
343 list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
345 $newimage = imagecreatetruecolor( $dst_w, $dst_h);
347 // preserve PNG transparency
348 if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
349 imagealphablending( $newimage, false);
350 imagesavealpha( $newimage, true);
353 imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
355 // we don't need the original in memory anymore
356 imagedestroy( $image );
358 // $suffix will be appended to the destination filename, just before the extension
360 $suffix = "{$dst_w}x{$dst_h}";
362 $info = pathinfo($file);
363 $dir = $info['dirname'];
364 $ext = $info['extension'];
365 $name = basename($file, ".{$ext}");
366 if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
368 $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
370 if ( $orig_type == IMAGETYPE_GIF ) {
371 if (!imagegif( $newimage, $destfilename ) )
372 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
374 elseif ( $orig_type == IMAGETYPE_PNG ) {
375 if (!imagepng( $newimage, $destfilename ) )
376 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
379 // all other formats are converted to jpg
380 $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
381 if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
382 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
385 imagedestroy( $newimage );
387 // Set correct file permissions
388 $stat = stat( dirname( $destfilename ));
389 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
390 @ chmod( $destfilename, $perms );
392 return $destfilename;
396 * Resize an image to make a thumbnail or intermediate size.
398 * The returned array has the file size, the image width, and image height. The
399 * filter 'image_make_intermediate_size' can be used to hook in and change the
400 * values of the returned array. The only parameter is the resized file path.
404 * @param string $file File path.
405 * @param int $width Image width.
406 * @param int $height Image height.
407 * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
408 * @return bool|array False, if no image was created. Metadata array on success.
410 function image_make_intermediate_size($file, $width, $height, $crop=false) {
411 if ( $width || $height ) {
412 $resized_file = image_resize($file, $width, $height, $crop);
413 if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) {
414 $resized_file = apply_filters('image_make_intermediate_size', $resized_file);
416 'file' => basename( $resized_file ),
418 'height' => $info[1],
426 * Retrieve the image's intermediate size (resized) path, width, and height.
428 * The $size parameter can be an array with the width and height respectively.
429 * If the size matches the 'sizes' metadata array for width and height, then it
430 * will be used. If there is no direct match, then the nearest image size larger
431 * than the specified size will be used. If nothing is found, then the function
432 * will break out and return false.
434 * The metadata 'sizes' is used for compatible sizes that can be used for the
435 * parameter $size value.
437 * The url path will be given, when the $size parameter is a string.
441 * @param int $post_id Attachment ID for image.
442 * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
443 * @return bool|array False on failure or array of file path, width, and height on success.
445 function image_get_intermediate_size($post_id, $size='thumbnail') {
446 if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
449 // get the best one for a specified set of dimensions
450 if ( is_array($size) && !empty($imagedata['sizes']) ) {
451 foreach ( $imagedata['sizes'] as $_size => $data ) {
452 // already cropped to width or height; so use this size
453 if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
454 $file = $data['file'];
455 list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
456 return compact( 'file', 'width', 'height' );
458 // add to lookup table: area => size
459 $areas[$data['width'] * $data['height']] = $_size;
461 if ( !$size || !empty($areas) ) {
462 // find for the smallest image not smaller than the desired size
464 foreach ( $areas as $_size ) {
465 $data = $imagedata['sizes'][$_size];
466 if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
467 $file = $data['file'];
468 list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
469 return compact( 'file', 'width', 'height' );
475 if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
478 $data = $imagedata['sizes'][$size];
479 // include the full filesystem path of the intermediate file
480 if ( empty($data['path']) && !empty($data['file']) ) {
481 $file_url = wp_get_attachment_url($post_id);
482 $data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
483 $data['url'] = path_join( dirname($file_url), $data['file'] );
489 * Retrieve an image to represent an attachment.
491 * A mime icon for files, thumbnail or intermediate size for images.
495 * @param int $attachment_id Image attachment ID.
496 * @param string $size Optional, default is 'thumbnail'.
497 * @param bool $icon Optional, default is false. Whether it is an icon.
498 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
500 function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
502 // get a thumbnail or intermediate image if there is one
503 if ( $image = image_downsize($attachment_id, $size) )
508 if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
509 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
510 $src_file = $icon_dir . '/' . basename($src);
511 @list($width, $height) = getimagesize($src_file);
513 if ( $src && $width && $height )
514 return array( $src, $width, $height );
519 * Get an HTML img element representing an image attachment
521 * @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array
522 * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions
525 * @param int $attachment_id Image attachment ID.
526 * @param string $size Optional, default is 'thumbnail'.
527 * @param bool $icon Optional, default is false. Whether it is an icon.
528 * @return string HTML img element or empty string on failure.
530 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) {
533 $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
535 list($src, $width, $height) = $image;
536 $hwstring = image_hwstring($width, $height);
537 if ( is_array($size) )
538 $size = join('x', $size);
539 $attachment =& get_post($attachment_id);
542 'class' => "attachment-$size",
543 'alt' => trim(strip_tags( $attachment->post_excerpt )),
544 'title' => trim(strip_tags( $attachment->post_title )),
546 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
547 $attr = array_map( 'esc_attr', $attr );
548 $html = rtrim("<img $hwstring");
549 foreach ( $attr as $name => $value ) {
550 $html .= " $name=" . '"' . $value . '"';
558 add_shortcode('wp_caption', 'img_caption_shortcode');
559 add_shortcode('caption', 'img_caption_shortcode');
562 * The Caption shortcode.
564 * Allows a plugin to replace the content that would otherwise be returned. The
565 * filter is 'img_caption_shortcode' and passes an empty string, the attr
566 * parameter and the content parameter values.
568 * The supported attributes for the shortcode are 'id', 'align', 'width', and
573 * @param array $attr Attributes attributed to the shortcode.
574 * @param string $content Optional. Shortcode content.
577 function img_caption_shortcode($attr, $content = null) {
579 // Allow plugins/themes to override the default caption template.
580 $output = apply_filters('img_caption_shortcode', '', $attr, $content);
584 extract(shortcode_atts(array(
586 'align' => 'alignnone',
591 if ( 1 > (int) $width || empty($caption) )
594 if ( $id ) $id = 'id="' . $id . '" ';
596 return '<div ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px">'
597 . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
600 add_shortcode('gallery', 'gallery_shortcode');
603 * The Gallery shortcode.
605 * This implements the functionality of the Gallery Shortcode for displaying
606 * WordPress images on a post.
610 * @param array $attr Attributes attributed to the shortcode.
611 * @return string HTML content to display gallery.
613 function gallery_shortcode($attr) {
616 static $instance = 0;
619 // Allow plugins/themes to override the default gallery template.
620 $output = apply_filters('post_gallery', '', $attr);
624 // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
625 if ( isset( $attr['orderby'] ) ) {
626 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
627 if ( !$attr['orderby'] )
628 unset( $attr['orderby'] );
631 extract(shortcode_atts(array(
633 'orderby' => 'menu_order ID',
637 'captiontag' => 'dd',
639 'size' => 'thumbnail'
643 $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
645 if ( empty($attachments) )
650 foreach ( $attachments as $att_id => $attachment )
651 $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
655 $itemtag = tag_escape($itemtag);
656 $captiontag = tag_escape($captiontag);
657 $columns = intval($columns);
658 $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
660 $selector = "gallery-{$instance}";
662 $output = apply_filters('gallery_style', "
663 <style type='text/css'>
667 #{$selector} .gallery-item {
671 width: {$itemwidth}%; }
673 border: 2px solid #cfcfcf;
675 #{$selector} .gallery-caption {
679 <!-- see gallery_shortcode() in wp-includes/media.php -->
680 <div id='$selector' class='gallery galleryid-{$id}'>");
683 foreach ( $attachments as $id => $attachment ) {
684 $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
686 $output .= "<{$itemtag} class='gallery-item'>";
688 <{$icontag} class='gallery-icon'>
691 if ( $captiontag && trim($attachment->post_excerpt) ) {
693 <{$captiontag} class='gallery-caption'>
694 " . wptexturize($attachment->post_excerpt) . "
697 $output .= "</{$itemtag}>";
698 if ( $columns > 0 && ++$i % $columns == 0 )
699 $output .= '<br style="clear: both" />';
703 <br style='clear: both;' />
710 * Display previous image link that has the same post parent.
713 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
714 * @param string $text Optional, default is false. If included, link will reflect $text variable.
715 * @return string HTML content.
717 function previous_image_link($size = 'thumbnail', $text = false) {
718 adjacent_image_link(true, $size, $text);
722 * Display next image link that has the same post parent.
725 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
726 * @param string $text Optional, default is false. If included, link will reflect $text variable.
727 * @return string HTML content.
729 function next_image_link($size = 'thumbnail', $text = false) {
730 adjacent_image_link(false, $size, $text);
734 * Display next or previous image link that has the same post parent.
736 * Retrieves the current attachment object from the $post global.
740 * @param bool $prev Optional. Default is true to display previous link, true for next.
742 function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
744 $post = get_post($post);
745 $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
747 foreach ( $attachments as $k => $attachment )
748 if ( $attachment->ID == $post->ID )
751 $k = $prev ? $k - 1 : $k + 1;
753 if ( isset($attachments[$k]) )
754 echo wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text);
758 * Retrieve taxonomies attached to the attachment.
762 * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
763 * @return array Empty array on failure. List of taxonomies on success.
765 function get_attachment_taxonomies($attachment) {
766 if ( is_int( $attachment ) )
767 $attachment = get_post($attachment);
768 else if ( is_array($attachment) )
769 $attachment = (object) $attachment;
771 if ( ! is_object($attachment) )
774 $filename = basename($attachment->guid);
776 $objects = array('attachment');
778 if ( false !== strpos($filename, '.') )
779 $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
780 if ( !empty($attachment->post_mime_type) ) {
781 $objects[] = 'attachment:' . $attachment->post_mime_type;
782 if ( false !== strpos($attachment->post_mime_type, '/') )
783 foreach ( explode('/', $attachment->post_mime_type) as $token )
784 if ( !empty($token) )
785 $objects[] = "attachment:$token";
788 $taxonomies = array();
789 foreach ( $objects as $object )
790 if ( $taxes = get_object_taxonomies($object) )
791 $taxonomies = array_merge($taxonomies, $taxes);
793 return array_unique($taxonomies);