How to get the image sizes, in a function, as per the sizes mentioned in the media settings?

I’m making a custom function for my WordPress site.

function mediumFeaturedImage() {
    if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( 'medium' ); ?>
    </a>
    <?php } else { ?>                                
    <img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg" alt="No Image Given" width="220" height="145"/>
    <?php }
}

I set the medium size images to 220 x 145 in Settings > Media.

Read More

But if you look at the default ‘no-image’ code block here, I used hard-coded numerals as width="220" height="145". But I want to use this function into some other projects of mine. And I won’t use the same settings for medium sized images always. It’ll be different from site to site. And always I want my code to be dynamic. Therefore I want a code like this:

<img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg" width="<?php $getimagesize('medium','width); ?>" height="<?php $getimagesize('medium','height); ?>"/>

It’s not mandatory using a function like me. Is there a built-in code like this, that will grab the sizes I assigned in Media settings in WP-Admin, always?

Or, by using the wp_get_attachment_image_src() how can I grab the width and height of medium size image only, that assigned for medium sized images in Admin panel?

Possible?

Related posts

2 comments

  1. I think you want wordpress setting for media

    for medium size their are

    get_option('medium_size_w');//width
    
    get_option('medium_size_h');//height
    

    all setting store in options table.get more media setting

  2. PHP has a function called getimagesize() which returns the an array having width as index 0, height as index 1 and other attributes.

    $x = getimagesize('image.jpg');
    
    print_r($x);
    

    Output:

    Array
    (
        [0] => 245 <--width
        [1] => 112 <--height
        [2] => 2
        [3] => width="245" height="112"
        [bits] => 8
        [channels] => 3
        [mime] => image/jpeg
    )
    

Comments are closed.