WP_Image_Editor::generate_filename() WordPress Method

The generate_filename() method is used to generate a new filename for an image. This is useful when you need to create a new image file, but don't want to use the same filename as the original image.

WP_Image_Editor::generate_filename( string $suffix = null, string $dest_path = null, string $extension = null ) #

Builds an output filename based on current file, and adding proper suffix


Parameters

$suffix

(string)(Optional)

Default value: null

$dest_path

(string)(Optional)

Default value: null

$extension

(string)(Optional)

Default value: null


Top ↑

Return

(string) filename


Top ↑

Source

File: wp-includes/class-wp-image-editor.php

	public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
		// $suffix will be appended to the destination filename, just before the extension.
		if ( ! $suffix ) {
			$suffix = $this->get_suffix();
		}

		$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
		$ext = pathinfo( $this->file, PATHINFO_EXTENSION );

		$name    = wp_basename( $this->file, ".$ext" );
		$new_ext = strtolower( $extension ? $extension : $ext );

		if ( ! is_null( $dest_path ) ) {
			if ( ! wp_is_stream( $dest_path ) ) {
				$_dest_path = realpath( $dest_path );
				if ( $_dest_path ) {
					$dir = $_dest_path;
				}
			} else {
				$dir = $dest_path;
			}
		}

		return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
	}


Top ↑

Changelog

Changelog
VersionDescription
3.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.