web/wp-admin/includes/image.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     3  * File contains all the administration image manipulation functions.
     3  * File contains all the administration image manipulation functions.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  */
     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 
     8 
    30 /**
     9 /**
    31  * Crop an Image to a given size.
    10  * Crop an Image to a given size.
    32  *
    11  *
    33  * @since 2.1.0
    12  * @since 2.1.0
    42  * @param int $src_abs Optional. If the source crop points are absolute.
    21  * @param int $src_abs Optional. If the source crop points are absolute.
    43  * @param string $dst_file Optional. The destination file to write to.
    22  * @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.
    23  * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
    45  */
    24  */
    46 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
    25 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
       
    26 	$src_file = $src;
    47 	if ( is_numeric( $src ) ) { // Handle int as attachment ID
    27 	if ( is_numeric( $src ) ) { // Handle int as attachment ID
    48 		$src_file = get_attached_file( $src );
    28 		$src_file = get_attached_file( $src );
       
    29 
    49 		if ( ! file_exists( $src_file ) ) {
    30 		if ( ! file_exists( $src_file ) ) {
    50 			// If the file doesn't exist, attempt a url fopen on the src link.
    31 			// If the file doesn't exist, attempt a url fopen on the src link.
    51 			// This can occur with certain file replication plugins.
    32 			// This can occur with certain file replication plugins.
    52 			$post = get_post( $src );
    33 			$src = _load_image_to_edit_path( $src, 'full' );
    53 			$image_type = $post->post_mime_type;
       
    54 			$src = load_image_to_edit( $src, $post->post_mime_type, 'full' );
       
    55 		} else {
    34 		} else {
    56 			$size = @getimagesize( $src_file );
    35 			$src = $src_file;
    57 			$image_type = ( $size ) ? $size['mime'] : '';
    36 		}
    58 			$src = wp_load_image( $src_file );
    37 	}
    59 		}
    38 
    60 	} else {
    39 	$editor = wp_get_image_editor( $src );
    61 		$size = @getimagesize( $src );
    40 	if ( is_wp_error( $editor ) )
    62 		$image_type = ( $size ) ? $size['mime'] : '';
    41 		return $editor;
    63 		$src = wp_load_image( $src );
    42 
    64 	}
    43 	$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
    65 
    44 	if ( is_wp_error( $src ) )
    66 	if ( ! is_resource( $src ) )
    45 		return $src;
    67 		return new WP_Error( 'error_loading_image', $src, $src_file );
       
    68 
       
    69 	$dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
       
    70 
       
    71 	if ( $src_abs ) {
       
    72 		$src_w -= $src_x;
       
    73 		$src_h -= $src_y;
       
    74 	}
       
    75 
       
    76 	if ( function_exists( 'imageantialias' ) )
       
    77 		imageantialias( $dst, true );
       
    78 
       
    79 	imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
       
    80 
       
    81 	imagedestroy( $src ); // Free up memory
       
    82 
    46 
    83 	if ( ! $dst_file )
    47 	if ( ! $dst_file )
    84 		$dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    48 		$dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    85 
       
    86 	if ( 'image/png' != $image_type )
       
    87 		$dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
       
    88 
    49 
    89 	// The directory containing the original file may no longer exist when
    50 	// The directory containing the original file may no longer exist when
    90 	// using a replication plugin.
    51 	// using a replication plugin.
    91 	wp_mkdir_p( dirname( $dst_file ) );
    52 	wp_mkdir_p( dirname( $dst_file ) );
    92 
    53 
    93 	$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
    54 	$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
    94 
    55 
    95 	if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) )
    56 	$result = $editor->save( $dst_file );
    96 		return $dst_file;
    57 	return $dst_file;
    97 	elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
       
    98 		return $dst_file;
       
    99 	else
       
   100 		return false;
       
   101 }
    58 }
   102 
    59 
   103 /**
    60 /**
   104  * Generate post thumbnail attachment meta data.
    61  * Generate post thumbnail attachment meta data.
   105  *
    62  *
   115 	$metadata = array();
    72 	$metadata = array();
   116 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    73 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
   117 		$imagesize = getimagesize( $file );
    74 		$imagesize = getimagesize( $file );
   118 		$metadata['width'] = $imagesize[0];
    75 		$metadata['width'] = $imagesize[0];
   119 		$metadata['height'] = $imagesize[1];
    76 		$metadata['height'] = $imagesize[1];
   120 		list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
       
   121 		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
       
   122 
    77 
   123 		// Make the file path relative to the upload dir
    78 		// Make the file path relative to the upload dir
   124 		$metadata['file'] = _wp_relative_upload_path($file);
    79 		$metadata['file'] = _wp_relative_upload_path($file);
   125 
    80 
   126 		// make thumbnails and other intermediate sizes
    81 		// make thumbnails and other intermediate sizes
   142 				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
    97 				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
   143 		}
    98 		}
   144 
    99 
   145 		$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
   100 		$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
   146 
   101 
   147 		foreach ($sizes as $size => $size_data ) {
   102 		if ( $sizes ) {
   148 			$resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
   103 			$editor = wp_get_image_editor( $file );
   149 			if ( $resized )
   104 
   150 				$metadata['sizes'][$size] = $resized;
   105 			if ( ! is_wp_error( $editor ) )
       
   106 				$metadata['sizes'] = $editor->multi_resize( $sizes );
       
   107 		} else {
       
   108 			$metadata['sizes'] = array();
   151 		}
   109 		}
   152 
   110 
   153 		// fetch additional metadata from exif/iptc
   111 		// fetch additional metadata from exif/iptc
   154 		$image_meta = wp_read_image_metadata( $file );
   112 		$image_meta = wp_read_image_metadata( $file );
   155 		if ( $image_meta )
   113 		if ( $image_meta )
   156 			$metadata['image_meta'] = $image_meta;
   114 			$metadata['image_meta'] = $image_meta;
   157 
   115 
   158 	}
   116 	}
   159 
   117 
   160 	return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
   118 	return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
   161 }
       
   162 
       
   163 /**
       
   164  * Calculated the new dimensions for a downsampled image.
       
   165  *
       
   166  * @since 2.0.0
       
   167  * @see wp_constrain_dimensions()
       
   168  *
       
   169  * @param int $width Current width of the image
       
   170  * @param int $height Current height of the image
       
   171  * @return mixed Array(height,width) of shrunk dimensions.
       
   172  */
       
   173 function get_udims( $width, $height) {
       
   174 	return wp_constrain_dimensions( $width, $height, 128, 96 );
       
   175 }
   119 }
   176 
   120 
   177 /**
   121 /**
   178  * Convert a fraction string to a decimal.
   122  * Convert a fraction string to a decimal.
   179  *
   123  *
   251 		if ( ! empty( $info['APP13'] ) ) {
   195 		if ( ! empty( $info['APP13'] ) ) {
   252 			$iptc = iptcparse( $info['APP13'] );
   196 			$iptc = iptcparse( $info['APP13'] );
   253 
   197 
   254 			// headline, "A brief synopsis of the caption."
   198 			// headline, "A brief synopsis of the caption."
   255 			if ( ! empty( $iptc['2#105'][0] ) )
   199 			if ( ! empty( $iptc['2#105'][0] ) )
   256 				$meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) );
   200 				$meta['title'] = trim( $iptc['2#105'][0] );
   257 			// title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
   201 			// title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
   258 			elseif ( ! empty( $iptc['2#005'][0] ) )
   202 			elseif ( ! empty( $iptc['2#005'][0] ) )
   259 				$meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) );
   203 				$meta['title'] = trim( $iptc['2#005'][0] );
   260 
   204 
   261 			if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
   205 			if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
   262 				$caption = utf8_encode( trim( $iptc['2#120'][0] ) );
   206 				$caption = trim( $iptc['2#120'][0] );
   263 				if ( empty( $meta['title'] ) ) {
   207 				if ( empty( $meta['title'] ) ) {
   264 					// Assume the title is stored in 2:120 if it's short.
   208 					// Assume the title is stored in 2:120 if it's short.
   265 					if ( strlen( $caption ) < 80 )
   209 					if ( strlen( $caption ) < 80 )
   266 						$meta['title'] = $caption;
   210 						$meta['title'] = $caption;
   267 					else
   211 					else
   270 					$meta['caption'] = $caption;
   214 					$meta['caption'] = $caption;
   271 				}
   215 				}
   272 			}
   216 			}
   273 
   217 
   274 			if ( ! empty( $iptc['2#110'][0] ) ) // credit
   218 			if ( ! empty( $iptc['2#110'][0] ) ) // credit
   275 				$meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
   219 				$meta['credit'] = trim( $iptc['2#110'][0] );
   276 			elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
   220 			elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
   277 				$meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
   221 				$meta['credit'] = trim( $iptc['2#080'][0] );
   278 
   222 
   279 			if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time
   223 			if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time
   280 				$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
   224 				$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
   281 
   225 
   282 			if ( ! empty( $iptc['2#116'][0] ) ) // copyright
   226 			if ( ! empty( $iptc['2#116'][0] ) ) // copyright
   283 				$meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) );
   227 				$meta['copyright'] = trim( $iptc['2#116'][0] );
   284 		 }
   228 		 }
   285 	}
   229 	}
   286 
   230 
   287 	// fetch additional info from exif if available
   231 	// fetch additional info from exif if available
   288 	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 ) ) ) ) {
   232 	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 ) ) ) ) {
   289 		$exif = @exif_read_data( $file );
   233 		$exif = @exif_read_data( $file );
   290 
   234 
   291 		if ( !empty( $exif['Title'] ) )
   235 		if ( !empty( $exif['Title'] ) )
   292 			$meta['title'] = utf8_encode( trim( $exif['Title'] ) );
   236 			$meta['title'] = trim( $exif['Title'] );
   293 
   237 
   294 		if ( ! empty( $exif['ImageDescription'] ) ) {
   238 		if ( ! empty( $exif['ImageDescription'] ) ) {
   295 			if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
   239 			if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
   296 				// Assume the title is stored in ImageDescription
   240 				// Assume the title is stored in ImageDescription
   297 				$meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) );
   241 				$meta['title'] = trim( $exif['ImageDescription'] );
   298 				if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
   242 				if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
   299 					$meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) );
   243 					$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
   300 			} elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
   244 			} elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
   301 				$meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) );
   245 				$meta['caption'] = trim( $exif['ImageDescription'] );
   302 			}
   246 			}
   303 		} elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
   247 		} elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
   304 			$meta['caption'] = utf8_encode( trim( $exif['Comments'] ) );
   248 			$meta['caption'] = trim( $exif['Comments'] );
   305 		}
   249 		}
   306 
   250 
   307 		if ( ! empty( $exif['Artist'] ) )
   251 		if ( ! empty( $exif['Artist'] ) )
   308 			$meta['credit'] = utf8_encode( trim( $exif['Artist'] ) );
   252 			$meta['credit'] = trim( $exif['Artist'] );
   309 		elseif ( ! empty($exif['Author'] ) )
   253 		elseif ( ! empty($exif['Author'] ) )
   310 			$meta['credit'] = utf8_encode( trim( $exif['Author'] ) );
   254 			$meta['credit'] = trim( $exif['Author'] );
   311 
   255 
   312 		if ( ! empty( $exif['Copyright'] ) )
   256 		if ( ! empty( $exif['Copyright'] ) )
   313 			$meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) );
   257 			$meta['copyright'] = trim( $exif['Copyright'] );
   314 		if ( ! empty($exif['FNumber'] ) )
   258 		if ( ! empty($exif['FNumber'] ) )
   315 			$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
   259 			$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
   316 		if ( ! empty($exif['Model'] ) )
   260 		if ( ! empty($exif['Model'] ) )
   317 			$meta['camera'] = utf8_encode( trim( $exif['Model'] ) );
   261 			$meta['camera'] = trim( $exif['Model'] );
   318 		if ( ! empty($exif['DateTimeDigitized'] ) )
   262 		if ( ! empty($exif['DateTimeDigitized'] ) )
   319 			$meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] );
   263 			$meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] );
   320 		if ( ! empty($exif['FocalLength'] ) )
   264 		if ( ! empty($exif['FocalLength'] ) )
   321 			$meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
   265 			$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
   322 		if ( ! empty($exif['ISOSpeedRatings'] ) ) {
   266 		if ( ! empty($exif['ISOSpeedRatings'] ) ) {
   323 			$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
   267 			$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
   324 			$meta['iso'] = utf8_encode( trim( $meta['iso'] ) );
   268 			$meta['iso'] = trim( $meta['iso'] );
   325 		}
   269 		}
   326 		if ( ! empty($exif['ExposureTime'] ) )
   270 		if ( ! empty($exif['ExposureTime'] ) )
   327 			$meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
   271 			$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
       
   272 	}
       
   273 
       
   274 	foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
       
   275 		if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) )
       
   276 			$meta[ $key ] = utf8_encode( $meta[ $key ] );
   328 	}
   277 	}
   329 
   278 
   330 	return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
   279 	return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
   331 
   280 
   332 }
   281 }