Using 'manual crop' style, and leaving the either or both of the minimum height or minimum width fields blank results in missing height attributes on the resulting image tag.

I believe the problem is due to the fact that the image_resize_dimensions callback is being used as the dimensions callback to process this style (manualcrop_crop), and that callback expects to have both width and height set. The issue can be partially resolved by switching to use the image_scale_dimensions callback instead which will generate correct dimensions if either one or the other is set. However this raises an undefined index warning as the 'upscale' attribute is not set. Also, this doesn't work correctly if neither of the minimum dimensions are specified.

It seems that the 'Manual crop' style is really a mixture of 'manual scale' and 'manual crop' functionality. Either, what we need is to split crop and scale actions into two separate actions ('manual crop' and 'manual scale'), which both more closely mimic cores 'scale' and 'crop' actions in which case we could continue to use core's dimension callbacks. Or alternatively, we need our own dimensions callback that handles this action more cleverly.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mrfelton’s picture

On further investigation, it seems impossible to do this without fixing core. See #1364670: ImageStyle::transformDimensions unable to deal with all effects.

mrfelton’s picture

Status: Active » Needs review
FileSize
2.28 KB

With patch from http://drupal.org/node/1364670#comment-5943622 applied to drupal core, this patch fixes the issue by defining our own dimensions callback that is able to properly calculate the new dimensions.

Matthijs’s picture

Assigned: Unassigned » Matthijs

Thanks four your patch, I'll commit it as soon as you're other patch makes it to the core.

mrfelton’s picture

Awesome. Maybe you could review and provide some feedback on that core issue to help speed it along?

Matthijs’s picture

No problem, but do I have to review both the 7.x and 8.x patch (because this is a Drupal 8.x issue)?
I'm not an experienced core contributor :-)

Wolfgang Reszel’s picture

Thanks, the patch works well for D7.

Edit: Sorry, i disabled error messages. The following warning will be shown:

Notice: Undefined index: path in manualcrop_resize_dimensions() (line 970 of /sites/all/modules/manual-crop/manualcrop.module).

Sorry, i forgot to patch the core, but $data['path'] is always NULL. It works anyway. ;-)

mrfelton’s picture

Status: Needs review » Needs work

I'm having some issues with this now, sometime resulting in images with a 0 width and no height attributess.

Wolfgang Reszel’s picture

The patch is not correct. Replace ...

image_dimensions_scale($dimensions, $width, $height);

... with ...

image_dimensions_scale($dimensions, $data['width'], $data['height']);
mrfelton’s picture

Status: Needs work » Needs review
FileSize
2.29 KB

Thanks! Updated patch

Wolfgang Reszel’s picture

The Patch does not work with the latest Dev version.

Ankabout’s picture

Patch works for me on 7.x-1.3, but then I get the following error on all pages with a cropped image:
Notice: Undefined index: path in manualcrop_resize_dimensions() (line 960 of /var/www/vhosts/*******/httpdocs/sites/all/modules/manual-crop/manualcrop.module).

Ankabout’s picture

Priority: Normal » Major

Just figured that this is quite a major bug since it simply causes images not to load on ALL IE browsers.

Ankabout’s picture

Setting the following properties in CSS fixes the problem in the mean time:

width: auto;
height: auto;
mrfelton’s picture

Updated patch to apply against latest dev.

Jon_B’s picture

I don't think the patch is quite right. In some uses, i.e. depending on what other image effects are applied to the image style, it will appear correct. However, the issue with the $data['path'] = NULL, as raised in other issues / previous comments, means that the width and height of the manual crop cannot be queried in the manualcrop table in the database.

The issue is that the core image module is not set up to pass the path of the image to the effect callback. This probably could be patched, but the signature of the callback function would need to be changed, so that the path could be passed to the function. This would cause problems for all modules that implement a 'dimensions callback' function. So not sure how to resolve this issue for this patch.

One way may be to use hook_theme_registry_alter to change the theme_image_style callback, so that a new callback function could be created to work out the correct width/height attributes.

OnkelTem’s picture

This is updated version of the patch against the latest dev (6 Mar 2012).
It adds 3 different dimension callbacks for crop, crop and scale, and for reuse effects. The latter callback simply calls image_style_transform_dimensions() recursively. Please test & review.

NOTE: This patch requires applying core patch from #1364670-9: ImageStyle::transformDimensions unable to deal with all effects.

OnkelTem’s picture

FileSize
4.01 KB

Patch from #16, against the latest dev.
See the NOTE from #16.

Matthijs’s picture

Project: Manual Crop (old) » Manual Crop
Version: 7.x-1.x-dev » 7.x-1.4
Priority: Major » Normal
mrfelton’s picture

