Upgrading imagecache presets to Drupal 7 image styles

Sun, 12/30/2012 - 20:46 -- Sam

While doing a Drupal 6 to 7 upgrade for a web project, I was wondering if there was a way to upgrade my imagecache presets to Drupal 7 image styles. Image styles in Drupal 7 are the equivalent of Imagecache presets in Drupal 6.
I found out that you can do it via Features or finding another solution (the one posted by Drupal user Vladimir).

Here's how I migrated my Drupal 6 imagecache presets to D7 image styles.

Create file in "Drupal_ROOT" and paste the following code:

('DRUPAL_ROOT', getcwd());

DRUPAL_ROOT . '/includes/bootstrap.inc';

function imagecache_preset_actions($preset, $reset = FALSE) {
$actions_cache = array();

$result = db_query('SELECT * FROM {imagecache_action} where presetid = '.$preset['presetid'].' order by weight' );
    foreach (
$result as $row ) {
$row['data'] = unserialize($row['data']);
$actions_cache[$preset['presetid']][] = $row;

  return isset(
$actions_cache[$preset['presetid']]) ? $actions_cache[$preset['presetid']] : array();

function imagecache_presets() {
$presets = array();

$normal_presets = array();

$result = db_query('SELECT * FROM {imagecache_preset} ORDER BY presetname');
    foreach (
$result as $preset) {
$presets[$preset['presetid']] = $preset;
$presets[$preset['presetid']]['actions'] = imagecache_preset_actions($preset);
$presets[$preset['presetid']]['storage'] = 0;

// Collect normal preset names so we can skip defaults and mark overrides accordingly
$normal_presets[$preset['presetname']] = $preset['presetid'];



$presets as $preset)
$styles_cnt++;// inc styles
$preset['actions'] as $action)
$effect_ieid=FALSE;// effect not exists
foreach($style['effects'] as $effect)
$effect['name'] == $action['action'] &&
$effect['module'] == $action['module'] &&
$effect['weight'] == $action['weight'] &&
$effect['data'] == $action['data'] )
$effect_ieid=$effect['ieid'];// effect exists
$effects_ext_cnt++;// inc exists
$effects_new_cnt++;// inc new
$effect['isid'] = $style['isid'];  
$effect['name'] = $action['action'];
$effect['module'] == $action['module'];
$effect['weight'] = $action['weight'];
$effect['data'] = $action['data'];
$effect = image_effect_save($effect);
$style['effects'][$effect['ieid']] = $effect;

"Styles: $styles_cnt,    Effects new: $effects_new_cnt, Effects exists: $effects_ext_cnt \n";

Open the file and you're done!

Happy Drupalling! :)


Tobias's picture

Hey... just wanted to thank you for this code snippet... I used it in some projects I had to upgrade to Drupal 7 and it saved me days!!!!

Sam's picture

You are welcome, Tobias. I am glad to have been able to help. Have a great day!

Siby's picture

Hello would you mind letting me know which hosting company you're working with? I've loaded your blog in 3 completely different browsers and I must say this blog loads a lot faster then most. Can you recommend a good hosting provider at a honest price? Cheers, I appreciate it!

Haris's picture

Wonderful blog! I found it while surfing around on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem to get there!

Sienn's picture

I think that what you posted made a ton of sense. But, what about this?
what if you added a little content? I ain't saying your content is not solid., but suppose you added a title to maybe grab people's
attention? I mean Upgrading imagecache presets to Drupal 7 image styles | Samira Elhaddad is kinda vanilla.
You should look at Yahoo's front page and see how they create article headlines to
get viewers interested. You might add a video or a picture or two to grab people excited about what you've written.
Just my opinion, it could bring your website a little livelier.

Add new comment


@azridesign on