Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#62385 new enhancement

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

Reported by: azaozz's profile azaozz Owned by:
Milestone: 6.8 Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch
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 (2)

#1 @azaozz
4 weeks 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 weeks 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).
Note: See TracTickets for help on using tickets.