Drupal - Get the Dimensions of the Theme Logo

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);
    }
  }

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.)

Tags:

Files