1

wordpress 通常对图像有很好的支持。

要获得新的图像尺寸,只需添加一些功能,例如:

add_theme_support( 'post-thumbnails' ); //thumnails
set_post_thumbnail_size( 200, 120, true ); // Normal post thumbnails
add_image_size( 'single-post-thumbnail', 400, 300,False ); // single-post-test
add_image_size( 'tooltip', 100, 100, true ); // Tooltips thumbnail size
/// and so on and so on 

我的问题是:

有人如何使这些功能以动态方式运行,这意味着这些大小将在上传时计算?

例如 - 如果我上传 3000x4000 像素的图片 - 我希望我的图片尺寸为:

 add_image_size( 'half', 50%, 350%, False ); // Half the original
 add_image_size( 'third', 30%, 30%, true ); // One Third the original

有没有办法做到这一点 ?我在哪里可以挂钩?这些图像尺寸用于注册在许多功能中 - 有人能想到一种 Uber 创造性的方式来实现这一点吗?

4

2 回答 2

2

或者你可以使用过滤器image_resize_dimensions

我已经设置了一个奇怪的宽度和高度的新图像,就像这样

add_image_size('half', 101, 102);

然后仅在调整一半图像大小时才使用过滤器将图像减半

add_filter( 'image_resize_dimensions', 'half_image_resize_dimensions', 10, 6 );

function half_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
    if($dest_w === 101){ //if half image size
        $width = $orig_w/2;
        $height = $orig_h/2;
        return array( 0, 0, 0, 0, $width, $height, $orig_w, $orig_h );
    } else { //do not use the filter
        return $payload;
    }
}
于 2014-03-23T19:50:14.743 回答
1

您可以使用 wp_get_attachment_image_src 来获取附件的缩小图像,如果您只需要add_theme_support( 'post-thumbnails' )functions.php文件中指定,然后在模板中执行以下操作:

$id = get_post_thumbnail_id($post->ID)
$orig = wp_get_attachment_image_src($id)
$half = wp_get_attachment_image_src($id, array($orig[1] / 2, orig[2] / 2))
$third = wp_get_attachment_image_src($id, array($orig[1] / 3, orig[2] / 3))
etc...
于 2012-03-31T06:12:37.123 回答