サーバ室の快適環境の中、常にサーバ・PCとにらめっこをしている がんちゃんです。
先日の健康診断にて視力がけっこう落ちててかるくヘコんでおります。
WordPressでは画像をアップロードする際に、自動的にサムネイル画像を作ってくれる機能がありますが、この画像のサイズは128×96固定になってしまい使い勝手があまりよくありません。
今回はこの作成されるサムネイル画像のサイズ変更をしてみます。
まず、wp-admin/admin-functions.phpのオリジナルファイルのバックアップを取っておきます。
cp -a admin-functions.php admin-functions.php.org
アップロードする画像のサイズを変更するには、wp-admin/admin-functions.phpを編集するとのこと。
vi /wp-admin/admin-functions.php
function get_udims( $width, $height) {
#if ( $height <= 96 && $width <= 128 )
if ( $height <= 480 && $width <= 640 )
return array( $width, $height);
elseif ( $width / $height > 4 / 3 )
#return array( 128, (int) ($height / $width * 128 ));
return array( 640, (int) ($height / $width * 640 ));
else
#return array( (int) ($width / $height * 96 ), 96 );
return array( (int) ($width / $height * 480 ), 480 );
}
if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
#$max_side = apply_filters( ‘wp_thumbnail_max_side_length’, 128, $attachment_id, $file );
$max_side = apply_filters( ‘wp_thumbnail_max_side_length’, 640, $attachment_id, $file );
$thumb = wp_create_thumbnail( $file, $max_side );if ( @file_exists($thumb) )
$metadata['thumb'] = basename($thumb);
}
128 と 96 と書かれた部分を検索して自分の求めているサイズの数値に指定をします。
画像サイズは4:3 の割合で指定するのがいいとのこと。
最後にwp-admin/admin-functions.php.orgとの差分を確認します。
diff admin-functions.php.org admin-functions.php
< if ( $height <= 96 && $width <= 128 )
—
> if ( $height <= 480 && $width <= 640 )
2011c2011
< return array( 128, (int) ($height / $width * 128 ));
—
> return array( 640, (int) ($height / $width * 640 ));
2013c2013
< return array( (int) ($width / $height * 96 ), 96 );
—
> return array( (int) ($width / $height * 480 ), 480 );
2126c2126
< $max_side = apply_filters( ‘wp_thumbnail_max_side_length’, 128, $attachment_id, $file );
—
> $max_side = apply_filters( ‘wp_thumbnail_max_side_length’, 640, $attachment_id, $file );
今回の方法ではWordPressのファイルを書き換えていますので、アップデートなど行うと修正内容が上書きされてしまう可能性があります。サムネイル画像のサイズを指定できる下記のようなプラグインもいくつかありますので、そちらを利用するのも一つの方法です。