PHP Wordpress动态自定义图像大小
wordpress通常对图像有很好的支持 要获得新的图像大小,只需添加一些函数,如:PHP Wordpress动态自定义图像大小,php,wordpress,image-size,Php,Wordpress,Image Size,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_ima
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
有办法吗?我在哪里可以钓到它?
这些图像大小在许多函数中使用-
有人能想出一种更具创意的方法来实现这一点吗?你可以使用wp\u get\u attachment\u image\u src来获取附件的缩小图像,在这种情况下,你只需要在
functions.php
文件中指定添加主题支持('post thumbnails')
,然后在模板中执行以下操作:
$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...
或者您可以使用过滤器
图像\调整大小\尺寸
我已经设置了一个新的图像,具有奇怪的宽度和高度,就像这样
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;
}
}
谢谢你的回答。首先,它将用于插件,而不是主题。第二,如果我使用这种方法,上传时不会创建拇指,或者我在这里错了吗?你也可以在插件中调用
wp\u get\u attachment\u image\u src
。是的,调整大小的图像将在第一次调用该方法时创建,但我想您可以挂接到add_attachment
,并在原始图像上载后创建它们。您的观点是什么?如果你连接到add_attachment
并调用上面的代码,每次上传一张图片时,你都会自动创建3个版本,这实际上就是你使用add_image_size
的方法,不是吗?