首页 文章

强制Wordpress创建与上载的图像大小相同的缩略图

提问于
浏览
8

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

我用

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;

那么我该如何启用这种“强制调整大小”功能呢?

7 回答

  • 2

    这是您要寻找的解决方案,放在您的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

    它将升级图像并为所有图像大小生成缩略图 . 唯一可能不是垮台的垮台是,无论你为所有上传的图像获得每个图像大小的单独文件......即使它是一个小图标或其他东西 . 希望这可以帮助 .

  • -3

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

    这将优化原始大小的图像 .

    可能会有一些警告,但实际上 - 任何东西都比修改核心更好 .

  • 0

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

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

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

  • -1

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

    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
        }
    }
    

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

  • 0

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

    简单图像尺寸

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

    enter image description here

  • -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中的完整文档

  • 0

    没关系,我改变了核心 . 在内部伤害我,但对于这个项目,它是最好的解决方案 .

相关问题