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

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

- Page: https://pluginprobe.com/plugins/imagify/1.4.2/code/inc/common/attachments.php
- Raw: https://pluginprobe.com/plugins/imagify/1.4.2/raw/inc/common/attachments.php
- Modified: 2016-02-27T17:21:00+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.4.2/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();
}

/**
 * Optimize a resized attachment
 *
 * @since 1.3.6
 * @since 1.4 Async job
 */
add_action( 'shutdown', '_imagify_optimize_save_image_editor_file' );
function _imagify_optimize_save_image_editor_file() {
	if ( isset( $_POST['action'], $_POST['do'], $_POST['postid'] )
		&& 'image-editor' === $_POST['action']
		&& check_ajax_referer( 'image_editor-' . $_POST['postid'] )
		&& get_post_meta( $_POST['postid'], '_imagify_data', true )
		&& 'open' != $_POST['do']
	) {

		$body                 = $_POST;
		$body['action']       = 'imagify_async_optimize_save_image_editor_file';
		$body['transient_id'] = $body['postid'];

		imagify_do_async_job( $body );
	}
}
```
