代码添加默认的image style

 

/**
 * Implements hook_image_default_styles().
 *
 *这里会增加新的图片样式,
 */
function image_example_image_default_styles() {
  //这个函数返回一个数组(image style中的各个样式),
  //key:是机读名称,机读名称的定义是有规范的,为了避免机读名称命名冲突,用模块名开头,例如:‘mymodule_stylename’这种形式定义,由数字,字母和下划线组成(_和-),
  $styles = array();
  $styles['image_example_style'] = array();
  //每个style数组下包含一个effect子数组,effect数组下可以包含多个不同效果
  $styles['image_example_style']['effects'] = array(
    array(
      'name' => 'image_scale',//这个效果会在image_image_effect_info()调用 (核心代码中在 modules/image/image.effects.inc )
      'data' => array( //参数 See image_scale_effect()
        'width' => 100,
        'height' => 100,
        'upscale' => 1,
      ),
      'weight' => 0,
    ),
    // 在这个样式中添加第2个效果
    array(
      'name' => 'image_example_colorize',  //这个效果名会在hook_image_effect_info中处理
      'data' => array(              //这是参数,会传递到callback function中
        'color' => '#FFFF66',
      ),
      'weight' => 1,
    ),
  );
  $styles['image_example_test_style'] = array();
  $styles['image_example_test_style']['effects'] = array(
    array(
      'name' => 'image_scale',
      'data' => array(
        'width' =>150,
        'height' =>150,
        'upscale' => 1,
        ),
      'weight' => 1,
      ),
    );
  return $styles;
}

 保存图片样式

 

 

/**
 * Implements hook_image_style_save().
 *
 * Allows modules to respond to updates to an image style's
 * settings.
 */
function image_example_image_style_save($style) {
  if (isset($style['old_name']) && $style['old_name'] == variable_get('image_example_style_name', '')) {
    variable_set('image_example_style_name', $style['name']);
  }
}

 

/**
 * Implements hook_image_effect_info().
 *
 * 实现你添加的自定义image style的效果
 */
function image_example_image_effect_info() {
  $effects = array();
  $effects['image_example_colorize'] = array
    'label' => t('Colorize'),
    'help' => t('The colorize effect will first remove all color from the source image and then tint the image using the color specified.'),
    'effect callback' => 'image_example_colorize_effect',
    'form callback' => 'image_example_colorize_form',
    'summary theme' => 'image_example_colorize_summary', //会去调用image_example_theme()中image_example_colorize_summary,
  );
  return $effects;
}

 

/**
 * Implements hook_image_style_flush().
 */
function image_example_style_flush($style) {
  // The $style parameter is an image style array.
  cache_clear_all('*', 'image_example', TRUE);
}

 

 

 

/**
 * Implements hook_image_style_delete().
 * @see image_example_style_save()
 */
function image_example_image_style_delete($style) {
  if (isset($style['old_name']) && $style['old_name'] == variable_get('image_example_style_name', '')) {
    variable_set('image_example_style_name', $style['name']);
  }
}

 form callback function

 

function image_example_colorize_form($data) {
  $form = array();
  $form['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Color'),
    '#description' => t('The color to use when colorizing the image. Use web-style hex colors. e.g.) #FF6633.'),
    '#default_value' => isset($data['color']) ? $data['color'] : '',
    '#size' => 7,
    '#max_length' => 7,
    '#required' => TRUE,
  );
  return $form;
}

 effect callback function

function image_example_colorize_effect(&$image, $data) {
  if (!function_exists('imagefilter')) {
    watchdog('image', 'The image %image could not be colorized because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->source));
    return FALSE;
  }
  if ($image->toolkit != 'gd') {
    watchdog('image', 'Image colorize failed on %path. Using non GD toolkit.', array('%path' => $image->source), WATCHDOG_ERROR);
    return FALSE;
  }

  // Convert short #FFF syntax to full #FFFFFF syntax.
  if (strlen($data['color']) == 4) {
    $c = $data['color'];
    $data['color'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];
  }
  $data['color'] = hexdec(str_replace('#', '0x', $data['color']));
  $rgb = array();
  for ($i = 16; $i >= 0; $i -= 8) {
    $rgb[] = (($data['color'] >> $i) & 0xFF);
  }
  imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
  imagefilter($image->resource, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]);

  return TRUE;
}

 

/**
 * Implements hook_theme().
 */
function image_example_theme() {
  return array(
    'image_example_colorize_summary' => array(   //会去找theme_image_example_colorize_summary()这个函数,做相应的处理
      'variables' => array('data' => NULL),
    ),
    'image_example_image' => array(
      'variables' => array('image' => NULL, 'style' => NULL), //
      'file' => 'image_example.pages.inc',
    ),
  );
}

/**
 * Formats a summary of an image colorize effect.
 *
 * @param $variables
 *   An associative array containing:
 *   - data: The current configuration for this colorize effect.
 */
function theme_image_example_colorize_summary($variables) {
  $data = $variables['data'];
  return t('as color #@color.', array('@color' => $data['color']));
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值