WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get media settings image size dimensions (7 posts)

  1. wpismypuppet
    Member
    Posted 2 years ago #

    Kind of an odd request... but I'm looking for a variable that holds the information for all the media settings image sizes.

    So in the admin area, when you go to media, you have the options of changing those default image size settings. I'm looking for a way to get those actual dimensions dynamically.

    I am aware of functions such as wp_get_attachment_image_src to get a specific image's width and height... but I'm not looking for a specific image. I want the dimensions someone enters into the media image setting fields.

    I came up with this solution:

    <?php
    $myrows = $wpdb->get_results("SELECT option_name, option_value FROM wp_options WHERE option_name = 'medium_size_w' OR option_name = 'medium_size_h'");
    
    ${$myrows[0]->option_name} = $myrows[0]->option_value;
    ${$myrows[1]->option_name} = $myrows[1]->option_value;
    
    echo $medium_size_w;
    echo $medium_size_h;
    ?>

    But I'm still looking for a safer/easier solution... if anyone has one.

  2. justingreerbbi
    Member
    Posted 2 years ago #

    I am not sure how you can grab the size from the core of WP but I think that when a user enters a size of the image in the field it adds a style tag to the <img tag and this is where it gets its size. I am not sure how it works completely .

    Are you developing a plugin that hooks into this feature?

  3. wpismypuppet
    Member
    Posted 2 years ago #

    Well, it's sort of a plugin. I have a custom function to create custom meta boxes dynamically. All you have to do is create an array... my function loops through the array and creates a table of meta box form fields.

    One of the options you can use is "photo" which basically creates a button on the page to allow you to open the built in Media Uploader in a thichbox. You can select an image from there, and it will place the id of that image in a hidden field, but show the image itself in the meta box as a preview.

    Since the javascript code only gets the path to the original image (which is what I want), I need the javascript to also be able to add a width and height attribute to the "preview" image when it inserts it... to keep it nicely formatted. When the page loads, PHP handles that piece, so no need to worry about that.

    Again, my solution above is working, and I'm getting the values. I'm just not sure it's the best approach and am looking for suggestions.

  4. justingreerbbi
    Member
    Posted 2 years ago #

    Interesting!

  5. wpismypuppet
    Member
    Posted 2 years ago #

    Yeah, pretty interesting stuff. I'd be willing to share the information with anyone who's interested. But before I do, I'd like to know that I'm doing it a clean and effective way. Anyone?

  6. iamskwerl
    Member
    Posted 2 years ago #

    foreach (get_intermediate_image_sizes() as $s) {
    	if (isset($_wp_additional_image_sizes[$s])) {
    		$width = intval($_wp_additional_image_sizes[$s]['width']);
    		$height = intval($_wp_additional_image_sizes[$s]['height']);
    	} else {
    		$width = get_option($s.'_size_w');
    		$height = get_option($s.'_size_h');
    	}
    }

    get_intermediate_image_sizes() is a built-in wordpress function that gets all registered image sizes. however, the height/width of the built-in sizes (defined on the media settings page) are stored differently than the dimensions of theme-defined sizes. hence the if/else.

    you may have to add:

    global $_wp_additional_image_sizes

    best of luck.

  7. wpismypuppet
    Member
    Posted 2 years ago #

    This is exactly what I needed!!! Thanks so much.

    When using my code above, I would have to change the table name from site to site... a bit of a nuisance. But with your code, I can add this to my core functionslib.php (my additional functions.php file for functions that never change and are used in all our sites) for all future WordPress sites!!!

    Again, thanks so much for helping.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.