0

I had a scale and crop image style created, but I had to move it to code. So, to re-create it, my code is:

function mymodule_image_default_styles() {
    $styles = array();

    $styles['mymodule_thumb'] = array(
        'label' => 'MyModule Thumbnail',
        'effects' => array(
            array(
                'name' => 'image_scale_and_crop',
                'data' => array(
                    'width' => 210,
                    'height' => 210

                ),
            )
        ),
        'name' => 'mymodule_thumb',
        'storage' => IMAGE_STORAGE_NORMAL

    );

    return $styles;
}

It's scaling to the 210px width, but it doesn't seem to be cropping right.

Am I missing something? Before this, I tried separate scale and crop effects, but got the same result.

EDIT: If it matters, I'm calling image_style_url() and passing the style.

Works when setting through UI

Doesn't work with my code

  • Why not use Features to export it then copy the code? – Beebee Aug 14 '14 at 8:47
  • I already have a ton of stuff set up for ctools export. It's only 3 styles, so I was hoping to just throw them into the style hook. – vintorg Aug 14 '14 at 9:04
  • What I meant is, you can export it into a dummy feature, then just cut and paste the hook_image_default_styles code it generated in the featurename.features.inc file into your own module. And then delete that dummy feature. But see my answer below if you want to have a manual stab at it. – Beebee Aug 14 '14 at 10:09
1

I suspect the hook_image_defaults_styles API page might be a bit out of date?

The features exported hook_image_default_styles code is slightly different on my drupal. Maybe this will help:

function hook_image_default_styles() {
  $styles = array();

  // Exported image style: learning_sessions_image.
  $styles['learning_sessions_image'] = array(
    'name' => 'learning_sessions_image',
    'effects' => array(
      7 => array(
        'label' => 'Scale and crop',
        'help' => 'Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.',
        'effect callback' => 'image_scale_and_crop_effect',
        'dimensions callback' => 'image_resize_dimensions',
        'form callback' => 'image_resize_form',
        'summary theme' => 'image_resize_summary',
        'module' => 'image',
        'name' => 'image_scale_and_crop',
        'data' => array(
          'width' => 963,
          'height' => 270,
        ),
        'weight' => 1,
      ),
    ),
    'label' => 'learning_sessions_image',
  );
  return $styles;
}

What you're missing is the effect callback and dimensions callback which are the essential parts afaik. I'm not sure about the significance of the array index under effects, or the form callback and summary theme. Hope this helps.

| improve this answer | |
  • That works. I actually haven't used features yet, so I will definitely be using it for exporting. – vintorg Aug 15 '14 at 0:08
1
function mymodule_image_default_styles() {
  $styles = array();
  $styles['mymodule_thumb'] = array(
    'label' => 'MyModule Thumbnail',
    'effects' => array(
      array(
        'name' => 'image_scale_and_crop',
        'data' => array(
          'width' => 210,
          'height' => 210,
          'upscale' => 1,
        ),
        'weight' => 0,
      )
    ),
    'name' => 'mymodule_thumb',
    'storage' => IMAGE_STORAGE_NORMAL
  );

  return $styles;
}

Clear your cache.

| improve this answer | |
  • I did clear my cache. I even went into the default/files... styles directories and removed the directories. I've been clearing my cache, and even running cron. – vintorg Aug 14 '14 at 8:27
  • Did you add 'upscale' => 1. It is not present in your code, and perhaps your source images are smaller than 210x210? – J. Reynolds Aug 14 '14 at 9:51
  • @J.Reynolds there is no upscale option for scale and crop effect. It upscales by default so that option is redundant. – Beebee Aug 14 '14 at 10:14
1

In addition to this code. Try using this

function mymodule_image_default_styles() {
  $styles = array();

  $styles['mymodule_thumb'] = array(
    'label' => 'MyModule Thumbnail',
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 200,
          'upscale' => 1,
          'height' => 200,
        ),
        'weight' => 0,
      ),
      array(
        'name' => 'image_crop',
        'data' => array(
          'width' => 210,
          'height' => 210
        ),
      )
    ),
    'name' => 'mymodule_thumb',
    'storage' => IMAGE_STORAGE_NORMAL
  );
  return $styles;
}

Hope it works!

| improve this answer | |
  • Same thing. I get the error: "Notice: Undefined index: height in image_resize_dimensions() (line 103 of /var/www/mymodule/modules/image/image.effects.inc)". I add the height param to the scale array, and it still doesn't work. – vintorg Aug 14 '14 at 5:25
  • Please, clear cache of your site. – TBI Infotech Aug 14 '14 at 7:13
  • I've been clearing my cache. – vintorg Aug 14 '14 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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