最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Rename image uploads replacing "width" and "height" in filename with "medium&

programmeradmin1浏览0评论

Good morning,

I am trying to change name format that Wordpress uses to rename uploaded files. As I upload an image whose filename is "desert-landscape.jpg", Wordpress will set up scaled versions and rename filenames to different combinations of "desert-landscape-{WIDTHxHEIGHT}.jpg" (dipending on thumbnail sizes).

Is there a way to remove width & height attribute from filename and replace them by "{medium}-desert-landscape.jpg", "{large}-desert-landscape.jpg" and "{small}-desert-landscape.jpg"?

Have been looking at this post Rename image uploads with width in filename me not able to fix it right way.

Thank you

// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'rename_intermediates_wpse_82193' );

function rename_intermediates_wpse_82193( $image ) 
{
    // Split the $image path into directory/extension/name
    $info = pathinfo($image);
    $dir = $info['dirname'] . '/';
    $ext = '.' . $info['extension'];
    $name = wp_basename( $image, "$ext" );

    // Build our new image name
    $name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
    $size_extension = substr( $name, strrpos( $name, '-' ) + 1 );
    $new_name = $dir . $size_extension . '-' . $name_prefix . $ext;

    // Rename the intermediate size
    $did_it = rename( $image, $new_name );

    // Renaming successful, return new name
    if( $did_it )
        return $new_name;

    return $image;
}

Good morning,

I am trying to change name format that Wordpress uses to rename uploaded files. As I upload an image whose filename is "desert-landscape.jpg", Wordpress will set up scaled versions and rename filenames to different combinations of "desert-landscape-{WIDTHxHEIGHT}.jpg" (dipending on thumbnail sizes).

Is there a way to remove width & height attribute from filename and replace them by "{medium}-desert-landscape.jpg", "{large}-desert-landscape.jpg" and "{small}-desert-landscape.jpg"?

Have been looking at this post Rename image uploads with width in filename me not able to fix it right way.

Thank you

// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'rename_intermediates_wpse_82193' );

function rename_intermediates_wpse_82193( $image ) 
{
    // Split the $image path into directory/extension/name
    $info = pathinfo($image);
    $dir = $info['dirname'] . '/';
    $ext = '.' . $info['extension'];
    $name = wp_basename( $image, "$ext" );

    // Build our new image name
    $name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
    $size_extension = substr( $name, strrpos( $name, '-' ) + 1 );
    $new_name = $dir . $size_extension . '-' . $name_prefix . $ext;

    // Rename the intermediate size
    $did_it = rename( $image, $new_name );

    // Renaming successful, return new name
    if( $did_it )
        return $new_name;

    return $image;
}
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Nov 14, 2013 at 10:12 morelomorelo 431 silver badge6 bronze badges 1
  • looks like that script is not working anymore in wp 4.9 - any idea how to fix it? was extremely useful. if you set up the script in your funtions.php, after the upload the thumbnail in media library is broken, no file is being saved in uploads...not even the old ones ZZZxYYYpx.jpg – ff-webdesigner Commented Feb 15, 2018 at 14:47
Add a comment  | 

1 Answer 1

Reset to default 4

To be honest I'm a bit puzzled that all what you have tried so far is copying & pasting an answer that doesn't solve your problem. On the other hand, you described your problem clearly. Below I outline a solution for you, take a look at the additional comments and information sources, if you really want to understand what's happening.

Code:

// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'wpse_123240_rename_intermediates' );
function wpse_123240_rename_intermediates( $image ) {
    // Split the $image path into directory/extension/name
    $info = pathinfo($image);
    $dir = $info['dirname'] . '/';
    $ext = '.' . $info['extension'];
    $name = wp_basename( $image, "$ext" );

    // Get image information 
    // Image edtor is used for this
    $img = wp_get_image_editor( $image );
    // Get image size, width and height
    $img_size = $img->get_size();

    // Image prefix for builtin sizes
    // Note: beware possible conflict with custom images sizes with the same width
    $widths = [];
    $size_based_img_name_prefix = '';
    foreach ( get_intermediate_image_sizes() as $_size ) {
        if ( in_array( $_size, [ 'thumbnail', 'medium', 'medium_large', 'large' ] ) ) {
            $width = get_option( "{$_size}_size_w" );
            if ( ! isset( $widths[ $width ] ) ) {
                $widths[ $width ]  = $_size;
            }
        }
    }
    if ( array_key_exists( $img_size[ 'width' ], $widths ) ) {
        $size_based_img_name_prefix = $widths[ $img_size['width'] ] . '-';
        $name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
    } else {
        $name_prefix = $name;
    }

    // Build our new image name
    $new_name = $dir . $size_based_img_name_prefix . $name_prefix . $ext;

    // Rename the intermediate size
    $did_it = rename( $image, $new_name );

    // Renaming successful, return new name
    if( $did_it )
        return $new_name;

    return $image;
}

Information Sources:

  • image_make_intermediate_size
  • wp_get_image_editor and Class: WP_Image_Editor
  • PHP: switch

Notes: untested

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论