[ Index ]

PHP Cross Reference of Wordpress 2.7.1

title

Body

[close]

/wp-includes/ -> media.php (source)

   1  <?php
   2  /**
   3   * WordPress API for media display.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Scale down the default size of an image.
  10   *
  11   * This is so that the image is a better fit for the editor and theme.
  12   *
  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
  18   * not set.
  19   *
  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.
  25   *
  26   * @since 2.5.0
  27   * @uses wp_constrain_dimensions() This function passes the widths and the heights.
  28   *
  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.
  33   */
  34  function image_constrain_size_for_editor($width, $height, $size = 'medium') {
  35      global $content_width;
  36  
  37      if ( is_array($size) ) {
  38          $max_width = $size[0];
  39          $max_height = $size[1];
  40      }
  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 ) {
  46              $max_width = 128;
  47              $max_height = 96;
  48          }
  49      }
  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
  54      }
  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 );
  64      }
  65      // $size == 'full' has no constraint
  66      else {
  67          $max_width = $width;
  68          $max_height = $height;
  69      }
  70  
  71      list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );
  72  
  73      return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
  74  }
  75  
  76  /**
  77   * Retrieve width and height attributes using given width and height values.
  78   *
  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.
  82   *
  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
  85   * the return.
  86   *
  87   * @since 2.5.0
  88   *
  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.
  92   */
  93  function image_hwstring($width, $height) {
  94      $out = '';
  95      if ($width)
  96          $out .= 'width="'.intval($width).'" ';
  97      if ($height)
  98          $out .= 'height="'.intval($height).'" ';
  99      return $out;
 100  }
 101  
 102  /**
 103   * Scale an image to fit a particular size (such as 'thumb' or 'medium').
 104   *
 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.
 108   *
 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.
 112   *
 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.
 117   *
 118   * @since 2.5.0
 119   * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
 120   *        resize services.
 121   *
 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.
 125   */
 126  function image_downsize($id, $size = 'medium') {
 127  
 128      if ( !wp_attachment_is_image($id) )
 129          return false;
 130  
 131      $img_url = wp_get_attachment_url($id);
 132      $meta = wp_get_attachment_metadata($id);
 133      $width = $height = 0;
 134      $is_intermediate = false;
 135  
 136      // plugins can use this to provide resize services
 137      if ( $out = apply_filters('image_downsize', false, $id, $size) )
 138          return $out;
 139  
 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;
 146      }
 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);
 151              $width = $info[0];
 152              $height = $info[1];
 153              $is_intermediate = true;
 154          }
 155      }
 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'];
 160      }
 161  
 162      if ( $img_url) {
 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 );
 165  
 166          return array( $img_url, $width, $height, $is_intermediate );
 167      }
 168      return false;
 169  
 170  }
 171  
 172  /**
 173   * An <img src /> tag for an image attachment, scaling it down if requested.
 174   *
 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.
 179   *
 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
 182   * content.
 183   *
 184   * @since 2.5.0
 185   *
 186   * @uses apply_filters() The 'get_image_tag_class' filter is the IMG element
 187   *        class attribute.
 188   * @uses apply_filters() The 'get_image_tag' filter is the full IMG element with
 189   *        all attributes.
 190   *
 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
 197   */
 198  function get_image_tag($id, $alt, $title, $align, $size='medium') {
 199  
 200      list( $img_src, $width, $height ) = image_downsize($id, $size);
 201      $hwstring = image_hwstring($width, $height);
 202  
 203      $class = 'align'.attribute_escape($align).' size-'.attribute_escape($size).' wp-image-'.$id;
 204      $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
 205  
 206      $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'" '.$hwstring.'class="'.$class.'" />';
 207  
 208      $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
 209  
 210      return $html;
 211  }
 212  
 213  /**
 214   * Calculates the new dimentions for a downsampled image.
 215   *
 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
 218   * that dimension.
 219   *
 220   * @since 2.5.0
 221   *
 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.
 227   */
 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 );
 231  
 232      $width_ratio = $height_ratio = 1.0;
 233  
 234      if ( $max_width > 0 && $current_width > $max_width )
 235          $width_ratio = $max_width / $current_width;
 236  
 237      if ( $max_height > 0 && $current_height > $max_height )
 238          $height_ratio = $max_height / $current_height;
 239  
 240      // the smaller ratio is the one we need to fit it to the constraining box
 241      $ratio = min( $width_ratio, $height_ratio );
 242  
 243      return array( intval($current_width * $ratio), intval($current_height * $ratio) );
 244  }
 245  
 246  /**
 247   * Retrieve calculated resized dimensions for use in imagecopyresampled().
 248   *
 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.
 252   *
 253   * @since 2.5.0
 254   *
 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.
 261   */
 262  function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
 263  
 264      if ($orig_w <= 0 || $orig_h <= 0)
 265          return false;
 266      // at least one of dest_w or dest_h must be specific
 267      if ($dest_w <= 0 && $dest_h <= 0)
 268          return false;
 269  
 270      if ( $crop ) {
 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);
 275          if (!$new_w) {
 276              $new_w = intval($new_h * $aspect_ratio);
 277          }
 278          if (!$new_h) {
 279              $new_h = intval($new_w / $aspect_ratio);
 280          }
 281  
 282          $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
 283  
 284          $crop_w = ceil($new_w / $size_ratio);
 285          $crop_h = ceil($new_h / $size_ratio);
 286  
 287          $s_x = floor(($orig_w - $crop_w)/2);
 288          $s_y = floor(($orig_h - $crop_h)/2);
 289      }
 290      else {
 291          // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
 292          $crop_w = $orig_w;
 293          $crop_h = $orig_h;
 294  
 295          $s_x = 0;
 296          $s_y = 0;
 297  
 298          list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
 299      }
 300  
 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)
 303          return false;
 304  
 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);
 308  
 309  }
 310  
 311  /**
 312   * Scale down an image to fit a particular size and save a new copy of the image.
 313   *
 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.
 317   *
 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.
 321   *
 322   * @since 2.5.0
 323   *
 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()}
 332   */
 333  function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
 334  
 335      $image = wp_load_image( $file );
 336      if ( !is_resource( $image ) )
 337          return new WP_Error('error_loading_image', $image);
 338  
 339      list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
 340      $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
 341      if (!$dims)
 342          return $dims;
 343      list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
 344  
 345      $newimage = imagecreatetruecolor( $dst_w, $dst_h);
 346  
 347      // preserve PNG transparency
 348      if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
 349          imagealphablending( $newimage, false);
 350          imagesavealpha( $newimage, true);
 351      }
 352  
 353      imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 354  
 355      // we don't need the original in memory anymore
 356      imagedestroy( $image );
 357  
 358      // $suffix will be appended to the destination filename, just before the extension
 359      if ( !$suffix )
 360          $suffix = "{$dst_w}x{$dst_h}";
 361  
 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) )
 367          $dir = $_dest_path;
 368      $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
 369  
 370      if ( $orig_type == IMAGETYPE_GIF ) {
 371          if (!imagegif( $newimage, $destfilename ) )
 372              return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
 373      }
 374      elseif ( $orig_type == IMAGETYPE_PNG ) {
 375          if (!imagepng( $newimage, $destfilename ) )
 376              return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
 377      }
 378      else {
 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 ) ) )
 382              return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
 383      }
 384  
 385      imagedestroy( $newimage );
 386  
 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 );
 391  
 392      return $destfilename;
 393  }
 394  
 395  /**
 396   * Resize an image to make a thumbnail or intermediate size.
 397   *
 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.
 401   *
 402   * @since 2.5.0
 403   *
 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.
 409   */
 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);
 415              return array(
 416                  'file' => basename( $resized_file ),
 417                  'width' => $info[0],
 418                  'height' => $info[1],
 419              );
 420          }
 421      }
 422      return false;
 423  }
 424  
 425  /**
 426   * Retrieve the image's intermediate size (resized) path, width, and height.
 427   *
 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.
 433   *
 434   * The metadata 'sizes' is used for compatible sizes that can be used for the
 435   * parameter $size value.
 436   *
 437   * The url path will be given, when the $size parameter is a string.
 438   *
 439   * @since 2.5.0
 440   *
 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.
 444   */
 445  function image_get_intermediate_size($post_id, $size='thumbnail') {
 446      if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
 447          return false;
 448  
 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' );
 457              }
 458              // add to lookup table: area => size
 459              $areas[$data['width'] * $data['height']] = $_size;
 460          }
 461          if ( !$size || !empty($areas) ) {
 462              // find for the smallest image not smaller than the desired size
 463              ksort($areas);
 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' );
 470                  }
 471              }
 472          }
 473      }
 474  
 475      if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
 476          return false;
 477  
 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'] );
 484      }
 485      return $data;
 486  }
 487  
 488  /**
 489   * Retrieve an image to represent an attachment.
 490   *
 491   * A mime icon for files, thumbnail or intermediate size for images.
 492   *
 493   * @since 2.5.0
 494   *
 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.
 499   */
 500  function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
 501  
 502      // get a thumbnail or intermediate image if there is one
 503      if ( $image = image_downsize($attachment_id, $size) )
 504          return $image;
 505  
 506      if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
 507          $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
 508          $src_file = $icon_dir . '/' . basename($src);
 509          @list($width, $height) = getimagesize($src_file);
 510      }
 511      if ( $src && $width && $height )
 512          return array( $src, $width, $height );
 513      return false;
 514  }
 515  
 516  /**
 517   * Retrieve img HTML content for an image to represent an attachment.
 518   *
 519   * @see wp_get_attachment_image_src() Returns img HTML element based on array.
 520   * @since 2.5.0
 521   *
 522   * @param int $attachment_id Image attachment ID.
 523   * @param string $size Optional, default is 'thumbnail'.
 524   * @param bool $icon Optional, default is false. Whether it is an icon.
 525   * @return string HTML img element or empty string on failure.
 526   */
 527  function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) {
 528  
 529      $html = '';
 530      $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
 531      if ( $image ) {
 532          list($src, $width, $height) = $image;
 533          $hwstring = image_hwstring($width, $height);
 534          if ( is_array($size) )
 535              $size = join('x', $size);
 536          $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';
 537      }
 538  
 539      return $html;
 540  }
 541  
 542  add_shortcode('wp_caption', 'img_caption_shortcode');
 543  add_shortcode('caption', 'img_caption_shortcode');
 544  
 545  /**
 546   * The Caption shortcode.
 547   *
 548   * Allows a plugin to replace the content that would otherwise be returned. The
 549   * filter is 'img_caption_shortcode' and passes an empty string, the attr
 550   * parameter and the content parameter values.
 551   *
 552   * The supported attributes for the shortcode are 'id', 'align', 'width', and
 553   * 'caption'.
 554   *
 555   * @since 2.6.0
 556   *
 557   * @param array $attr Attributes attributed to the shortcode.
 558   * @param string $content Optional. Shortcode content.
 559   * @return string
 560   */
 561  function img_caption_shortcode($attr, $content = null) {
 562  
 563      // Allow plugins/themes to override the default caption template.
 564      $output = apply_filters('img_caption_shortcode', '', $attr, $content);
 565      if ( $output != '' )
 566          return $output;
 567  
 568      extract(shortcode_atts(array(
 569          'id'    => '',
 570          'align'    => 'alignnone',
 571          'width'    => '',
 572          'caption' => ''
 573      ), $attr));
 574  
 575      if ( 1 > (int) $width || empty($caption) )
 576          return $content;
 577  
 578      if ( $id ) $id = 'id="' . $id . '" ';
 579  
 580      return '<div ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px">'
 581      . $content . '<p class="wp-caption-text">' . $caption . '</p></div>';
 582  }
 583  
 584  add_shortcode('gallery', 'gallery_shortcode');
 585  
 586  /**
 587   * The Gallery shortcode.
 588   *
 589   * This implements the functionality of the Gallery Shortcode for displaying
 590   * WordPress images on a post.
 591   *
 592   * @since 2.5.0
 593   *
 594   * @param array $attr Attributes attributed to the shortcode.
 595   * @return string HTML content to display gallery.
 596   */
 597  function gallery_shortcode($attr) {
 598      global $post;
 599  
 600      // Allow plugins/themes to override the default gallery template.
 601      $output = apply_filters('post_gallery', '', $attr);
 602      if ( $output != '' )
 603          return $output;
 604  
 605      // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
 606      if ( isset( $attr['orderby'] ) ) {
 607          $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
 608          if ( !$attr['orderby'] )
 609              unset( $attr['orderby'] );
 610      }
 611  
 612      extract(shortcode_atts(array(
 613          'order'      => 'ASC',
 614          'orderby'    => 'menu_order ID',
 615          'id'         => $post->ID,
 616          'itemtag'    => 'dl',
 617          'icontag'    => 'dt',
 618          'captiontag' => 'dd',
 619          'columns'    => 3,
 620          'size'       => 'thumbnail'
 621      ), $attr));
 622  
 623      $id = intval($id);
 624      $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
 625  
 626      if ( empty($attachments) )
 627          return '';
 628  
 629      if ( is_feed() ) {
 630          $output = "\n";
 631          foreach ( $attachments as $id => $attachment )
 632              $output .= wp_get_attachment_link($id, $size, true) . "\n";
 633          return $output;
 634      }
 635  
 636      $itemtag = tag_escape($itemtag);
 637      $captiontag = tag_escape($captiontag);
 638      $columns = intval($columns);
 639      $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
 640  
 641      $output = apply_filters('gallery_style', "
 642          <style type='text/css'>
 643              .gallery {
 644                  margin: auto;
 645              }
 646              .gallery-item {
 647                  float: left;
 648                  margin-top: 10px;
 649                  text-align: center;
 650                  width: {$itemwidth}%;            }
 651              .gallery img {
 652                  border: 2px solid #cfcfcf;
 653              }
 654              .gallery-caption {
 655                  margin-left: 0;
 656              }
 657          </style>
 658          <!-- see gallery_shortcode() in wp-includes/media.php -->
 659          <div class='gallery'>");
 660  
 661      $i = 0;
 662      foreach ( $attachments as $id => $attachment ) {
 663          $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
 664  
 665          $output .= "<{$itemtag} class='gallery-item'>";
 666          $output .= "
 667              <{$icontag} class='gallery-icon'>
 668                  $link
 669              </{$icontag}>";
 670          if ( $captiontag && trim($attachment->post_excerpt) ) {
 671              $output .= "
 672                  <{$captiontag} class='gallery-caption'>
 673                  {$attachment->post_excerpt}
 674                  </{$captiontag}>";
 675          }
 676          $output .= "</{$itemtag}>";
 677          if ( $columns > 0 && ++$i % $columns == 0 )
 678              $output .= '<br style="clear: both" />';
 679      }
 680  
 681      $output .= "
 682              <br style='clear: both;' />
 683          </div>\n";
 684  
 685      return $output;
 686  }
 687  
 688  /**
 689   * Display previous image link that has the same post parent.
 690   *
 691   * @since 2.5.0
 692   */
 693  function previous_image_link() {
 694      adjacent_image_link(true);
 695  }
 696  
 697  /**
 698   * Display next image link that has the same post parent.
 699   *
 700   * @since 2.5.0
 701   */
 702  function next_image_link() {
 703      adjacent_image_link(false);
 704  }
 705  
 706  /**
 707   * Display next or previous image link that has the same post parent.
 708   *
 709   * Retrieves the current attachment object from the $post global.
 710   *
 711   * @since 2.5.0
 712   *
 713   * @param bool $prev Optional. Default is true to display previous link, true for next.
 714   */
 715  function adjacent_image_link($prev = true) {
 716      global $post;
 717      $post = get_post($post);
 718      $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') ));
 719  
 720      foreach ( $attachments as $k => $attachment )
 721          if ( $attachment->ID == $post->ID )
 722              break;
 723  
 724      $k = $prev ? $k - 1 : $k + 1;
 725  
 726      if ( isset($attachments[$k]) )
 727          echo wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
 728  }
 729  
 730  /**
 731   * Retrieve taxonomies attached to the attachment.
 732   *
 733   * @since 2.5.0
 734   *
 735   * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
 736   * @return array Empty array on failure. List of taxonomies on success.
 737   */
 738  function get_attachment_taxonomies($attachment) {
 739      if ( is_int( $attachment ) )
 740          $attachment = get_post($attachment);
 741      else if ( is_array($attachment) )
 742          $attachment = (object) $attachment;
 743  
 744      if ( ! is_object($attachment) )
 745          return array();
 746  
 747      $filename = basename($attachment->guid);
 748  
 749      $objects = array('attachment');
 750  
 751      if ( false !== strpos($filename, '.') )
 752          $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
 753      if ( !empty($attachment->post_mime_type) ) {
 754          $objects[] = 'attachment:' . $attachment->post_mime_type;
 755          if ( false !== strpos($attachment->post_mime_type, '/') )
 756              foreach ( explode('/', $attachment->post_mime_type) as $token )
 757                  if ( !empty($token) )
 758                      $objects[] = "attachment:$token";
 759      }
 760  
 761      $taxonomies = array();
 762      foreach ( $objects as $object )
 763          if ( $taxes = get_object_taxonomies($object) )
 764              $taxonomies = array_merge($taxonomies, $taxes);
 765  
 766      return array_unique($taxonomies);
 767  }
 768  
 769  ?>


Generated: Mon Mar 23 16:23:02 2009 Cross-referenced by PHPXref 0.7