I've been working with Insert + Highslide modules and noticed that width and height attributes of imagecache thumbnail are not set until image is generated by imagecache.
I fixed this by customizing theme_imagecache function by replacing these lines:

if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
    $attributes['width'] = $image['width'];
    $attributes['height'] = $image['height'];
  }

With my new ones:

if ($getsize) {
	  $cachepath = imagecache_create_path($presetname, $path);
	  if (!is_file($cachepath)) {
		  $preset = imagecache_preset_by_name($presetname);
		  $dst = imagecache_create_path($presetname, $path);
		  imagecache_build_derivative($preset['actions'], $path, $dst);
	  }
	  if ($image = image_get_info($cachepath)) {
		$attributes['width'] = $image['width'];
		$attributes['height'] = $image['height'];
	  }
  }

This code generates the image if it's not generated already.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Brodingo’s picture

I tried to override theme_imagecache as phptemplate_imagecache with this code and neither width or height was set when I inserted an image.

jrstmartin’s picture

FileSize
1.18 KB

Thank you for this! I was having a problem in Views Slideshow where the first/new slide, which contained an image, was halfway hidden. The height that Views Slideshow's JS was setting on the enclosing <div> was wrong because it couldn't calculate the dimensions of the image contained within it, as the dimensions were not set. This patch sets the dimensions and everything is working!

fizk’s picture

Category: bug » feature
Issue tags: -insert, -dimensions +ImageCache 3

Marking as ImageCache 3.x Todo.

j0rd’s picture

Same problem and causes a fairly large bug on my website, since I depend on these values being there for some image resizing JS I'm doing. Just noticed this problem after I changed the imagecache's and flushed the images and now my website doesn't work as expected.

// Drupal 7 equiv for those who are curious

function theme_image_style($variables) {
  // Determine the dimensions of the styled image.
  $dimensions = array(
    'width' => $variables['width'],
    'height' => $variables['height'],
  );

  image_style_transform_dimensions($variables['style_name'], $dimensions);

  $variables['width'] = $dimensions['width'];
  $variables['height'] = $dimensions['height'];

  // Determine the url for the styled image.
  $variables['path'] = image_style_url($variables['style_name'], $variables['path']);
  return theme('image', $variables);
}

borgewarvik’s picture

I can confirm that this patch is working. Any plans on implementing it in an release soon?

fizk’s picture

Title: Width and height not set on theme_imagecahe properly » Width and height not set on theme_imagecache properly
fuzzy76’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Works for me.

fuzzy76’s picture

Version: 6.x-2.0-beta10 » 6.x-2.0-rc1
FileSize
968 bytes

Rerolled against 2.0-rc1.