【问题标题】:add_image_size() using ratio instead of sizes - WordPressadd_image_size() 使用比率而不是大小 - WordPress
【发布时间】:2012-08-30 11:42:35
【问题描述】:

我正在开发一个 WordPress 主题,我需要通过内置媒体上传器上传的图像具有相同的比例。我知道这很容易通过定义高度和宽度并使用裁剪来完成。

问题是我想将这些图像用于全屏滑块,因此将所有图像缩小到 300x200 作为示例是行不通的。我希望尽可能多地保留图像的默认大小,同时按比例裁剪它。

WordPress 带有 add_image_size() 功能,但我更喜欢寻找 add_image_ratio() 功能。这甚至可能吗?

【问题讨论】:

  • 你考虑过使用 ThumbGen 插件吗?
  • 我不太喜欢为主题使用插件。我宁愿把它全部内置到主题中。

标签: wordpress image wordpress-theming add image-size


【解决方案1】:

您可以使用比率:

$thubmnail_ratio = (150 / 200); // height to width ratio, you can use some existing working ratio

add_image_size( 'thumbnail-small',  100, round(100*$thubmnail_ratio), true );
add_image_size( 'thumbnail-medium', 200, round(200*$thubmnail_ratio), true );
add_image_size( 'thumbnail-large',  400, round(400*$thubmnail_ratio), true );

也应该可以编写一个包装 add_image_size(...) 的小辅助函数。

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2019-05-19
    • 2017-01-13
    • 2012-11-12
    • 2023-03-06
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多