问题描述
我正在使用 WordPress Multisite 构建一个会员网站。是否可以限制根据所选模板生成的图像数量?
我已经尝试了以下代码行在图库模板上生成某些图像:
// Generate on all uploads
add_theme_support('post-thumbnails');
set_post_thumbnail_size( 1440, 350, true );
add_image_size( 'standard_box', 450, 215, true );
add_image_size( 'default_image', 691, 9999 );
// Generate on gallery template only
if ( is_page_template('page-gallery.php') ) {
add_image_size( 'gallery', 900, 9999 );
add_image_size( 'gallery_thumb', 450, 450, true );
}
这没有奏效。我做了一些研究,似乎没有找到任何关于这个问题。如果你能指出我正确的方向,我真的很感激。
最佳解决方案
这对我来说一直是一个 bugb,因为缺少 on-demand 图像大小,以及随后的文件数量,如果你有很多的大小!
我可以看到你的努力背后的逻辑 – 麻烦的是,add_image_size
只能真正发挥在 point-of-upload 。因此,is_page_template(..)
将始终为 false
。
一个快速的 google 挖掘 Aqua Resizer,一个旨在解决这个问题的脚本。而不是使用 add_image_size
,您可以直接在主题中使用 aq_resize
,如果图像的大小不存在,则会创建并缓存 on-the-fly 。
事实上,我在几个具有许多图像大小的网站上使用了类似的,尽管不同的技术。您仍然保存 WordPress 为每个上传的图像生成每个大小的开销 – 它们在请求时生成 on-the-fly(& Cached) 。不同之处在于,您可以像通常一样使用 WP 的所有标准图像功能和模板标签!
另外,如 @Waqas 所提到的,当您从媒体库中删除图像时,使用 Aqua Resizer 将离开孤立的文件。使用我的技术,所有文件将被删除,因为它们被保存到数据库& 被 WordPress 认可。
/**
* Resize internally-registered image sizes on-demand.
*
* @link http://wordpress.stackexchange.com/q/139624/1685
*
* @param mixed $null
* @param int $id
* @param mixed $size
* @return mixed
*/
function wpse_139624_image_downsize( $null, $id, $size ) {
static $sizes = array(
'post-thumbnail' => array(
'height' => 350,
'width' => 1440,
'crop' => true,
),
'standard_box' => array(
'height' => 215,
'width' => 450,
'crop' => true,
),
'default_image' => array(
'height' => 9999,
'width' => 691,
'crop' => false,
),
'gallery' => array(
'height' => 900,
'width' => 9999,
'crop' => false,
),
'gallery_thumb' => array(
'height' => 450,
'width' => 450,
'crop' => true,
),
);
if ( ! is_string( $size ) || ! isset( $sizes[ $size ] ) )
return $null;
if ( ! is_array( $data = wp_get_attachment_metadata( $id ) ) )
return $null;
if ( ! empty( $data['sizes'][ $size ] ) )
return $null;
if ( $data['height'] <= $sizes[ $size ]['height'] && $data['width'] <= $sizes[ $size ]['width'] )
return $null;
if ( ! $file = get_attached_file( $id ) )
return $null;
$editor = wp_get_image_editor( $file );
if ( ! is_wp_error( $editor ) ) {
$data['sizes'] += $editor->multi_resize(
array(
$size => $sizes[ $size ],
)
);
wp_update_attachment_metadata( $id, $data );
}
return $null;
}
add_filter( 'image_downsize', 'wpse_139624_image_downsize', 10, 3 );
在实践中
wp_get_attachment_image( $id, 'gallery' ); // Resized if not already
wp_get_attachment_image_src( $id, 'standard_box' ); // Resized if not already
the_post_thumbnail(); // You get the idea!
// And so forth!
我打算把它变成一个插件,它会自动将所有的 add_image_size
调用转换成 on-demand 调整大小,所以看这个空间!
次佳解决方案
免责声明:
– 这个
不
实际上是一个答案。
是
旨在帮助您进一步研究该主题。
– 此外
是
反映了至少感觉到最近更频繁地发生类似问题的类似问题。
关于 Wordpress 开发的这个主题的其他信息:
-
Generate fixed image sizes for different pages, eliminate sizes during upload
-
How to Dynamically Resize WordPress Image On-The-Fly (custom field/theme option)
-
How do I delete all generated images from my server except those currently used in posts
注意:列表没有任何手段订购和全面。
第三种解决方案
如果你想在飞行中创建拇指,你可以使用 Aqua image resizer,但这个迷你脚本有一个缺点。从库中删除图像时,创建的缩略图将不会被删除。但它不是很大的事情。如果需要,您可以通过 SHH commands 这样做
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。