Skip to content

Avoid duplicate execution of setting up image editor when determining dominant color and transparency #373

Closed
@felixarntz

Description

@felixarntz

Follow-up to #19 / #282: Currently, both the dominant_color_get_dominant_color() and the dominant_color_has_transparency() functions execute the same logic to get the attachment file and instantiate the correct editor based on it.

This is unnecessary and somewhat wasteful since the only place where the two functions are relevant is in immediately subsequent calls for when the image has just been uploaded.

In addition to that, the two functions suggest by their public nature and clear names that they are okay to be called by third-party developers - which is not the case. These two functions perform expensive actions, and they must not be called "lightly". Their current name and documentation may lead to plugin developers calling them in the frontend to determine the dominant color and transparency of an image, but that is definitely not the intended use-case.

If we want to provide an easy-to-use API to get the dominant color and transparency of an image, these functions should rely on the already computed and stored values in the attachment's metadata. The way that the current functions behave is a no go for a "public API" though, and per the initial point of this issue is also slightly wasteful for performance during the upload process of an image.

Here's what I propose:

  • Combine the two current functions dominant_color_get_dominant_color() and dominant_color_has_transparency() into a single function marked as @access private that computes both values and returns them, or a WP_Error.
  • Introduce two new functions dominant_color_get_dominant_color() and dominant_color_has_transparency() instead, which can truly serve as an "API" that is okay to be called from third-party developers. Both of these functions should simply look at the attachment's metadata and returned the stored value if set, or something like null otherwise.

cc @spacedmonkey @pbearne

Metadata

Metadata

Assignees

Labels

[Plugin] Image PlaceholdersIssues for the Image Placeholders plugin (formerly Dominant Color Images)[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions