更改 WordPress 中上传图片的缩略图大小

WordPress 中添加/编辑文章时,可以使用 WordPress 自带的 Uploader 上传文件或者图片。而如果服务器支持的话,WordPress 将会自动将太大的图片另外生成一个名字为 [FileName].thumbnail.[extension] 的缩略图放在上传目录下,这样我们可以在 Blog 中使用缩略图并链接到原始图从而避免图片太大影响浏览。但是 WordPress 默认的缩略图大小太小了,并且后台没有提供这样的选项来更改这个值,这是因为缩略图的长度和宽度是硬编码在 WordPress 的源代码中的。

要想更改缩略图的长宽值,首先需要修改 wp-admin/inline-uploading.php 这个文件,找到下面一段代码:

if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
if ( $imagedata['width'] > 128 && $imagedata[’width’] >= $imagedata[’height’] * 4 / 3 )
$thumb = wp_create_thumbnail($file, 128);
elseif ( $imagedata[’height’] > 96 )
$thumb = wp_create_thumbnail($file, 96);

将其中的宽度值 128 和长度值 96 改成你喜欢的值,最好保持 4:3 的比例。

另外还需要修改 wp-admin/inline-uploading.phpadmin-functions.php,找到此处:

function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) {
if ( $height <= $hmax && $width <= $wmax )
return array($width, $height);
elseif ( $width / $height > $wmax / $hmax )
return array($wmax, (int) ($height / $width * $wmax));
else
return array((int) ($width / $height * $hmax), $hmax);
}

function get_udims($width, $height) {
if ( $height <= 96 && $width <= 128 )
return array($width, $height);
elseif ( $width / $height > 4 / 3 )
return array(128, (int) ($height / $width * 128));
else
return array((int) ($width / $height * 96), 96);
}

同样改掉其中的数值就可以了。不知道 WordPress 后续的版本会不会考虑提供这样的选项供用户修改呢。

WordPress 官方论坛上的讨论贴:http://wordpress.org/support/topic/53640



Trackbacks & Pingbacks

  1. Think in Something / Problems Occured In Upgrading to WP 2.1 pingbacked Posted 2007-02-19, 22:52

    […] 有关我以前说的通过修改某些 WordPress 文件来实现增大上传图片的所略图的默认大小的功能,在 2.1 版本中,wp-admin/inline-uploading.php 文件不再存在,我试验过如果仅仅修改 wp-admin/admin-functions.php 是不够,因此如何在 WordPress 2.1 中实现同样的功能,还需要进一步研究。 […]

Comments

  1. Quote

    请问,我要怎么找到这段代码?翻遍了没找到……

  2. Quote

    不明白你为什么找不到?
    文件名是 wp-admin/inline-uploading.php 和 wp-admin/inline-uploading.php,然后搜索对应的函数就可以了啊。
    不过我的 WordPress 版本是 2.0.3 的,不清楚其余的版本是否有什么不一样。

  3. Quote

    不好意思,刚刚发现,第二个文件名写错了,已修正

  4. Quote

    还是找不到啊???

  5. Quote

    找到很容易,修改也很容易,但是有什么办法能直接禁用缩略图么?

  6. Quote

    To 周易:
    新版本的WP变了,参考这篇:
    http://www.thinkinsth.net/blog/321

    To iamcj:
    我还不知道,呵呵,应该可以修改代码的

  7. Quote

    嗯,我就是看看有没有人研究过,哈哈,看来得自己看一下代码了

Leave a Comment

(required)

(required)

Formatting Your Comment

The following XHTML tags are available for use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

URLs are automatically converted to hyperlinks.