Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress images upload via JSON API #41

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

tijmenbruggeman
Copy link
Contributor

@tijmenbruggeman tijmenbruggeman commented Feb 6, 2025

Description
This merge request will add an action to rest_api_init to create a compressor. This will ensure that images uploaded via the media endpoints (https://developer.wordpress.org/rest-api/reference/media/) are properly compressed if possible.

Background
Whenever an image is uploaded it will generate various sizes and call the action wp_generate_attachment_metadata (link). This action was called on uploading an image through the REST API and the compress function on Tiny_Image was called, though because no compressor existed in the settings the images were not compressed:

// class-tiny-image.php:197
public function compress() {
	if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
		return;
	}
        ...
}

Usually, when admin or ajax is initiated it will call the admin_init or ajax_init functions. These have methods hooked up which will create a compressor. When the request goes to the REST API, nothing was done to create the compressor. The solution was to add an action to the rest_api_init hook, which is called when receiving a new REST request, and make sure the compressor is created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant