Make WordPress Core

Opened 4 months ago

Closed 4 weeks ago

Last modified 2 weeks ago

#62385 closed enhancement (fixed)

Improve `WP_Image_Editor::generate_filename()` to support generating filenames without a suffix

Reported by: azaozz's profile azaozz Owned by: joedolson's profile joedolson
Milestone: 6.8 Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch needs-dev-note commit
Focuses: Cc:

Description

Currently WP_Image_Editor::generate_filename( $suffix = null, $dest_path = null, $extension = null ) forces a dimensions suffix like -600x800 when a $suffix is not present or has a "falsey" value.

This makes it impossible to use the function to generate an image file name without a suffix. However that functionality is required in some cases like converting an image to a different format where a suffix is not desirable, see https://github.com/WordPress/wordpress-develop/pull/7748.

Change History (11)

#1 @azaozz
4 months ago

Thinking that the best solution here would be to accept an empty string for "no suffix". I.e. a patch would be something like:

if ( $suffix ) {
    $suffix = '-' . $suffix;
} elseif ( '' !== $suffix ) {
    $suffix = '-' . $this->get_suffix();
}

instead of:

if ( ! $suffix ) {
    $suffix = $this->get_suffix();
}

(And the dash will be removed from the returned value: "{$name}{$suffix}.{$new_ext}".)

Implementing this will change the behavior of generate_filename() a little bit, but thinking the change will be quite insignificant and as far as I see won't affect existing plugins. Then the current code that was added in 6.7 in [59379] can be replaced with $saved = $editor->save( $editor->generate_filename( '' ) );.

This ticket was mentioned in PR #7772 on WordPress/wordpress-develop by @debarghyabanerjee.


4 months ago
#2

  • Keywords has-patch added

Trac Ticket: Core-62385

## Overview

The current implementation of the WP_Image_Editor::generate_filename() method automatically appends a dimension suffix (e.g., -600x800) to the file name when no $suffix is provided, or when it is "falsey" (e.g., null or false). This behavior can be restrictive in scenarios where no suffix is desired, such as when converting an image to a different format.

## Problem

In use cases like converting images from one format to another (e.g., from PNG to JPG), it is often undesirable to append a suffix. The current implementation forces the addition of the suffix even when it's not needed.

## Solution

The proposed solution allows passing an empty string ("") as the $suffix to explicitly indicate that no suffix should be applied. This adjustment makes it possible to generate a file name without the dimension suffix in cases where it's not desirable.

## Change Details

  • The change will modify the existing condition that checks for a "falsey" $suffix value. Specifically, it will allow an empty string to be passed as a valid $suffix, meaning no suffix will be appended to the file name. If the $suffix is left null, the function will fallback to the default behavior and append the default suffix (e.g., based on dimensions).

## Impact

  • Behavior Change: This will change the behavior of generate_filename(), but it will be a minor change. The function will now allow an empty string as a valid value for $suffix, which will result in no suffix being added to the filename.
  • Backward Compatibility: This change is backward-compatible and will not break any existing functionality. Existing use cases where a dimension suffix is desired will still work as expected.

## Use Cases

  • This change is especially useful in image format conversion scenarios, where users may want to convert an image without appending a suffix (e.g., image.jpg to image.png without a suffix).

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


4 weeks ago

#4 follow-up: @audrasjb
4 weeks ago

  • Keywords needs-testing-info added

As per today's bug scrub: We have a PR for this ticket, @azaozz do you have some time to perform a quick code review?

Also adding needs-testing-info so the patch can be tested by the Test Team.

#5 @audrasjb
4 weeks ago

  • Owner set to joedolson
  • Status changed from new to assigned

As per today's bug scrub, assigning to @joedolson for final review

#6 in reply to: ↑ 4 @azaozz
4 weeks ago

  • Keywords needs-dev-note added

Replying to audrasjb:

@azaozz do you have some time to perform a quick code review?

Sure. The PR looks good imho. May need a little update of the docs, or an inline comment to explain the new behavior. Also probably good to mention this in a dev. note as plugins authors may find it useful.

After this is committed this bit of code: https://core.trac.wordpress.org/changeset/59379/trunk/src/wp-admin/includes/image.php should be replaced with:

$saved = $editor->save( $editor->generate_filename( '' ) );

That uses the new functionality from this ticket.

#7 @joedolson
4 weeks ago

  • Keywords commit added; needs-testing-info removed

Tested and confirmed functionality using the test method and image from #62359; file was converted with no suffix, as expected.

Marking for commit; will follow up with a new ticket to make the additional change in image.php.

#8 @joedolson
4 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 59855:

Media: Support generating filenames without a suffix.

Add support handling an empty string in the $suffix parameter that allows a file name to be generated with no suffix added. This makes it possible to avoid adding irrelevant suffixes in cases like converting image formats.

Props azaozz, debarghyabanerjee, joedolson.
See #62359.
Fixes #62385.

#9 @joedolson
4 weeks ago

Follow up in #63003

#10 @joedolson
2 weeks ago

In 59897:

Media: Simplify file name generation for image conversions.

Use the empty string argument in $editor->save() added in [59855] to simplify the code used to prevent image format conversions from getting a suffix appended to their file names.

Props joedolson, audrasjb, azaozz.
See #62385.
Fixes #63003.

@joedolson commented on PR #7772:


2 weeks ago
#11

In 59897

Note: See TracTickets for help on using tickets.