8

AFAIK,Wordpress 仅在所需图像尺寸较小时(而不是在目标尺寸相同时)才生成缩略图(例如,“大”)。

我用

add_image_size( 'referenzen-big', 627, 490, true );

因此,如果我的客户上传一个 627x490px 的文件,将使用原始图像。

但这并不是我们所希望的,因为这种习惯是出于善意,会以正确的大小上传图像,并且还可以进行最高的 .jpg 压缩。在这种情况下,这导致图像大约为 300kB。

一种实用但技术上并非完美无缺的方法是要求他上传 628x490 的图像,因此宽度增加 1px,从而强制重新缩放。这样的建议将不被接受:-)

到目前为止,我已经调查了负责图像创建的钩子,并跟踪了负责的函数;这是我找到的最后一个钩子: image_make_intermediate_size()

这是负责实际调整大小的函数: image_resize_dimensions()。甚至有一句台词:

// if the resulting image would be the same size or larger we don't want to resize it
if ( $new_w >= $orig_w && $new_h >= $orig_h )
    return false;

那么如何启用此“强制调整大小”功能?

4

7 回答 7

3

这是您正在寻找的解决方案,将放在您的 functions.php 文件中:

function thumbnail_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ){
    if ( !$crop ) return null; // let the wordpress default function handle this

    $aspect_ratio = $orig_w / $orig_h;
    $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

    $crop_w = round($new_w / $size_ratio);
    $crop_h = round($new_h / $size_ratio);

    $s_x = floor( ($orig_w - $crop_w) / 2 );
    $s_y = floor( ($orig_h - $crop_h) / 2 );

    return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
add_filter( 'image_resize_dimensions', 'thumbnail_upscale', 10, 6 );

可以在这里找到: https ://wordpress.stackexchange.com/questions/50649/how-to-scale-up-featured-post-thumbnail

它将放大图像并为所有图像尺寸生成缩略图。唯一的失败,可能不是失败,是不管你会为所有上传的图像的每个图像大小获得一个单独的文件......即使它是一个小图标或其他东西。希望这可以帮助。

于 2015-11-18T20:24:27.917 回答
0

虽然仍然不是最优的,但使用像Timthumb这样的服务器端库然后提供原始源并使用“q”(=质量)参数不是更好吗?

这将使用原始大小优化图像。

可能有一些警告,但实际上 - 任何事情都比修改核心更好。

于 2013-07-22T09:51:49.160 回答
0

如果磁盘空间不是问题,而只是下载速度,就像我的情况一样......

我面临的问题是客户没有注意特征图像上传的大小或尺寸。该主题是一个响应式主题,适用于图像高度范围并缩放到页面中所需的图像宽度。因此,如果坐在高速网络上,可以上传非常大的文件而不会注意到大小。

我添加了自定义图像大小,使用插件强制重新生成并修改主题以显示自定义大小。

于 2013-08-24T18:36:06.137 回答
0

我遇到了类似的问题:客户网站应该产生更大的拇指。我最终构建了一个带有自定义图像编辑器的插件,如下所示:

require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';

function im_add_image_editor($implementations) {
    array_unshift($implementations, 'IM_Image_Editor_GD');
    return $implementations;
}
add_filter('wp_image_editors', 'im_add_image_editor');

IM_Image_Editor_GD 扩展了 WP_Image_Editor_GD,将 _resize 函数(调用者image_resize_dimensions)更改为调用它的内部版本,如下所示:

class IM_Image_Editor_GD extends WP_Image_Editor_GD {
    public function __destruct() {
        parent::__destruct();
    }

    protected function _resize( $max_w, $max_h, $crop = false ) {
        $dims = $this->image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
        if ( ! $dims ) {
            return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
        }
        list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;

        $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
        imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );

        if ( is_resource( $resized ) ) {
            $this->update_size( $dst_w, $dst_h );
            return $resized;
        }

        return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
    }

    protected function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
        // Copied original image_resize_dimensions and changed the test
    }
}

希望这对我的英语有所帮助和抱歉!

于 2016-02-24T03:35:01.157 回答
-1

您可以使用此插件访问https://wordpress.org/plugins/simple-image-sizes/进行管理

简单的图像尺寸

此插件允许为您的站点创建自定义图像大小。直接在媒体选项页面上覆盖您的主题大小。您可以重新生成刚刚创建的所有尺​​寸并选择要重新生成的尺寸。您现在可以将所有代码复制并粘贴到您的函数主题文件中。现在您可以直接在帖子中使用生成的尺寸,并以正确的尺寸插入图片!现在您选择是否要在插入后的图像中显示大小。现在您可以在“媒体”常规窗格中一张一张地重新生成图像。现在,您可以通过“媒体”常规窗格中的批量操作重新生成图像。现在您可以在单个附件编辑页面上重新生成图像大小。


在此处输入图像描述

于 2014-06-05T12:59:38.943 回答
-1

WordPress 的默认图片大小为“缩略图”、“中”、“大”和“完整”(您上传的图片的大小)。这些图像大小可以在设置 > 媒体下的 WordPress 管理媒体面板中进行配置。这就是您如何使用 the_post_thumbnail() 使用这些默认大小的方法:

the_post_thumbnail();                  // without parameter -> 'post-thumbnail'
the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Full resolution (original size uploaded)
the_post_thumbnail( array(100,100) );  // Other resolutions

另请参阅http://codex.wordpress.org/Function_Reference/the_post_thumbnail中的完整文档

于 2014-07-22T11:00:33.793 回答
-2

没关系,我换了核心。伤害了我的内心,但对于这个项目来说,这是最好的解决方案。

于 2012-11-21T16:25:28.620 回答