Updated patch to apply cleanly against latest dev with git apply (patch from #17 applied with fuzz)

mrfelton’s picture

Issue summary: View changes

fix typo

bessone’s picture

Issue summary: View changes

any update on this issue?

xkhaven’s picture

If someone is still looking for a quick fix for empty IMG tag dimension attributes, here's my solution.

function THEME_preprocess_image(&$vars) {
  // image has lacking dimensions
  if (empty($vars['height']) && empty($vars['width'])) {
    // get image size
    $image_size = getimagesize($vars['path']);
    // set measurements
    $vars['width'] = $image_size[0];
    $vars['height'] = $image_size[1];
  }
}
OnkelTem’s picture

FileSize
5.05 KB

Updated patch which also covers "Auto reuse" feature.
Please test & review

p.s. oops, I created patch with my credentials in autopilot. You are free to not use this data. Sorry.

Fabianx’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Assigned: Matthijs » Unassigned
FileSize
4.61 KB

Re-rolled patch for 7.x-1.4+109-dev.

One change in crop style was changed.

Fabianx’s picture

And add some more doxygen and fix code style.

OnkelTem’s picture

Status: Needs review » Reviewed & tested by the community

Let's commit it!

rbomhof’s picture

Just applied this patch, as we needed it to get the "img with srcset" functionality from the picture module: https://www.drupal.org/node/2450863#comment-10116088. Do you think this will be committed soon? Also we see an error with one of our image styles that has a "Manual Crop: Reuse cropped style" effect:

Notice: Undefined index: path in manualcrop_reuse_transform_dimensions() (line 1037 of manualcrop.module).

Not sure if anyone else has come across that.

Fabianx’s picture

#26: This needs a core patch for D7 (and D8).

BarisW’s picture

Thanks for the patch. It works perfect. I've attached a re-roll of #24 against 7.x-1.x with some minor Drupal coding standard improvements.
Would be nice to have this committed, since having width and height attributes on images really improves the end user experience (since the browser can allocate the space even before the image is downloaded).

AndyThornton’s picture

thanks BarisW and all

we'd love to see this patch applied too.

each time i apply it, though ... i have to modify a couple of lines to stop it erroring
1018: $crop = manualcrop_load_crop_selection(isset($data['path']) ? $data['path'] : null, $data["style_name"]);

and

1039: image_style_transform_dimensions($data['reuse_crop_style'], $dimensions, isset($data['path']) ? $data['path'] : null);

i guess this must be some idiosyncrasy of our system ... as no one else seems to mention it.

ShaunDychko’s picture

The manualcrop_crop_and_scale_transform_dimensions dimensions callback is invoked by image_style_transform_dimensions which passes only the style name and the original dimensions of the image. This means the $data['path'] will always be an undefined index, and manualcrop_load_crop_selection($data['path'], $data["style_name"]); will always return false. This means manualcrop_crop_and_scale_transform_dimensions can only ever return the dimensions configured in the style settings, so I've simplified the code for that function and removed the "Undefined index" error message it was causing. This creates the known limitation, which I think belongs in another issue since solving it requires changing something in Drupal core to make image_style_transform_dimensions pass along the image path:

// Known limitation: in the crop_and_scale transformation the dimensions
// assigned below will not be correct if upscaling is disallowed and the image
// is smaller than the crop area. The workaround is to ensure the image field
// enforces a minimum image size.

The image_style_transform_dimensions takes only two parameters, so I removed the third on a few lines.

ShaunDychko’s picture

Status: Reviewed & tested by the community » Needs review
BarisW’s picture

Status: Needs review » Reviewed & tested by the community

Tested the patch in #30. The width and height tags appear, and I see no notices. Great work!

Chris Charlton’s picture

Wow. Long time coming. :)

BarisW’s picture

Ping

Chris Charlton’s picture

Ping+

vinmassaro’s picture

Status: Reviewed & tested by the community » Needs work

I have 'Manual Crop: Automatically reuse cropped style' set for my image style, with a fallback style that uses 'Scale and Crop' to 575x334. I offer 3 'Manual Crop: Crop and Scale' presets at 3 different sizes. What I'm finding is that with this patch, the correctly sized image is loaded into the page, but the height/width attributes are always set to the fallback image style dimensions. In the code, it looks like manualcrop_auto_reuse_transform_dimensions() never gets a style from _manualcrop_get_auto_reuse_style_name() and always uses the fallback image dimensions.

Nobody has mentioned having a fallback image style set, so I'm setting status back to 'Needs work'. If there is a misconfiguration on my end, please let me know. Thanks!