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

Set custom name for generated thumbnails

programmeradmin1浏览0评论

Is there any filter available to set the naming convention of those auto generated thumbnails?

Something like this:

  • thumbnail_150x150.jpg -> thumbnail-s.jpg
  • thumbnail_300x300.jpg -> thumbnail-m.jpg
  • thumbnail_600x600.jpg -> thumbnail-l.jpg

Is there any filter available to set the naming convention of those auto generated thumbnails?

Something like this:

  • thumbnail_150x150.jpg -> thumbnail-s.jpg
  • thumbnail_300x300.jpg -> thumbnail-m.jpg
  • thumbnail_600x600.jpg -> thumbnail-l.jpg
Share Improve this question edited May 12, 2012 at 22:17 brasofilo 22.1k8 gold badges70 silver badges264 bronze badges asked May 11, 2012 at 21:05 Sean LeeSean Lee 1,1093 gold badges15 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

Seems that the answer is no...

I've followed the core functions and found a dead-end. And found this post ( How can I make add_image_size() crop from the top? ) where Rarst says:

Intermediate image generation is extremely rigid. Image_resize() keeps it close to code and completely lacks hooks.

But, following the lead of the other answer (from bradt) and the code he published ( Image Crop Position in WordPress ), I think I got it :)

In the function bt_generate_attachment_metadata, I've just modified the call to bt_image_make_intermediate_size including the last parameter $size

$resized = bt_image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'], $size );

And modified the beggining of the function bt_image_make_intermediate_size as follows:

  • added the $size parameter to the function
  • instead of the default null value to $suffix, a switch to our new suffixes
function bt_image_make_intermediate_size( $file, $width, $height, $crop = false, $size ) {
    if ( $width || $height ) {
        switch($size) {
            case 'thumbnail':
                $suffix = 't';
                break;
            case 'medium':
                $suffix = 'm';
                break;
            case 'large':
                $suffix = 'l';
                break;
            default:
                $suffix = null;
                break;
        }
        $resized_file = bt_image_resize( $file, $width, $height, $crop, $suffix, null, 90 );

 Here , a copy of the full code with my mods, just for reference.
And the diff from the original.

[2021]
I reviewed the code to fix the following warnings, (h/t @meek2100): two WP functions were deprecated and there's a PHP 8 notice about named parameters.
Gist with the changes annotated on code.

You could use the filter image_make_intermediate_size, but you would have to figure out what name you want to change the intermediate file to, according to the $filename you get (and then rename the file, because at this point it already has been generated).

I only generate an intermediate size image for the "thumbnail", so it is as simple as this:

add_filter( 'image_make_intermediate_size', function( $filename ) {

    // old 2017_234783843-100x100.jpg
    $old = $filename;
    // new 2017_234783843-thumbnail.jpg
    $new = preg_replace("/(\d+_\d+)-\d+x\d+\.(.*)/i", "$1-thumbnail.$2", $old );

    rename($old, $new);

    return $new;
} );
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>