Skip to content

Strip image metadata on upload

By default, image metadata is not stripped from uploaded images. The GET ?strip=all|info|color query parameter string can be added to images served by VIP’s File System in order to remove JPEG image Exif, IPTC, comment, and color data from the output image in the browser.

A more effective method to strip image metadata is to programmatically set image metadata to be stripped on upload. This custom code can be added to a theme’s functions.php, or as a separate plugin.

In the code example below, a custom function is preserving an image file’s ICC color profile data (icc) but stripping all other metadata from images on upload. This code example can be modified to preserve other types of profile data, or to strip all metadata entirely upon file upload.

add_filter( 'wp_handle_upload', 'yourprefix_strip_metadata_from_images_on_upload' );
 * Overwrite the uploaded image with a new version that has no metadata.
 * @param array $upload {
 *     Upload arguments.
 *     @type string $file The path to the image file.
 *     @type string $url  The URL to the image.
 *     @type string $type The MIME type of the image.
 * }
 * @return array Unmodified $upload array.
function yourprefix_strip_metadata_from_images_on_upload( array $upload ):array {
	// Return if the file is not an image.
	if ( ! in_array( $upload['type'], array( 'image/jpeg', 'image/png', 'image/gif' ), true ) ) {
		return $upload;

	try {
		yourprefix_strip_metadata_from_image( $upload['file'] );
	} catch ( \GmagickException $e ) {
		// Do nothing.

	return $upload;

 * Strip metadata from an image file, but keep the ICC color profile data.
 * @param string      $file   Path to the image file.
 * @param string|null $output Path to write the image to. Leave null to overwrite the original.
function yourprefix_strip_metadata_from_image( string $file, ?string $output = null ) {
	$image = new \Gmagick( $file );

	// Check if we have an ICC profile, so we can restore it later.
	try {
		$profile = $image->getimageprofile( 'icc' );
	} catch ( \GmagickException $exception ) {
		// Raises an exception if no profile is found.

	// Strip all image metadata.

	// Restore the ICC profile if we have one.
	if ( ! empty( $profile ) ) {
		$image->setimageprofile( 'icc', $profile );

	// Replace the uploaded image with the stripped down version.
	if ( empty( $output ) ) {
		$output = $file;

	$image->writeimage( $output, true );

Last updated: December 26, 2023

Relevant to

  • WordPress