Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

In Drupal 7 theme settings, you can upload a logo. How do you print the width and height of this logo in your theme?


Background Research:

theme_get_setting('logo') lets you get the path of a logo uploaded in the theme settings.

image_get_info lets you get the height and width of an image.

However, image_get_info(theme_get_setting('logo')) does nothing.

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

You can use the following code. (Add error checking where necessary.)

  $image_url = parse_url(theme_get_setting('logo'));
  $image_path = str_replace(base_path(), realpath(DRUPAL_ROOT) . '/', $image_url['path']);
  $info = image_get_info($image_path);
  $height = $info['height'];
  $width = $info['width'];

As side note, theme_get_settings('logo_path') returns the path of the uploaded file used as logo. While theme_get_settings('logo') always return a value, whatever the logo is the default one, or one that has been uploaded, theme_get_settings('logo_path') returns a value when the logo has been uploaded.
The value returned for "logo_path" is similar to public://logo.png. To obtain the real path, you would use code similar to the following one.

  if ($image_uri = theme_get_settings('logo_path')) {
    if ($wrapper = file_stream_wrapper_get_instance_by_uri($image_uri)) {
      $image_path = $wrapper->realpath();
    }
    elseif (!empty($image_uri)) {
      $image_path = realpath($image_uri);
    }
  }
share|improve this answer
    
Correct me if I'm wrong. You can also use url() with absolute and aliased keys set to TRUE and path set to the front page. It will give an absolute URL to the front page so you can str_replace. –  Ayesh K Dec 17 '12 at 6:30
    
If there aren't modules implementing hook_outbound_alter() that alter the URL for the front page, it can work too. –  kiamlaluno Dec 17 '12 at 6:33
    
Thanks! I used to put it everywhere even for files. Thanks for the outbound alter tip. Even though it's not used everywhere that can beat my bad habit. –  Ayesh K Dec 17 '12 at 6:37
    
@kiamlaluno This is brilliant! Thanks! –  big_smile Dec 17 '12 at 6:52
add comment

The call to theme_get_setting('logo') gives you the URL of the logo, not the path (which is what you need in order to use image_get_info). The reason your call to image_get_info returns nothing (actually FALSE) is that you're feeding it garbage. As the old saying goes: GIGO.

You need to do something like the following:

$filepath =  $_SERVER['DOCUMENT_ROOT'] . base_path() . path_to_theme() . '/logo.png';
$image_info = image_get_info($filepath);
$width  = $image_info['width'];
$height = $image_info['height'];

(My method for sniffing the $filepath is probably not optimal, as it assumes the logo file is named "logo.png", which is not always the case, but I hope you get the idea.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.