[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0.1

Provided by Yoast

title

Body

[close]

/wp-admin/includes/ -> image.php (source)

   1  <?php
   2  /**
   3   * File contains all the administration image manipulation functions.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Create a thumbnail from an Image given a maximum side size.
  11   *
  12   * This function can handle most image file formats which PHP supports. If PHP
  13   * does not have the functionality to save in a file of the same format, the
  14   * thumbnail will be created as a jpeg.
  15   *
  16   * @since 1.2.0
  17   *
  18   * @param mixed $file Filename of the original image, Or attachment id.
  19   * @param int $max_side Maximum length of a single side for the thumbnail.
  20   * @param mixed $deprecated Never used.
  21   * @return string Thumbnail path on success, Error string on failure.
  22   */
  23  function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
  24      if ( !empty( $deprecated ) )
  25          _deprecated_argument( __FUNCTION__, '1.2' );
  26      $thumbpath = image_resize( $file, $max_side, $max_side );
  27      return apply_filters( 'wp_create_thumbnail', $thumbpath );
  28  }
  29  
  30  /**
  31   * Crop an Image to a given size.
  32   *
  33   * @since 2.1.0
  34   *
  35   * @param string|int $src_file The source file or Attachment ID.
  36   * @param int $src_x The start x position to crop from.
  37   * @param int $src_y The start y position to crop from.
  38   * @param int $src_w The width to crop.
  39   * @param int $src_h The height to crop.
  40   * @param int $dst_w The destination width.
  41   * @param int $dst_h The destination height.
  42   * @param int $src_abs Optional. If the source crop points are absolute.
  43   * @param string $dst_file Optional. The destination file to write to.
  44   * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
  45   */
  46  function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
  47      if ( is_numeric( $src_file ) ) // Handle int as attachment ID
  48          $src_file = get_attached_file( $src_file );
  49  
  50      $src = wp_load_image( $src_file );
  51  
  52      if ( !is_resource( $src ) )
  53          return new WP_Error( 'error_loading_image', $src, $src_file );
  54  
  55      $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
  56  
  57      if ( $src_abs ) {
  58          $src_w -= $src_x;
  59          $src_h -= $src_y;
  60      }
  61  
  62      if (function_exists('imageantialias'))
  63          imageantialias( $dst, true );
  64  
  65      imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
  66  
  67      imagedestroy( $src ); // Free up memory
  68  
  69      if ( ! $dst_file )
  70          $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
  71  
  72      $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
  73  
  74      if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
  75          return $dst_file;
  76      else
  77          return false;
  78  }
  79  
  80  /**
  81   * Generate post thumbnail attachment meta data.
  82   *
  83   * @since 2.1.0
  84   *
  85   * @param int $attachment_id Attachment Id to process.
  86   * @param string $file Filepath of the Attached image.
  87   * @return mixed Metadata for attachment.
  88   */
  89  function wp_generate_attachment_metadata( $attachment_id, $file ) {
  90      $attachment = get_post( $attachment_id );
  91  
  92      $metadata = array();
  93      if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
  94          $imagesize = getimagesize( $file );
  95          $metadata['width'] = $imagesize[0];
  96          $metadata['height'] = $imagesize[1];
  97          list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
  98          $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
  99  
 100          // Make the file path relative to the upload dir
 101          $metadata['file'] = _wp_relative_upload_path($file);
 102  
 103          // make thumbnails and other intermediate sizes
 104          global $_wp_additional_image_sizes;
 105  
 106          foreach ( get_intermediate_image_sizes() as $s ) {
 107              $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
 108              if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
 109                  $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
 110              else
 111                  $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
 112              if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
 113                  $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
 114              else
 115                  $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
 116              if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
 117                  $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
 118              else
 119                  $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
 120          }
 121  
 122          $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
 123  
 124          foreach ($sizes as $size => $size_data ) {
 125              $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
 126              if ( $resized )
 127                  $metadata['sizes'][$size] = $resized;
 128          }
 129  
 130          // fetch additional metadata from exif/iptc
 131          $image_meta = wp_read_image_metadata( $file );
 132          if ( $image_meta )
 133              $metadata['image_meta'] = $image_meta;
 134  
 135      }
 136  
 137      return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
 138  }
 139  
 140  /**
 141   * Calculated the new dimentions for a downsampled image.
 142   *
 143   * @since 2.0.0
 144   * @see wp_constrain_dimensions()
 145   *
 146   * @param int $width Current width of the image
 147   * @param int $height Current height of the image
 148   * @return mixed Array(height,width) of shrunk dimensions.
 149   */
 150  function get_udims( $width, $height) {
 151      return wp_constrain_dimensions( $width, $height, 128, 96 );
 152  }
 153  
 154  /**
 155   * Convert a fraction string to a decimal.
 156   *
 157   * @since 2.5.0
 158   *
 159   * @param string $str
 160   * @return int|float
 161   */
 162  function wp_exif_frac2dec($str) {
 163      @list( $n, $d ) = explode( '/', $str );
 164      if ( !empty($d) )
 165          return $n / $d;
 166      return $str;
 167  }
 168  
 169  /**
 170   * Convert the exif date format to a unix timestamp.
 171   *
 172   * @since 2.5.0
 173   *
 174   * @param string $str
 175   * @return int
 176   */
 177  function wp_exif_date2ts($str) {
 178      @list( $date, $time ) = explode( ' ', trim($str) );
 179      @list( $y, $m, $d ) = explode( ':', $date );
 180  
 181      return strtotime( "{$y}-{$m}-{$d} {$time}" );
 182  }
 183  
 184  /**
 185   * Get extended image metadata, exif or iptc as available.
 186   *
 187   * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
 188   * created_timestamp, focal_length, shutter_speed, and title.
 189   *
 190   * The IPTC metadata that is retrieved is APP13, credit, byline, created date
 191   * and time, caption, copyright, and title. Also includes FNumber, Model,
 192   * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
 193   *
 194   * @todo Try other exif libraries if available.
 195   * @since 2.5.0
 196   *
 197   * @param string $file
 198   * @return bool|array False on failure. Image metadata array on success.
 199   */
 200  function wp_read_image_metadata( $file ) {
 201      if ( ! file_exists( $file ) )
 202          return false;
 203  
 204      list( , , $sourceImageType ) = getimagesize( $file );
 205  
 206      // exif contains a bunch of data we'll probably never need formatted in ways
 207      // that are difficult to use. We'll normalize it and just extract the fields
 208      // that are likely to be useful.  Fractions and numbers are converted to
 209      // floats, dates to unix timestamps, and everything else to strings.
 210      $meta = array(
 211          'aperture' => 0,
 212          'credit' => '',
 213          'camera' => '',
 214          'caption' => '',
 215          'created_timestamp' => 0,
 216          'copyright' => '',
 217          'focal_length' => 0,
 218          'iso' => 0,
 219          'shutter_speed' => 0,
 220          'title' => '',
 221      );
 222  
 223      // read iptc first, since it might contain data not available in exif such
 224      // as caption, description etc
 225      if ( is_callable( 'iptcparse' ) ) {
 226          getimagesize( $file, $info );
 227  
 228          if ( ! empty( $info['APP13'] ) ) {
 229              $iptc = iptcparse( $info['APP13'] );
 230  
 231              // headline, "A brief synopsis of the caption."
 232              if ( ! empty( $iptc['2#105'][0] ) )
 233                  $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) );
 234              // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
 235              elseif ( ! empty( $iptc['2#005'][0] ) )
 236                  $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) );
 237  
 238              if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
 239                  $caption = utf8_encode( trim( $iptc['2#120'][0] ) );
 240                  if ( empty( $meta['title'] ) ) {
 241                      // Assume the title is stored in 2:120 if it's short.
 242                      if ( strlen( $caption ) < 80 )
 243                          $meta['title'] = $caption;
 244                      else
 245                          $meta['caption'] = $caption;
 246                  } elseif ( $caption != $meta['title'] ) {
 247                      $meta['caption'] = $caption;
 248                  }
 249              }
 250  
 251              if ( ! empty( $iptc['2#110'][0] ) ) // credit
 252                  $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
 253              elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
 254                  $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
 255  
 256              if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time
 257                  $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
 258  
 259              if ( ! empty( $iptc['2#116'][0] ) ) // copyright
 260                  $meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) );
 261           }
 262      }
 263  
 264      // fetch additional info from exif if available
 265      if ( is_callable( 'exif_read_data' ) && in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) {
 266          $exif = @exif_read_data( $file );
 267  
 268          if ( !empty( $exif['Title'] ) )
 269              $meta['title'] = utf8_encode( trim( $exif['Title'] ) );
 270  
 271          if ( ! empty( $exif['ImageDescription'] ) ) {
 272              if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
 273                  // Assume the title is stored in ImageDescription
 274                  $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) );
 275                  if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
 276                      $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) );
 277              } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
 278                  $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) );
 279              }
 280          } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
 281              $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) );
 282          }
 283  
 284          if ( ! empty( $exif['Artist'] ) )
 285              $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) );
 286          elseif ( ! empty($exif['Author'] ) )
 287              $meta['credit'] = utf8_encode( trim( $exif['Author'] ) );
 288  
 289          if ( ! empty( $exif['Copyright'] ) )
 290              $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) );
 291          if ( ! empty($exif['FNumber'] ) )
 292              $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
 293          if ( ! empty($exif['Model'] ) )
 294              $meta['camera'] = utf8_encode( trim( $exif['Model'] ) );
 295          if ( ! empty($exif['DateTimeDigitized'] ) )
 296              $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] );
 297          if ( ! empty($exif['FocalLength'] ) )
 298              $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
 299          if ( ! empty($exif['ISOSpeedRatings'] ) )
 300              $meta['iso'] = utf8_encode( trim( $exif['ISOSpeedRatings'] ) );
 301          if ( ! empty($exif['ExposureTime'] ) )
 302              $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
 303      }
 304  
 305      return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
 306  
 307  }
 308  
 309  /**
 310   * Validate that file is an image.
 311   *
 312   * @since 2.5.0
 313   *
 314   * @param string $path File path to test if valid image.
 315   * @return bool True if valid image, false if not valid image.
 316   */
 317  function file_is_valid_image($path) {
 318      $size = @getimagesize($path);
 319      return !empty($size);
 320  }
 321  
 322  /**
 323   * Validate that file is suitable for displaying within a web page.
 324   *
 325   * @since 2.5.0
 326   * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
 327   *
 328   * @param string $path File path to test.
 329   * @return bool True if suitable, false if not suitable.
 330   */
 331  function file_is_displayable_image($path) {
 332      $info = @getimagesize($path);
 333      if ( empty($info) )
 334          $result = false;
 335      elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )    // only gif, jpeg and png images can reliably be displayed
 336          $result = false;
 337      else
 338          $result = true;
 339  
 340      return apply_filters('file_is_displayable_image', $result, $path);
 341  }


Generated: Thu Oct 14 05:12:05 2010 Cross-referenced by PHPXref 0.7