Support » Plugin: Clean Filenames » Remove dimensions (ex720x300.jpg)

  • alexgenaud

    (@alexgenaud)


    Image dimensions in the uploaded filename clashes with many other plugins such as thumbnail creation. The following code snippet added to sanitize-spanish-filenames.php replaces any dimension (examples: 123×123. , 1x1_, 2×2- with . _ – respectively).

    // First step of cleanup using WP core function
    $friendly_filename = remove_accents( $filename );
    
    // begin Alex remove dimensions (ex. 1200x630.png)
    $dimension_pattern = '/([0-9][0-9]*[xX][0-9][0-9]*)([\._-])/';
    $dimension_replace = '${2}';
    $friendly_filename = preg_replace( $dimension_pattern,
                         $dimension_replace, $friendly_filename );
    // end Alex remove dimensions
    
    // Replacing additional characters
    $friendly_filename = preg_replace( $original_chars,
                         $sanitized_chars, $friendly_filename );
    

    See comment https://wordpress.org/support/topic/remove-numbers-on-end-of-file-name/ by @bkjproductions

    • This topic was modified 5 months ago by alexgenaud.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Samuel Aguilera

    (@samuelaguilera)

    Hi @alexgenaud

    I added a filter in 1.2.2 to allow additional modifications of the filename, allowing you to apply your custom changes without having to edit the plugin file for each new version.

    add_filter ('scf_friendly_filename', 'removing_dimensions', 10, 1);
    function removing_dimensions( $friendly_filename ){
    
    	$dimension_pattern = '/([0-9][0-9]*[xX][0-9][0-9]*)([\._-])/';
    	$dimension_replace = '${2}';
    
    	$friendly_filename = preg_replace( $dimension_pattern,
    	$dimension_replace, $friendly_filename );
    
    	return $friendly_filename;
    }

    Excellent. Thank you. Where should I put the custom (removing_dimensions) filter? In functions.php?

    Plugin Author Samuel Aguilera

    (@samuelaguilera)

    Yeah, if you paste the above snippet on your theme’s functions.php file that should be enough.

    You could also use the Code Snippets plugin.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.