【问题标题】:How to override the Drupal plugin's function?如何覆盖 Drupal 插件的功能?
【发布时间】:2021-05-01 04:20:28
【问题描述】:

我是 Drupal 的新手。

我正在使用 imagemagick 插件来修改图像。我的问题是当它调整 GIF 图像的大小时,图像变得混乱了。我在互联网上搜索解决方案,我发现了相关的变化。 但我不想将代码更改直接写入插件模块文件。 我想覆盖它的 image_imagemagick_resize() 函数。我不知道如何覆盖它以及将它的覆盖功能放在哪里。

默认功能:-

function image_imagemagick_resize(stdClass $image, $width, $height) {
  $image->ops[] = '-resize ' . (int) $width . 'x' . (int) $height . '!';
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;
}

重写函数:

function imageapi_imagemagick_image_resize(&$image, $width, $height) {
  $extra = '';
  if($image->info['mime_type'] == 'image/gif') {
    $extra = '-coalesce ';
  }
  $image->ops[] = $extra . '-resize '. (int) $width .'x'. (int) $height .'!';
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;
}

谢谢

【问题讨论】:

    标签: drupal drupal-7 imagemagick


    【解决方案1】:

    希望image_imagemagick_resize 通过_image_imagemagick_alter_invoke 调用更改挂钩:

    function image_imagemagick_resize($source, $dest, $width, $height) {
      $args = array('resize' => '-resize ' . (int) $width . 'x' . (int) $height . '!');
      $args = _image_imagemagick_alter_invoke('resize', $source, $args);
      return _image_imagemagick_convert($source, $dest, $args);
    }
    
    function _image_imagemagick_alter_invoke($op, $filepath, $args) {
      foreach (module_implements('imagemagick_alter') as $module) {
        $function = $module . '_imagemagick_alter';
        $function($op, $filepath, $args);
      }
      return $args;
    }
    

    因此,您可以通过在自定义模块中实现 hook_imagemagick_alter() 来覆盖调整大小参数:

    function mymodule_imagemagick_alter($op, $filepath, &$args) {
      # altering $args...
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2022-10-14
      • 1970-01-01
      • 2015-01-06
      • 2016-01-13
      相关资源
      最近更新 更多