wp/wp-admin/includes/image.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    45 		return $src;
    45 		return $src;
    46 
    46 
    47 	if ( ! $dst_file )
    47 	if ( ! $dst_file )
    48 		$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 );
    49 
    49 
    50 	// The directory containing the original file may no longer exist when
    50 	/*
    51 	// using a replication plugin.
    51 	 * The directory containing the original file may no longer exist when
       
    52 	 * using a replication plugin.
       
    53 	 */
    52 	wp_mkdir_p( dirname( $dst_file ) );
    54 	wp_mkdir_p( dirname( $dst_file ) );
    53 
    55 
    54 	$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
    56 	$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
    55 
    57 
    56 	$result = $editor->save( $dst_file );
    58 	$result = $editor->save( $dst_file );
    77 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    79 	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    78 		$imagesize = getimagesize( $file );
    80 		$imagesize = getimagesize( $file );
    79 		$metadata['width'] = $imagesize[0];
    81 		$metadata['width'] = $imagesize[0];
    80 		$metadata['height'] = $imagesize[1];
    82 		$metadata['height'] = $imagesize[1];
    81 
    83 
    82 		// Make the file path relative to the upload dir
    84 		// Make the file path relative to the upload dir.
    83 		$metadata['file'] = _wp_relative_upload_path($file);
    85 		$metadata['file'] = _wp_relative_upload_path($file);
    84 
    86 
    85 		// make thumbnails and other intermediate sizes
    87 		// Make thumbnails and other intermediate sizes.
    86 		global $_wp_additional_image_sizes;
    88 		global $_wp_additional_image_sizes;
    87 
    89 
    88 		$sizes = array();
    90 		$sizes = array();
    89 		foreach ( get_intermediate_image_sizes() as $s ) {
    91 		foreach ( get_intermediate_image_sizes() as $s ) {
    90 			$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    92 			$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    95 			if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
    97 			if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
    96 				$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
    98 				$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
    97 			else
    99 			else
    98 				$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
   100 				$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
    99 			if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
   101 			if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
   100 				$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
   102 				$sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes
   101 			else
   103 			else
   102 				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
   104 				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
   103 		}
   105 		}
   104 
   106 
       
   107 		/**
       
   108 		 * Filter the image sizes automatically generated when uploading an image.
       
   109 		 *
       
   110 		 * @since 2.9.0
       
   111 		 *
       
   112 		 * @param array $sizes An associative array of image sizes.
       
   113 		 */
   105 		$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
   114 		$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
   106 
   115 
   107 		if ( $sizes ) {
   116 		if ( $sizes ) {
   108 			$editor = wp_get_image_editor( $file );
   117 			$editor = wp_get_image_editor( $file );
   109 
   118 
   111 				$metadata['sizes'] = $editor->multi_resize( $sizes );
   120 				$metadata['sizes'] = $editor->multi_resize( $sizes );
   112 		} else {
   121 		} else {
   113 			$metadata['sizes'] = array();
   122 			$metadata['sizes'] = array();
   114 		}
   123 		}
   115 
   124 
   116 		// fetch additional metadata from exif/iptc
   125 		// Fetch additional metadata from EXIF/IPTC.
   117 		$image_meta = wp_read_image_metadata( $file );
   126 		$image_meta = wp_read_image_metadata( $file );
   118 		if ( $image_meta )
   127 		if ( $image_meta )
   119 			$metadata['image_meta'] = $image_meta;
   128 			$metadata['image_meta'] = $image_meta;
   120 
   129 
   121 	} elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) {
   130 	} elseif ( wp_attachment_is( 'video', $attachment ) ) {
   122 		$metadata = wp_read_video_metadata( $file );
   131 		$metadata = wp_read_video_metadata( $file );
   123 		$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' );
   132 		$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
   124 	} elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) {
   133 	} elseif ( wp_attachment_is( 'audio', $attachment ) ) {
   125 		$metadata = wp_read_audio_metadata( $file );
   134 		$metadata = wp_read_audio_metadata( $file );
   126 		$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' );
   135 		$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
   127 	}
   136 	}
   128 
   137 
   129 	if ( $support && ! empty( $metadata['image']['data'] ) ) {
   138 	if ( $support && ! empty( $metadata['image']['data'] ) ) {
   130 		$ext = '.jpg';
   139 		// Check for existing cover.
   131 		switch ( $metadata['image']['mime'] ) {
   140 		$hash = md5( $metadata['image']['data'] );
   132 		case 'image/gif':
   141 		$posts = get_posts( array(
   133 			$ext = '.gif';
   142 			'fields' => 'ids',
   134 			break;
   143 			'post_type' => 'attachment',
   135 		case 'image/png':
   144 			'post_mime_type' => $metadata['image']['mime'],
   136 			$ext = '.png';
   145 			'post_status' => 'inherit',
   137 			break;
   146 			'posts_per_page' => 1,
   138 		}
   147 			'meta_key' => '_cover_hash',
   139 		$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
   148 			'meta_value' => $hash
   140 		$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
   149 		) );
   141 		if ( false === $uploaded['error'] ) {
   150 		$exists = reset( $posts );
   142 			$attachment = array(
   151 
   143 				'post_mime_type' => $metadata['image']['mime'],
   152 		if ( ! empty( $exists ) ) {
   144 				'post_type' => 'attachment',
   153 			update_post_meta( $attachment_id, '_thumbnail_id', $exists );
   145 				'post_content' => '',
   154 		} else {
   146 			);
   155 			$ext = '.jpg';
   147 			$sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] );
   156 			switch ( $metadata['image']['mime'] ) {
   148 			$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
   157 			case 'image/gif':
   149 			wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
   158 				$ext = '.gif';
   150 			update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
   159 				break;
   151 		}
   160 			case 'image/png':
   152 	}
   161 				$ext = '.png';
   153 	// remove the blob of binary data from the array
   162 				break;
   154 	unset( $metadata['image']['data'] );
   163 			}
   155 
   164 			$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
       
   165 			$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
       
   166 			if ( false === $uploaded['error'] ) {
       
   167 				$image_attachment = array(
       
   168 					'post_mime_type' => $metadata['image']['mime'],
       
   169 					'post_type' => 'attachment',
       
   170 					'post_content' => '',
       
   171 				);
       
   172 				/**
       
   173 				 * Filter the parameters for the attachment thumbnail creation.
       
   174 				 *
       
   175 				 * @since 3.9.0
       
   176 				 *
       
   177 				 * @param array $image_attachment An array of parameters to create the thumbnail.
       
   178 				 * @param array $metadata         Current attachment metadata.
       
   179 				 * @param array $uploaded         An array containing the thumbnail path and url.
       
   180 				 */
       
   181 				$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
       
   182 
       
   183 				$sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] );
       
   184 				add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
       
   185 				$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
       
   186 				wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
       
   187 				update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
       
   188 			}
       
   189 		}
       
   190 	}
       
   191 
       
   192 	// Remove the blob of binary data from the array.
       
   193 	if ( isset( $metadata['image']['data'] ) )
       
   194 		unset( $metadata['image']['data'] );
       
   195 
       
   196 	/**
       
   197 	 * Filter the generated attachment meta data.
       
   198 	 *
       
   199 	 * @since 2.1.0
       
   200 	 *
       
   201 	 * @param array $metadata      An array of attachment meta data.
       
   202 	 * @param int   $attachment_id Current attachment ID.
       
   203 	 */
   156 	return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
   204 	return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
   157 }
   205 }
   158 
   206 
   159 /**
   207 /**
   160  * Convert a fraction string to a decimal.
   208  * Convert a fraction string to a decimal.
   206 	if ( ! file_exists( $file ) )
   254 	if ( ! file_exists( $file ) )
   207 		return false;
   255 		return false;
   208 
   256 
   209 	list( , , $sourceImageType ) = getimagesize( $file );
   257 	list( , , $sourceImageType ) = getimagesize( $file );
   210 
   258 
   211 	// exif contains a bunch of data we'll probably never need formatted in ways
   259 	/*
   212 	// that are difficult to use. We'll normalize it and just extract the fields
   260 	 * EXIF contains a bunch of data we'll probably never need formatted in ways
   213 	// that are likely to be useful. Fractions and numbers are converted to
   261 	 * that are difficult to use. We'll normalize it and just extract the fields
   214 	// floats, dates to unix timestamps, and everything else to strings.
   262 	 * that are likely to be useful. Fractions and numbers are converted to
       
   263 	 * floats, dates to unix timestamps, and everything else to strings.
       
   264 	 */
   215 	$meta = array(
   265 	$meta = array(
   216 		'aperture' => 0,
   266 		'aperture' => 0,
   217 		'credit' => '',
   267 		'credit' => '',
   218 		'camera' => '',
   268 		'camera' => '',
   219 		'caption' => '',
   269 		'caption' => '',
   221 		'copyright' => '',
   271 		'copyright' => '',
   222 		'focal_length' => 0,
   272 		'focal_length' => 0,
   223 		'iso' => 0,
   273 		'iso' => 0,
   224 		'shutter_speed' => 0,
   274 		'shutter_speed' => 0,
   225 		'title' => '',
   275 		'title' => '',
       
   276 		'orientation' => 0,
   226 	);
   277 	);
   227 
   278 
   228 	// read iptc first, since it might contain data not available in exif such
   279 	/*
   229 	// as caption, description etc
   280 	 * Read IPTC first, since it might contain data not available in exif such
       
   281 	 * as caption, description etc.
       
   282 	 */
   230 	if ( is_callable( 'iptcparse' ) ) {
   283 	if ( is_callable( 'iptcparse' ) ) {
   231 		getimagesize( $file, $info );
   284 		getimagesize( $file, $info );
   232 
   285 
   233 		if ( ! empty( $info['APP13'] ) ) {
   286 		if ( ! empty( $info['APP13'] ) ) {
   234 			$iptc = iptcparse( $info['APP13'] );
   287 			$iptc = iptcparse( $info['APP13'] );
   235 
   288 
   236 			// headline, "A brief synopsis of the caption."
   289 			// Headline, "A brief synopsis of the caption."
   237 			if ( ! empty( $iptc['2#105'][0] ) )
   290 			if ( ! empty( $iptc['2#105'][0] ) ) {
   238 				$meta['title'] = trim( $iptc['2#105'][0] );
   291 				$meta['title'] = trim( $iptc['2#105'][0] );
   239 			// title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
   292 			/*
   240 			elseif ( ! empty( $iptc['2#005'][0] ) )
   293 			 * Title, "Many use the Title field to store the filename of the image,
       
   294 			 * though the field may be used in many ways."
       
   295 			 */
       
   296 			} elseif ( ! empty( $iptc['2#005'][0] ) ) {
   241 				$meta['title'] = trim( $iptc['2#005'][0] );
   297 				$meta['title'] = trim( $iptc['2#005'][0] );
       
   298 			}
   242 
   299 
   243 			if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
   300 			if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
   244 				$caption = trim( $iptc['2#120'][0] );
   301 				$caption = trim( $iptc['2#120'][0] );
   245 				if ( empty( $meta['title'] ) ) {
   302 
       
   303 				mbstring_binary_safe_encoding();
       
   304 				$caption_length = strlen( $caption );
       
   305 				reset_mbstring_encoding();
       
   306 
       
   307 				if ( empty( $meta['title'] ) && $caption_length < 80 ) {
   246 					// Assume the title is stored in 2:120 if it's short.
   308 					// Assume the title is stored in 2:120 if it's short.
   247 					if ( strlen( $caption ) < 80 )
   309 					$meta['title'] = $caption;
   248 						$meta['title'] = $caption;
       
   249 					else
       
   250 						$meta['caption'] = $caption;
       
   251 				} elseif ( $caption != $meta['title'] ) {
       
   252 					$meta['caption'] = $caption;
       
   253 				}
   310 				}
       
   311 
       
   312 				$meta['caption'] = $caption;
   254 			}
   313 			}
   255 
   314 
   256 			if ( ! empty( $iptc['2#110'][0] ) ) // credit
   315 			if ( ! empty( $iptc['2#110'][0] ) ) // credit
   257 				$meta['credit'] = trim( $iptc['2#110'][0] );
   316 				$meta['credit'] = trim( $iptc['2#110'][0] );
   258 			elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
   317 			elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
   259 				$meta['credit'] = trim( $iptc['2#080'][0] );
   318 				$meta['credit'] = trim( $iptc['2#080'][0] );
   260 
   319 
   261 			if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time
   320 			if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time
   262 				$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
   321 				$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
   263 
   322 
   264 			if ( ! empty( $iptc['2#116'][0] ) ) // copyright
   323 			if ( ! empty( $iptc['2#116'][0] ) ) // copyright
   265 				$meta['copyright'] = trim( $iptc['2#116'][0] );
   324 				$meta['copyright'] = trim( $iptc['2#116'][0] );
   266 		 }
   325 		 }
   267 	}
   326 	}
   268 
   327 
   269 	// fetch additional info from exif if available
   328 	/**
       
   329 	 * Filter the image types to check for exif data.
       
   330 	 *
       
   331 	 * @since 2.5.0
       
   332 	 *
       
   333 	 * @param array $image_types Image types to check for exif data.
       
   334 	 */
   270 	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 ) ) ) ) {
   335 	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 ) ) ) ) {
   271 		$exif = @exif_read_data( $file );
   336 		$exif = @exif_read_data( $file );
   272 
   337 
   273 		if ( !empty( $exif['Title'] ) )
       
   274 			$meta['title'] = trim( $exif['Title'] );
       
   275 
       
   276 		if ( ! empty( $exif['ImageDescription'] ) ) {
   338 		if ( ! empty( $exif['ImageDescription'] ) ) {
   277 			if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
   339 			mbstring_binary_safe_encoding();
       
   340 			$description_length = strlen( $exif['ImageDescription'] );
       
   341 			reset_mbstring_encoding();
       
   342 
       
   343 			if ( empty( $meta['title'] ) && $description_length < 80 ) {
   278 				// Assume the title is stored in ImageDescription
   344 				// Assume the title is stored in ImageDescription
   279 				$meta['title'] = trim( $exif['ImageDescription'] );
   345 				$meta['title'] = trim( $exif['ImageDescription'] );
   280 				if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
   346 			}
   281 					$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
   347 
   282 			} elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
   348 			if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) {
       
   349 				$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
       
   350 			}
       
   351 
       
   352 			if ( empty( $meta['caption'] ) ) {
   283 				$meta['caption'] = trim( $exif['ImageDescription'] );
   353 				$meta['caption'] = trim( $exif['ImageDescription'] );
   284 			}
   354 			}
   285 		} elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
   355 		} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
   286 			$meta['caption'] = trim( $exif['Comments'] );
   356 			$meta['caption'] = trim( $exif['Comments'] );
   287 		}
   357 		}
   288 
   358 
   289 		if ( ! empty( $exif['Artist'] ) )
   359 		if ( empty( $meta['credit'] ) ) {
   290 			$meta['credit'] = trim( $exif['Artist'] );
   360 			if ( ! empty( $exif['Artist'] ) ) {
   291 		elseif ( ! empty($exif['Author'] ) )
   361 				$meta['credit'] = trim( $exif['Artist'] );
   292 			$meta['credit'] = trim( $exif['Author'] );
   362 			} elseif ( ! empty($exif['Author'] ) ) {
   293 
   363 				$meta['credit'] = trim( $exif['Author'] );
   294 		if ( ! empty( $exif['Copyright'] ) )
   364 			}
       
   365 		}
       
   366 
       
   367 		if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) {
   295 			$meta['copyright'] = trim( $exif['Copyright'] );
   368 			$meta['copyright'] = trim( $exif['Copyright'] );
   296 		if ( ! empty($exif['FNumber'] ) )
   369 		}
       
   370 		if ( ! empty( $exif['FNumber'] ) ) {
   297 			$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
   371 			$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
   298 		if ( ! empty($exif['Model'] ) )
   372 		}
       
   373 		if ( ! empty( $exif['Model'] ) ) {
   299 			$meta['camera'] = trim( $exif['Model'] );
   374 			$meta['camera'] = trim( $exif['Model'] );
   300 		if ( ! empty($exif['DateTimeDigitized'] ) )
   375 		}
   301 			$meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] );
   376 		if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) {
   302 		if ( ! empty($exif['FocalLength'] ) )
   377 			$meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] );
       
   378 		}
       
   379 		if ( ! empty( $exif['FocalLength'] ) ) {
   303 			$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
   380 			$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
   304 		if ( ! empty($exif['ISOSpeedRatings'] ) ) {
   381 		}
       
   382 		if ( ! empty( $exif['ISOSpeedRatings'] ) ) {
   305 			$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
   383 			$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
   306 			$meta['iso'] = trim( $meta['iso'] );
   384 			$meta['iso'] = trim( $meta['iso'] );
   307 		}
   385 		}
   308 		if ( ! empty($exif['ExposureTime'] ) )
   386 		if ( ! empty( $exif['ExposureTime'] ) ) {
   309 			$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
   387 			$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
       
   388 		}
       
   389 		if ( ! empty( $exif['Orientation'] ) ) {
       
   390 			$meta['orientation'] = $exif['Orientation'];
       
   391 		}
   310 	}
   392 	}
   311 
   393 
   312 	foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
   394 	foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
   313 		if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) )
   395 		if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) {
   314 			$meta[ $key ] = utf8_encode( $meta[ $key ] );
   396 			$meta[ $key ] = utf8_encode( $meta[ $key ] );
   315 	}
   397 		}
   316 
   398 	}
       
   399 
       
   400 	foreach ( $meta as &$value ) {
       
   401 		if ( is_string( $value ) ) {
       
   402 			$value = wp_kses_post( $value );
       
   403 		}
       
   404 	}
       
   405 
       
   406 	/**
       
   407 	 * Filter the array of meta data read from an image's exif data.
       
   408 	 *
       
   409 	 * @since 2.5.0
       
   410 	 *
       
   411 	 * @param array  $meta            Image meta data.
       
   412 	 * @param string $file            Path to image file.
       
   413 	 * @param int    $sourceImageType Type of image.
       
   414 	 */
   317 	return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
   415 	return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
   318 
   416 
   319 }
   417 }
   320 
   418 
   321 /**
   419 /**
   333 
   431 
   334 /**
   432 /**
   335  * Validate that file is suitable for displaying within a web page.
   433  * Validate that file is suitable for displaying within a web page.
   336  *
   434  *
   337  * @since 2.5.0
   435  * @since 2.5.0
   338  * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
       
   339  *
   436  *
   340  * @param string $path File path to test.
   437  * @param string $path File path to test.
   341  * @return bool True if suitable, false if not suitable.
   438  * @return bool True if suitable, false if not suitable.
   342  */
   439  */
   343 function file_is_displayable_image($path) {
   440 function file_is_displayable_image($path) {
   344 	$info = @getimagesize($path);
   441 	$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP );
   345 	if ( empty($info) )
   442 
       
   443 	$info = @getimagesize( $path );
       
   444 	if ( empty( $info ) ) {
   346 		$result = false;
   445 		$result = false;
   347 	elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )	// only gif, jpeg and png images can reliably be displayed
   446 	} elseif ( ! in_array( $info[2], $displayable_image_types ) ) {
   348 		$result = false;
   447 		$result = false;
   349 	else
   448 	} else {
   350 		$result = true;
   449 		$result = true;
   351 
   450 	}
   352 	return apply_filters('file_is_displayable_image', $result, $path);
   451 
       
   452 	/**
       
   453 	 * Filter whether the current image is displayable in the browser.
       
   454 	 *
       
   455 	 * @since 2.5.0
       
   456 	 *
       
   457 	 * @param bool   $result Whether the image can be displayed. Default true.
       
   458 	 * @param string $path   Path to the image.
       
   459 	 */
       
   460 	return apply_filters( 'file_is_displayable_image', $result, $path );
   353 }
   461 }
   354 
   462 
   355 /**
   463 /**
   356  * Load an image resource for editing.
   464  * Load an image resource for editing.
   357  *
   465  *
   380 		default:
   488 		default:
   381 			$image = false;
   489 			$image = false;
   382 			break;
   490 			break;
   383 	}
   491 	}
   384 	if ( is_resource($image) ) {
   492 	if ( is_resource($image) ) {
   385 		$image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
   493 		/**
       
   494 		 * Filter the current image being loaded for editing.
       
   495 		 *
       
   496 		 * @since 2.9.0
       
   497 		 *
       
   498 		 * @param resource $image         Current image.
       
   499 		 * @param string   $attachment_id Attachment ID.
       
   500 		 * @param string   $size          Image size.
       
   501 		 */
       
   502 		$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
   386 		if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
   503 		if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
   387 			imagealphablending($image, false);
   504 			imagealphablending($image, false);
   388 			imagesavealpha($image, true);
   505 			imagesavealpha($image, true);
   389 		}
   506 		}
   390 	}
   507 	}
   407 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
   524 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
   408 	$filepath = get_attached_file( $attachment_id );
   525 	$filepath = get_attached_file( $attachment_id );
   409 
   526 
   410 	if ( $filepath && file_exists( $filepath ) ) {
   527 	if ( $filepath && file_exists( $filepath ) ) {
   411 		if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
   528 		if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
       
   529 			/**
       
   530 			 * Filter the path to the current image.
       
   531 			 *
       
   532 			 * The filter is evaluated for all image sizes except 'full'.
       
   533 			 *
       
   534 			 * @since 3.1.0
       
   535 			 *
       
   536 			 * @param string $path          Path to the current image.
       
   537 			 * @param string $attachment_id Attachment ID.
       
   538 			 * @param string $size          Size of the image.
       
   539 			 */
   412 			$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
   540 			$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
   413 		}
   541 		}
   414 	} elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
   542 	} elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
       
   543 		/**
       
   544 		 * Filter the image URL if not in the local filesystem.
       
   545 		 *
       
   546 		 * The filter is only evaluated if fopen is enabled on the server.
       
   547 		 *
       
   548 		 * @since 3.1.0
       
   549 		 *
       
   550 		 * @param string $image_url     Current image URL.
       
   551 		 * @param string $attachment_id Attachment ID.
       
   552 		 * @param string $size          Size of the image.
       
   553 		 */
   415 		$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
   554 		$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
   416 	}
   555 	}
   417 
   556 
       
   557 	/**
       
   558 	 * Filter the returned path or URL of the current image.
       
   559 	 *
       
   560 	 * @since 2.9.0
       
   561 	 *
       
   562 	 * @param string|bool $filepath      File path or URL to current image, or false.
       
   563 	 * @param string      $attachment_id Attachment ID.
       
   564 	 * @param string      $size          Size of the image.
       
   565 	 */
   418 	return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
   566 	return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
   419 }
   567 }
   420 
   568 
   421 /**
   569 /**
   422  * Copy an existing image file.
   570  * Copy an existing image file.
   434 
   582 
   435 	if ( $src_file ) {
   583 	if ( $src_file ) {
   436 		$dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
   584 		$dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
   437 		$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
   585 		$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
   438 
   586 
   439 		// The directory containing the original file may no longer exist when
   587 		/*
   440 		// using a replication plugin.
   588 		 * The directory containing the original file may no longer
       
   589 		 * exist when using a replication plugin.
       
   590 		 */
   441 		wp_mkdir_p( dirname( $dst_file ) );
   591 		wp_mkdir_p( dirname( $dst_file ) );
   442 
   592 
   443 		if ( ! @copy( $src_file, $dst_file ) )
   593 		if ( ! @copy( $src_file, $dst_file ) )
   444 			$dst_file = false;
   594 			$dst_file = false;
   445 	} else {
   595 	} else {