-
+ 6ECEB2C3F3E1303063681E0B4EF7399AE15E33730711A61D53E6FB982F8AA20FF7C3740017F20953518A9F4275AE782AB5B46FA926C89725E36DA715D34D2F43
mp-wp/wp-admin/includes/image.php
(0 . 0)(1 . 330)
34115 <?php
34116 /**
34117 * File contains all the administration image manipulation functions.
34118 *
34119 * @package WordPress
34120 * @subpackage Administration
34121 */
34122
34123 /**
34124 * Create a thumbnail from an Image given a maximum side size.
34125 *
34126 * This function can handle most image file formats which PHP supports. If PHP
34127 * does not have the functionality to save in a file of the same format, the
34128 * thumbnail will be created as a jpeg.
34129 *
34130 * @since 1.2.0
34131 *
34132 * @param mixed $file Filename of the original image, Or attachment id.
34133 * @param int $max_side Maximum length of a single side for the thumbnail.
34134 * @return string Thumbnail path on success, Error string on failure.
34135 */
34136 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
34137 $thumbpath = image_resize( $file, $max_side, $max_side );
34138 return apply_filters( 'wp_create_thumbnail', $thumbpath );
34139 }
34140
34141 /**
34142 * Crop an Image to a given size.
34143 *
34144 * @since 2.1.0
34145 *
34146 * @param string|int $src_file The source file or Attachment ID.
34147 * @param int $src_x The start x position to crop from.
34148 * @param int $src_y The start y position to crop from.
34149 * @param int $src_w The width to crop.
34150 * @param int $src_h The height to crop.
34151 * @param int $dst_w The destination width.
34152 * @param int $dst_h The destination height.
34153 * @param int $src_abs Optional. If the source crop points are absolute.
34154 * @param string $dst_file Optional. The destination file to write to.
34155 * @return string New filepath on success, String error message on failure.
34156 */
34157 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
34158 if ( is_numeric( $src_file ) ) // Handle int as attachment ID
34159 $src_file = get_attached_file( $src_file );
34160
34161 $src = wp_load_image( $src_file );
34162
34163 if ( !is_resource( $src ))
34164 return $src;
34165
34166 $dst = imagecreatetruecolor( $dst_w, $dst_h );
34167
34168 if ( $src_abs ) {
34169 $src_w -= $src_x;
34170 $src_h -= $src_y;
34171 }
34172
34173 if (function_exists('imageantialias'))
34174 imageantialias( $dst, true );
34175
34176 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
34177
34178 imagedestroy( $src ); // Free up memory
34179
34180 if ( ! $dst_file )
34181 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
34182
34183 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
34184
34185 if ( imagejpeg( $dst, $dst_file ) )
34186 return $dst_file;
34187 else
34188 return false;
34189 }
34190
34191 /**
34192 * Generate post image attachment meta data.
34193 *
34194 * @since 2.1.0
34195 *
34196 * @param int $attachment_id Attachment Id to process.
34197 * @param string $file Filepath of the Attached image.
34198 * @return mixed Metadata for attachment.
34199 */
34200 function wp_generate_attachment_metadata( $attachment_id, $file ) {
34201 $attachment = get_post( $attachment_id );
34202
34203 $metadata = array();
34204 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
34205 $full_path_file = $file;
34206 $imagesize = getimagesize( $full_path_file );
34207 $metadata['width'] = $imagesize[0];
34208 $metadata['height'] = $imagesize[1];
34209 list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
34210 $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
34211
34212 // Make the file path relative to the upload dir
34213 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
34214 if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
34215 $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
34216 $file = ltrim($file, '/');
34217 }
34218 }
34219 $metadata['file'] = $file;
34220
34221 // make thumbnails and other intermediate sizes
34222 $sizes = array('thumbnail', 'medium', 'large');
34223 $sizes = apply_filters('intermediate_image_sizes', $sizes);
34224
34225 foreach ($sizes as $size) {
34226 $resized = image_make_intermediate_size( $full_path_file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
34227 if ( $resized )
34228 $metadata['sizes'][$size] = $resized;
34229 }
34230
34231 // fetch additional metadata from exif/iptc
34232 $image_meta = wp_read_image_metadata( $full_path_file );
34233 if ($image_meta)
34234 $metadata['image_meta'] = $image_meta;
34235
34236 }
34237
34238 return apply_filters( 'wp_generate_attachment_metadata', $metadata );
34239 }
34240
34241 /**
34242 * Load an image from a string, if PHP supports it.
34243 *
34244 * @since 2.1.0
34245 *
34246 * @param string $file Filename of the image to load.
34247 * @return resource The resulting image resource on success, Error string on failure.
34248 */
34249 function wp_load_image( $file ) {
34250 if ( is_numeric( $file ) )
34251 $file = get_attached_file( $file );
34252
34253 if ( ! file_exists( $file ) )
34254 return sprintf(__("File '%s' doesn't exist?"), $file);
34255
34256 if ( ! function_exists('imagecreatefromstring') )
34257 return __('The GD image library is not installed.');
34258
34259 // Set artificially high because GD uses uncompressed images in memory
34260 @ini_set('memory_limit', '256M');
34261 $image = imagecreatefromstring( file_get_contents( $file ) );
34262
34263 if ( !is_resource( $image ) )
34264 return sprintf(__("File '%s' is not an image."), $file);
34265
34266 return $image;
34267 }
34268
34269 /**
34270 * Calculated the new dimentions for a downsampled image.
34271 *
34272 * @since 2.0.0
34273 * @see wp_shrink_dimensions()
34274 *
34275 * @param int $width Current width of the image
34276 * @param int $height Current height of the image
34277 * @return mixed Array(height,width) of shrunk dimensions.
34278 */
34279 function get_udims( $width, $height) {
34280 return wp_shrink_dimensions( $width, $height );
34281 }
34282
34283 /**
34284 * Calculates the new dimentions for a downsampled image.
34285 *
34286 * @since 2.0.0
34287 * @see wp_constrain_dimensions()
34288 *
34289 * @param int $width Current width of the image
34290 * @param int $height Current height of the image
34291 * @param int $wmax Maximum wanted width
34292 * @param int $hmax Maximum wanted height
34293 * @return mixed Array(height,width) of shrunk dimensions.
34294 */
34295 function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
34296 return wp_constrain_dimensions( $width, $height, $wmax, $hmax );
34297 }
34298
34299 /**
34300 * Convert a fraction string to a decimal.
34301 *
34302 * @since 2.5.0
34303 *
34304 * @param string $str
34305 * @return int|float
34306 */
34307 function wp_exif_frac2dec($str) {
34308 @list( $n, $d ) = explode( '/', $str );
34309 if ( !empty($d) )
34310 return $n / $d;
34311 return $str;
34312 }
34313
34314 /**
34315 * Convert the exif date format to a unix timestamp.
34316 *
34317 * @since 2.5.0
34318 *
34319 * @param string $str
34320 * @return int
34321 */
34322 function wp_exif_date2ts($str) {
34323 @list( $date, $time ) = explode( ' ', trim($str) );
34324 @list( $y, $m, $d ) = explode( ':', $date );
34325
34326 return strtotime( "{$y}-{$m}-{$d} {$time}" );
34327 }
34328
34329 /**
34330 * Get extended image metadata, exif or iptc as available.
34331 *
34332 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
34333 * created_timestamp, focal_length, shutter_speed, and title.
34334 *
34335 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
34336 * and time, caption, copyright, and title. Also includes FNumber, Model,
34337 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
34338 *
34339 * @todo Try other exif libraries if available.
34340 * @since 2.5.0
34341 *
34342 * @param string $file
34343 * @return bool|array False on failure. Image metadata array on success.
34344 */
34345 function wp_read_image_metadata( $file ) {
34346 if ( !file_exists( $file ) )
34347 return false;
34348
34349 list(,,$sourceImageType) = getimagesize( $file );
34350
34351 // exif contains a bunch of data we'll probably never need formatted in ways
34352 // that are difficult to use. We'll normalize it and just extract the fields
34353 // that are likely to be useful. Fractions and numbers are converted to
34354 // floats, dates to unix timestamps, and everything else to strings.
34355 $meta = array(
34356 'aperture' => 0,
34357 'credit' => '',
34358 'camera' => '',
34359 'caption' => '',
34360 'created_timestamp' => 0,
34361 'copyright' => '',
34362 'focal_length' => 0,
34363 'iso' => 0,
34364 'shutter_speed' => 0,
34365 'title' => '',
34366 );
34367
34368 // read iptc first, since it might contain data not available in exif such
34369 // as caption, description etc
34370 if ( is_callable('iptcparse') ) {
34371 getimagesize($file, $info);
34372 if ( !empty($info['APP13']) ) {
34373 $iptc = iptcparse($info['APP13']);
34374 if ( !empty($iptc['2#110'][0]) ) // credit
34375 $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
34376 elseif ( !empty($iptc['2#080'][0]) ) // byline
34377 $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
34378 if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time
34379 $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
34380 if ( !empty($iptc['2#120'][0]) ) // caption
34381 $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
34382 if ( !empty($iptc['2#116'][0]) ) // copyright
34383 $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
34384 if ( !empty($iptc['2#005'][0]) ) // title
34385 $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));
34386 }
34387 }
34388
34389 // fetch additional info from exif if available
34390 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)) ) ) {
34391 $exif = @exif_read_data( $file );
34392 if (!empty($exif['FNumber']))
34393 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
34394 if (!empty($exif['Model']))
34395 $meta['camera'] = trim( $exif['Model'] );
34396 if (!empty($exif['DateTimeDigitized']))
34397 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
34398 if (!empty($exif['FocalLength']))
34399 $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
34400 if (!empty($exif['ISOSpeedRatings']))
34401 $meta['iso'] = $exif['ISOSpeedRatings'];
34402 if (!empty($exif['ExposureTime']))
34403 $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
34404 }
34405
34406 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
34407
34408 }
34409
34410 /**
34411 * Validate that file is an image.
34412 *
34413 * @since 2.5.0
34414 *
34415 * @param string $path File path to test if valid image.
34416 * @return bool True if valid image, false if not valid image.
34417 */
34418 function file_is_valid_image($path) {
34419 $size = @getimagesize($path);
34420 return !empty($size);
34421 }
34422
34423 /**
34424 * Validate that file is suitable for displaying within a web page.
34425 *
34426 * @since 2.5.0
34427 * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
34428 *
34429 * @param string $path File path to test.
34430 * @return bool True if suitable, false if not suitable.
34431 */
34432 function file_is_displayable_image($path) {
34433 $info = @getimagesize($path);
34434 if ( empty($info) )
34435 $result = false;
34436 elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed
34437 $result = false;
34438 else
34439 $result = true;
34440
34441 return apply_filters('file_is_displayable_image', $result, $path);
34442 }
34443
34444 ?>