Search code examples
drupaldrupal-7imagemagick

How to override the Drupal plugin's function?


I am new in Drupal.

I am using imagemagick plugin for modify images. My problem is when it resize GIF image the image got messed up. I search the solution on internet i found the changes regarding that. but i don't want to write code changes directly to plugins module file. I want to override it's image_imagemagick_resize() function. I can not figure out how to Override it and where to put it's override function.

Default Function :-

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;
}

Override Function:

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;
}

Thanks


Solution

  • Fortunately, image_imagemagick_resize invokes alter hooks via _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;
    }
    

    So you can override the resize parameters by implementing the hook_imagemagick_alter() in a custom module :

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