<?php

function scratchpads_thumbnail_style_alter_enable(){
  // When enabling, we flush the cropped thumbnails and images.
  $styles = image_styles();
  image_style_flush($styles['square_thumbnail']);
  image_style_flush($styles['slideshow_large']);
  image_style_flush($styles['slideshow_thumbnail']);
  image_style_flush($styles['species_slideshow_thumbnail']);
  image_style_flush($styles['species_slideshow_large']);
}

function scratchpads_thumbnail_style_alter_disable(){
  // When disabling, we revert the image styles, and flush the styles.
  $styles = image_styles();
  image_default_style_revert($styles['square_thumbnail']);
  image_default_style_revert($styles['slideshow_large']);
  image_default_style_revert($styles['slideshow_thumbnail']);
  image_default_style_revert($styles['species_slideshow_thumbnail']);
  image_default_style_revert($styles['species_slideshow_large']);
  scratchpads_thumbnail_style_alter_enable();
  // We disable the imagecache_actions module and its dependents
  module_disable(array(
    'imagecache_actions',
    'imagecache_canvasactions'
  ), FALSE);
  drupal_uninstall_modules(array(
    'imagecache_actions',
    'imagecache_canvasactions'
  ), FALSE);
}

/**
 * Implements hook_image_default_styles_alter().
 */
function scratchpads_thumbnail_style_alter_image_styles_alter(&$styles){
  foreach(array(
    'square_thumbnail' => array(
      'width' => 85,
      'height' => 85
    ),
    'slideshow_large' => array(
      'width' => '619',
      'height' => '463'
    ),
    'slideshow_thumbnail' => array(
      'width' => '154',
      'height' => '115'
    ),
    'species_slideshow_thumbnail' => array(
      'width' => '92',
      'height' => '69'
    ),
    'species_slideshow_large' => array(
      'width' => '284',
      'height' => '211'
    )
  ) as $machine_name => $info){
    if($styles[$machine_name]['storage'] == IMAGE_STORAGE_DEFAULT){
      /* get the original default convert_image_format effect*/
      $convert_image_format = array_pop($styles[$machine_name]['effects']);
      /* change the weight of it */
      $convert_image_format['weight'] = '2';
      /* remove the other square_thumbnail effects */
      unset($styles[$machine_name]['effects']);
      $styles[$machine_name]['effects'] = array(
        array(
          'label' => t('Scale'),
          'help' => t('Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.'),
          'effect callback' => 'image_scale_effect',
          'dimensions callback' => 'image_scale_dimensions',
          'form callback' => 'image_scale_form',
          'summary theme' => 'image_scale_summary',
          'module' => 'image',
          'name' => 'image_scale',
          'data' => array(
            'width' => $info['width'],
            'height' => $info['height'],
            'upscale' => '1'
          ),
          'weight' => '0'
        ),
        array(
          'label' => t('Define canvas'),
          'help' => t('Define the size of the working canvas and background color, this controls the dimensions of the output image.'),
          'effect callback' => 'canvasactions_definecanvas_effect',
          'dimensions callback' => 'canvasactions_definecanvas_dimensions',
          'form callback' => 'canvasactions_definecanvas_form',
          'summary theme' => 'canvasactions_definecanvas_summary',
          'module' => 'imagecache_canvasactions',
          'name' => 'canvasactions_definecanvas',
          'data' => array(
            'RGB' => array(
              'HEX' => 'ffffff'
            ),
            'under' => 1,
            'exact' => array(
              'width' => $info['width'],
              'height' => $info['height'],
              'xpos' => 'center',
              'ypos' => 'center'
            ),
            'relative' => array(
              'leftdiff' => '0',
              'rightdiff' => '0',
              'topdiff' => '0',
              'bottomdiff' => '0'
            )
          ),
          'weight' => '1'
        ),
        $convert_image_format
      );
    }
  }
}
