PHP Wordpress 动态自定义图像大小 [英] PHP Wordpress dynamic custom image sizes

查看:21
本文介绍了PHP Wordpress 动态自定义图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wordpress 总体上对图像有很好的支持.

wordpress has a good support for images in general.

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

to get new image sizes, one would just add some functions like :

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 

我的问题是:

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

How can someone make those functions act in a dynamic manner , meaning that those sizes will be calculated on upload ?

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

for example - If I upload an image of 3000x4000 px - I would like my image sizes to be :

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

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

Is there a way to do that ? where can I hook for that ? Those image sizes are used registered in many functions - Can someone think of an Uber-creative way to achieve that ?

推荐答案

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

You can use wp_get_attachment_image_src to get downsized images of an attachement, in you case you only need to specify add_theme_support( 'post-thumbnails' ) in your functions.php file then in your template do the following:

$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...

这篇关于PHP Wordpress 动态自定义图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