简体   繁体   English

从wordpress中的图像获取高度和宽度

[英]Get height and width from image in wordpress

I'm trying to make an info block on my WordPress website I am working on. 我正在尝试在我正在工作的WordPress网站上进行信息屏蔽。 I want to get the height X width of the photo: 我想获取照片的高度X宽度:

get_the_post_thumbnail_url() 

I want it printed out on the page something like: 我希望将其打印在页面上,例如:

image size: 1234x123 1.2mb

I just don't know how to make it. 我只是不知道该怎么做。 I have tried a lot of different examples I found on the web but I always get different errors. 我尝试了很多在网上找到的不同示例,但总是遇到不同的错误。

Hope someone has an idea of a good way to do this. 希望有人对实现此目标的好方法有所了解。 :) :)

This WP function wp_get_attachment_image_src , provides image width and height: 此WP函数wp_get_attachment_image_src提供图像的宽度和高度:

Read here more: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/ 在这里阅读更多: https : //developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

From docs, it says that it Returns: 从文档中说,它返回:

Returns an array (url, width, height, is_intermediate), or false, if no img. 返回一个数组(URL,宽度,高度,is_intermediate),如果没有img,则返回false。

For Example: 例如:

  $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), "full" );

Now 现在

$url = $img[0];
$width = $img[1];
$height = $img[2];

Try 尝试

<?php
    $size = getimagesize(get_the_post_thumbnail_url());
?>

and then echo out the size 然后回显大小

<?php
    echo $size;
?>

taken from the docs here 取自此处的文档

you can try below code 你可以尝试下面的代码

            if ( has_post_thumbnail() ) {
                    //thumbnail
                    echo  get_the_post_thumbnail(get_the_ID(),"thumbnail"); //thumbnail,medium,large,full,array(1234,123)
                    //medium
                    echo  get_the_post_thumbnail(get_the_ID(),"medium");
                    //large
                    echo  get_the_post_thumbnail(get_the_ID(),"large"); 
                    //full
                    echo  get_the_post_thumbnail(get_the_ID(),"full"); 
                    //fixed size image(width, height)
                    echo  get_the_post_thumbnail(get_the_ID(),array(1234,123)); 

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM