Image styles theme function theme_image_style() uses $variables['getsize'] = TRUE to determine width/height
But this feature was removed from theme_image() in favour of explicitly passing in $variables['width'] and $variables['height']

This causes problems such as #1012324: Images don't display fully when visiting the view's URL

The following adds this functionality back:

function theme_image_style($variables) {
  $style_name = $variables['style_name'];
  $path = $variables['path'];

  // theme_image() can only honor the $getsize parameter with local file paths.
  // The derivative image is not created until it has been requested so the file
  // may not yet exist, in this case we just fallback to the URL.
  $style_path = image_style_path($style_name, $path);
  if (!file_exists($style_path)) {
    $style_path = image_style_url($style_name, $path);
  }
  $variables['path'] = $style_path;

  if ($variables['getsize'] && (is_file($style_path) && (list($width, $height, $type, $attributes) = @getimagesize($style_path)))) {
    $variables['width'] = $width;
    $variables['height'] = $height;
  }

  return theme('image', $variables);
}

Comments

redndahead’s picture

Status: Needs work » Needs review
StatusFileSize
new851 bytes

Here is the patch

Status: Needs review » Needs work

The last submitted patch, 1012416-getsize-1.patch, failed testing.

redndahead’s picture

Status: Needs work » Needs review
StatusFileSize
new1.62 KB

Test weren't passing because during render getsize was set to false when the default value is true. So output wasn't matching. Fixed the test.

Status: Needs review » Needs work

The last submitted patch, 1012416-getsize-2.patch, failed testing.

redndahead’s picture

Status: Needs work » Needs review
StatusFileSize
new1.85 KB

Let's try this one. Need to use theme('image_style' since that is what is being used to display the image on the node. This also takes different variables.

redndahead’s picture

StatusFileSize
new1.85 KB

This patch just removes extra white spacing. Will still pass tests.

redndahead’s picture

If you want to see a place where the change is made you can add an image to a user profile. Before the patch the image will not have the width and height attributes on them. After the patch they will be there. This should also be the case with an image field.

You can also see where this existed in imagefield module before being incorporated into d7. On line 328:
http://drupalcode.org/viewvc/drupal/contributions/modules/imagefield/ima...

threewestwinds’s picture

Status: Needs review » Reviewed & tested by the community

Works great for me, and code looks good. The change to the test has no functional change - it just uses the API properly instead of unnecessarily doing a manual step. Let's make stuff work like it's supposed to.

webchick’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

Ok, I knew I recognized this issue.

See #908282: Remove unnecessary I/O from theme_image() where this was removed, on purpose. :( :( We'll need to get some reviews from the folks over there in order to add this back, though I agree it's a major WTF right now that we have a function argument that completely lies.

sun’s picture

Status: Closed (duplicate) » Closed (works as designed)

Reasoning for why getsize doesn't work for image styles:
#908282-15: Remove unnecessary I/O from theme_image()

eric_a’s picture

In the other issue I just suggested that we need a new render element type.
I feel that code like this really should be in a preprocessor or in a render element type. The point being that theme functions and templates are for markup and overriding markup, basically.

luco’s picture

the patch provided by redndahead in #6 worked for me. thanks :]

redndahead’s picture

@luco I wouldn't use that patch. In a future release of drupal 7 it won't work anymore.

Wolfgang Reszel’s picture

You could add the function to your template.php. For Drupal 7.2 you have to remove "$variables['getsize'] && " from the if statement.

slackrunner’s picture

Status: Closed (works as designed) » Needs review

#6: 1012416-getsize-4.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1012416-getsize-4.patch, failed testing.

redndahead’s picture

Status: Needs work » Closed (works as designed)

Please do not run tests on this. It's not going to get fixed.

eric_a’s picture

sumerokr’s picture

Status: Closed (works as designed) » Needs review

#6: 1012416-getsize-4.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1012416-getsize-4.patch, failed testing.

damien tournoud’s picture

Status: Needs work » Closed (works as designed)
jpmesquita’s picture

Status: Closed (works as designed) » Needs review

#6: 1012416-getsize-4.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1012416-getsize-4.patch, failed testing.

redndahead’s picture

Status: Needs work » Closed (fixed)