WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 4 months ago

#33278 new enhancement

Naming files with dimensions when manually cropping images

Reported by: acbaile Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1.6
Component: Media Keywords: has-patch needs-refresh
Focuses: Cc:
PR Number:

Description

Hi. When I create a new dimension for image file in image editor, I get file with random name. It is more useful to name it like basic files (filename-150x150.ext, filename-300x300.ext). I change for it "/wp-admin/includes/image-edit.php". This code first try to use dimension-name before using random. Starting from line 707. My code in a pluses.

	// Generate new filename.
	$path = get_attached_file($post_id);
	$path_parts = pathinfo( $path );
	$filename = $path_parts['filename'];
	$suffix = time() . rand(100, 999);

	//++++++++++++++++++++++++++++++++++++++++++++++++
	// First try suffix-sizes - mine
	$suffix_sizes = $img->get_size();
	$suffix_sizes = "-".$suffix_sizes["width"]."x".$suffix_sizes["height"];
	//++++++++++++++++++++++++++++++++++++++++++++++++

	if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
		isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {

		if ( 'thumbnail' == $target )
			$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
		else
			$new_path = $path;
	} else {
		//++++++++++++++++++++++++++++++++++++++++++++++++
		// First try suffix-sizes - mine
		$filename = preg_replace( "/-e([0-9]+)$/", "", $filename );
		$filename = preg_replace( "/-([0-9]{1,5})x([0-9]{1,5})$/", "", $filename );
		$filename .= $suffix_sizes;
		$new_filename = $filename.".".$path_parts["extension"];
		$new_path = $path_parts["dirname"]."/".$new_filename;
		if ( file_exists($new_path) ){
		//++++++++++++++++++++++++++++++++++++++++++++++++
			while( true ) {
				$filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
				$filename .= "-e{$suffix}";
				$new_filename = "{$filename}.{$path_parts['extension']}";
				$new_path = "{$path_parts['dirname']}/$new_filename";
				if ( file_exists($new_path) )
					$suffix++;
				else
					break;
			}
		//++++++++++++++++++++++++++++++++++++++++++++++++
		}
		//++++++++++++++++++++++++++++++++++++++++++++++++
	}


Attachments (2)

image-edit.php (29.6 KB) - added by acbaile 4 years ago.
/wp-admin/includes/image-edit.php
image-edit.php.patch (1.6 KB) - added by acbaile 4 years ago.
Patch that improve this functionality

Download all attachments as: .zip

Change History (7)

@acbaile
4 years ago

/wp-admin/includes/image-edit.php

#1 @joedolson
4 years ago

  • Focuses accessibility removed

#2 @acbaile
4 years ago

  • Keywords has-patch added
  • Type changed from feature request to enhancement

#3 follow-up: @swissspidy
4 years ago

  • Keywords needs-refresh added
  • Summary changed from Naming files with dimensions in the name (/wp-admin/includes/image-edit.php) to Naming files with dimensions when manually cropping images

Hey there, thanks for your report!

Please have a look at the handbook on how to create proper patch files.

Can you elaborate on why this behaviour should be changed?

As far as I can tell, a random hash is generated on purpose because it shouldn't conflict with already existing images.

#4 in reply to: ↑ 3 @acbaile
4 years ago

Replying to swissspidy:

Can you elaborate on why this behaviour should be changed?

Main reason. I use fancybox, and when I want to place in a post static image with smaller size, I need to go to the image editor, I create smaller size, and then I need put name of this file in a post. But I don't know it. I need to open ftp-client to see and copy this name. It very long and difficult way. When naming is by dimension I can simply write it by keyboard.

@acbaile
4 years ago

Patch that improve this functionality

#5 @acbaile
4 years ago

Hi! I made the patch! Please, put it in the code.

Last edited 4 years ago by acbaile (previous) (diff)
Note: See TracTickets for help on using tickets.