# imagify/1.3.5.1/inc/common/attachments.php

Imagify Image Optimization: Optimize Images | Compress &amp; Convert to WebP/AVIF, version 1.3.5.1. 33 lines.

- Page: https://pluginprobe.com/plugins/imagify/1.3.5.1/code/inc/common/attachments.php
- Raw: https://pluginprobe.com/plugins/imagify/1.3.5.1/raw/inc/common/attachments.php
- Modified: 2016-01-27T15:07:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/imagify/1.3.5.1/code/inc/common/attachments.php#L10-L20`.

```php
<?php
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );

/**
 * Auto-optimize when a new attachment is generated
 *
 * @since 1.0
 */
add_filter( 'wp_generate_attachment_metadata', '_imagify_optimize_attachment', PHP_INT_MAX, 2 );
function _imagify_optimize_attachment( $metadata, $attachment_id ) {
	$api_key = get_imagify_option( 'api_key', false );
	
	if ( ! empty( $api_key ) && get_imagify_option( 'auto_optimize', false ) ) {
		$attachment = new Imagify_Attachment( $attachment_id );
		
		// Optimize it!!!!!
		$attachment->optimize( null, $metadata );
	}

	return $metadata;
}


/**
 * Delete the backup file when an attachement is deleted.
 *
 * @since 1.0
 */
add_action( 'delete_attachment', '_imagify_delete_backup_file' );
function _imagify_delete_backup_file( $post_id ) {
	$attachment  = new Imagify_Attachment( $post_id );
	$attachment->delete_backup();
}
```
