How to set image style in your own module in Drupal 7

Some time ago one of our customers asked to port gallery from Drupal 6 site to Drupal 7 one.
The module was not complex, the main difficult that I faced during the process to change Image Cache approach to the Image style one that native in Drupal 7. The main idea is to set resize rules of thumbnails in the gallery. Users should no do that themselves.

Previously in Drupal 6 there was following solution. The Image Cache preset set in the .install file.

<?php
/**
* Implementation of hook_install().
*/
function ms_gallery_install() {
 
// Create preset.
 
ms_gallery_install_imagecache_presets();
}

function
ms_gallery_install_imagecache_presets() {
 
// First, build an array of all the preset names so we do not make duplicates
  // Set the argument to TRUE to reset the cache
 
$presets = imagecache_presets(TRUE);
 
$preset_names = array();

 
// If there are any presets
 
if ($presets != '') {
    foreach (
$presets as $preset) {
     
$preset_names[] = $preset['presetname'];
    }
  }

 
// Prepare to install ImageCache presets
 
$imagecache_presets = array();
 
$imagecache_actions = array();

 
// We are checking to make sure the preset name does not exist before creating
 
if (!in_array('ms_gallery', $preset_names)) {
   
$imagecache_presets[] = array(
     
'presetname' => 'ms_gallery',
    );
   
$imagecache_actions['ms_gallery'][] = array(
     
'action' => 'imagecache_scale_and_crop',
     
'data' => array(
       
'width' => 120,
       
'height' => 67,
      ),
     
'weight' => 0,
    );
  }
 
// Need to install preset, id will be returned by function,
  // Then install action add presetid to action prior to install:
 
foreach ($imagecache_presets as $preset) {
   
$preset = imagecache_preset_save($preset);
    foreach (
$imagecache_actions[$preset['presetname']] as $action) {
     
$action['presetid'] = $preset['presetid'];
     
imagecache_action_save($action);
    }
   
drupal_set_message(t('ImageCache preset %id: %name and corresponding actions saved.', array('%id' => $preset['presetid'], '%name' => $preset['presetname'])));
  }
}
?>

Above the part of ms_gallery.install code. I would like to show how to set Image Cache preset programmatically.

In the case of Drupal 7 there is no Image Cache module. Instead it we should use Image Style approach that included in the Drupal 7 core.
I try to find any example in the Google, but the examples that I found does not work.

There is no any other option that to study Drupal.org. And after several hours I have found following beautiful HOOK:
hook_image_default_styles()

In terms of this hook to solve my task will be so easy!

<?php
function ms_gallery_image_default_styles() {
 
$styles = array();

 
$styles['carousel_gallery'] = array(
   
'effects' => array(
      array(
       
'name' => 'image_scale_and_crop',
       
'data' => array(
         
'width' => 120,
         
'height' => 67,
         
'upscale' => 1,
        ),
       
'weight' => 0,
      ),
    ),
  );

  return
$styles;
}
?>

I really like Drupal 7 :-)

Comments

It might be useful to also add other ways of displaying image styles, like
http://api.drupal.org/api/drupal/modules!image!image.module/function/ima...

The topic of this atricle was how to set image styles. I think that image display will be overviewed in the other blog post.

Hi Vasily,
I'm having a question about Drupal and I was thinking that you may be helpful (as you're a drupal developer:).
Basically, I've created a module (drupal 7) and know I wanna integrate it into my website as a button (when users click on it, a small window opens and display a certain content). So far the module I created is a very basic block.
Any idea who to do that?
Thanks,

Sofiane, I did not understand your question.
Do you mean: "Any idea how to do that?"

If my version correct, you can use colorbox module for the popup window.

Thank you very much,
You saved my time.

Is there a way in D7 to set the size attributes using percentage? I'm toying with it now and all it asks is for an integer. I think D6 could use percentages, which is what I want.

I'll never understand why Drupal can't get images right, lol.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.
Image CAPTCHA
Enter the characters shown in the image.