• Resolved wpismypuppet

    (@wordpressismypuppet)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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?

    Thread Starter wpismypuppet

    (@wordpressismypuppet)

    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.

    Interesting!

    Thread Starter wpismypuppet

    (@wordpressismypuppet)

    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?

    iamskwerl

    (@iamskwerl)

    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.

    Thread Starter wpismypuppet

    (@wordpressismypuppet)

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get media settings image size dimensions’ is closed to new replies.