Make WordPress Core


Ignore:
Timestamp:
09/06/2022 09:13:17 PM (2 years ago)
Author:
adamsilverstein
Message:

Media: Output WebP by default when uploading JPEGs.

Uploaded JPEGs will automatically be converted to WebP sub-sizes instead of JPEG, saving space and making sites faster.

The original JPEG upload is always retained and can be accessed by calling wp_get_original_image_url.

Props azaozz, flixos90.
Fixes #55443.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor.php

    r54085 r54086  
    425425    }
    426426
    427     /**
    428      * Builds an output filename based on current file, and adding proper suffix
    429      *
    430      * @since 3.5.0
    431      *
    432      * @param string $suffix
    433      * @param string $dest_path
    434      * @param string $extension
    435      * @return string filename
     427        /**
     428     * Builds an output filename based on current file, and adding proper suffix.
     429     *
     430     * @since 3.5.0
     431     * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
     432     *              file extension being generated doesn't match the image file extension,
     433     *              add the extension to the suffix
     434     *
     435     * @param string $suffix    Optional. Suffix to add to the filename. The default null
     436     *                          will result in a 'widthxheight' suffix. Passing
     437     *                          an empty string will result in no suffix.
     438     * @param string $dest_path Optional. The path to save the file to. The default null
     439     *                          will use the image file path.
     440     * @param string $extension Optional. The file extension to use. The default null
     441     *                          will use the image file extension.
     442     * @return string filename The generated file name.
    436443     */
    437444    public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    438445        // $suffix will be appended to the destination filename, just before the extension.
    439         if ( ! $suffix ) {
     446        if ( null === $suffix ) {
    440447            $suffix = $this->get_suffix();
    441448        }
     
    458465        }
    459466
    460         return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
     467        if ( empty( $suffix ) ) {
     468            $suffix = '';
     469        } else {
     470            $suffix = "-{$suffix}";
     471        }
     472
     473        // When the file extension being generated doesn't match the image file extension,
     474        // add the extension to the suffix to ensure a unique file name. Prevents
     475        // name conflicts when a single image type can have multiple extensions,
     476        // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
     477        if ( ! empty( $extension ) && $extension !== $ext ) {
     478            $suffix .= "-{$ext}";
     479        }
     480
     481        return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    461482    }
    462483
Note: See TracChangeset for help on using the changeset viewer.