Index: /extensions/external_ImageMagick/branch/2.1/functions.inc.php
===================================================================
--- /extensions/external_ImageMagick/branch/2.1/functions.inc.php	(revision 9857)
+++ /extensions/external_ImageMagick/branch/2.1/functions.inc.php	(revision 9857)
@@ -0,0 +1,193 @@
+<?php
+
+if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
+
+function image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
+{
+  return pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, false);
+}
+
+function thumb_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
+{
+  return pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, true);
+}
+
+function pwg_image_resize_ext_im($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $thumb)
+{
+  global $conf;
+
+  if ($result !== false)
+  {
+    //someone hooked us - so we skip
+    return $result;
+  }
+
+  // extension of the picture filename
+  $extension = strtolower(get_extension($source_filepath));
+  if (!in_array($extension, array('jpg', 'jpeg', 'png')))
+  {
+    die('[Imagick] unsupported file extension');
+  }
+
+  $image = new Imagick($source_filepath);
+
+  $rotation = get_rotation_angle($source_filepath);
+  
+  // width/height
+  $source_width  = $image->getImageWidth();
+  $source_height = $image->getImageHeight();
+
+  if ($thumb and isset($conf['upload_form_thumb_square']) and $conf['upload_form_thumb_square'])
+  {
+    $coord = process_ratio($source_width, $source_height, $rotation);
+    $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']);
+  }
+  
+  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
+
+  // testing on height is useless in theory: if width is unchanged, there
+  // should be no resize, because width/height ratio is not modified.
+  if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height)
+  {
+    // the image doesn't need any resize! We just copy it to the destination
+    copy($source_filepath, $destination_filepath);
+    return true;
+  }
+
+  $image->setImageCompressionQuality($quality);
+  $image->setInterlaceScheme(Imagick::INTERLACE_LINE);
+  
+  $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9);
+
+  if (isset($rotation))
+  {
+    $image->rotateImage(new ImagickPixel(), -$rotation);
+    $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
+  }
+
+  $image->writeImage($destination_filepath);
+  $image->destroy();
+
+  // everything should be OK if we are here!
+  return true;
+}
+
+function get_rotation_angle($source_filepath)
+{
+  global $conf;
+
+  if (!function_exists('exif_read_data'))
+  {
+    return null;
+  }
+
+  $rotation = null;
+  
+  $exif = exif_read_data($source_filepath);
+  
+  if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches))
+  {
+    $orientation = $matches[1];
+    if (in_array($orientation, array(3, 4)))
+    {
+      $rotation = 180;
+    }
+    elseif (in_array($orientation, array(5, 6)))
+    {
+      $rotation = 270;
+    }
+    elseif (in_array($orientation, array(7, 8)))
+    {
+      $rotation = 90;
+    }
+  }
+
+  return $rotation;
+}
+
+function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null)
+{
+  $rotate_for_dimensions = false;
+  if (isset($rotation) and in_array(abs($rotation), array(90, 270)))
+  {
+    $rotate_for_dimensions = true;
+  }
+
+  if ($rotate_for_dimensions)
+  {
+    list($width, $height) = array($height, $width);
+  }
+  
+  $ratio_width  = $width / $max_width;
+  $ratio_height = $height / $max_height;
+  
+  // maximal size exceeded ?
+  if ($ratio_width > 1 or $ratio_height > 1)
+  {
+    if ($ratio_width < $ratio_height)
+    { 
+      $destination_width = ceil($width / $ratio_height);
+      $destination_height = $max_height;
+    }
+    else
+    { 
+      $destination_width = $max_width; 
+      $destination_height = ceil($height / $ratio_width);
+    }
+  }
+
+  if ($rotate_for_dimensions)
+  {
+    list($destination_width, $destination_height) = array($destination_height, $destination_width);
+  }
+  
+  return array(
+    'width' => $destination_width,
+    'height'=> $destination_height,
+    );
+}
+
+function process_ratio(&$source_width, &$source_height, $rotation=null)
+{
+  global $conf;
+
+  if (!isset($conf['thumbnails_ratio']) or !preg_match('/^\d+:\d+$/', $conf['thumbnails_ratio']))
+    $conf['thumbnails_ratio'] = '1:1';
+
+  if (!isset($conf['thumbnails_ratio_orientation']))
+    $conf['thumbnails_ratio_orientation'] = true;
+
+  if (!$conf['thumbnails_ratio_orientation'] and $rotation)
+    $conf['thumbnails_ratio_orientation'] = true;
+
+  $x = 0;
+  $y = 0;
+
+  if ($source_width >= $source_height or !$conf['thumbnails_ratio_orientation'])
+    list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']);
+  else
+    list($heightRatio, $widthRatio) = explode(':', $conf['thumbnails_ratio']);
+
+  $img_ratio = $source_width / $source_height;
+  $dest_ratio = $widthRatio / $heightRatio;
+
+  if($dest_ratio > $img_ratio)
+  {
+    $destHeight = ceil(($source_width * $heightRatio) / $widthRatio);
+    $y = ceil(($source_height - $destHeight) / 2 );
+    $source_height = $destHeight ;
+  }
+  elseif ($dest_ratio < $img_ratio)
+  {
+    $destWidth = ceil(($source_height * $widthRatio) / $heightRatio);
+    $x = ceil(($source_width - $destWidth) / 2 );
+    $source_width = $destWidth ;
+  }
+
+  return array('x' => $x, 'y' => $y);
+}
+
+add_event_handler('upload_image_resize', 'image_resize_ext_im', 30, 6);
+add_event_handler('upload_thumbnail_resize', 'thumb_resize_ext_im', 30, 6);
+
+?>
Index: /extensions/external_ImageMagick/branch/2.1/imagick.class.php
===================================================================
--- /extensions/external_ImageMagick/branch/2.1/imagick.class.php	(revision 9857)
+++ /extensions/external_ImageMagick/branch/2.1/imagick.class.php	(revision 9857)
@@ -0,0 +1,158 @@
+<?php
+
+class Imagick
+{
+  var $source_path = '';
+  var $tmp_path = '';
+  var $image_data = array();
+  var $imagemagickdir = '';
+  var $commands = array();
+
+  const INTERLACE_NO = 'none';
+  const INTERLACE_LINE = 'line';
+  const INTERLACE_PLANE = 'plane';
+  const INTERLACE_PARTITION = 'partition';
+  const INTERLACE_GIF = 'GIF';
+  const INTERLACE_JPEG = 'JPEG';
+  const INTERLACE_PNG = 'PNG';
+
+  const FILTER_POINT = 'Point';
+  const FILTER_BOX = 'Box';
+  const FILTER_TRIANGLE = 'Triangle';
+  const FILTER_HERMITE = 'Hermite';
+  const FILTER_HANNING = 'Hanning';
+  const FILTER_HAMMING = 'Hamming';
+  const FILTER_BLACKMAN = 'Blackman';
+  const FILTER_GAUSSIAN = 'Gaussian';
+  const FILTER_QUADRATIC = 'Quadratic';
+  const FILTER_CUBIC = 'Cubic';
+  const FILTER_CATROM = 'Catrom';
+  const FILTER_MITCHELL = 'Mitchell';
+  const FILTER_LANCZOS = 'Lanczos';
+  const FILTER_BESSEL = 'Bessel';
+  const FILTER_SINC = 'Sinc';
+
+  const ORIENTATION_TOPLEFT = 'top-left';
+  const ORIENTATION_TOPRIGHT = 'top-right';
+  const ORIENTATION_BOTTOMRIGHT = 'bottom-right';
+  const ORIENTATION_BOTTOMLEFT = 'bottom-left';
+  const ORIENTATION_LEFTTOP = 'left-top';
+  const ORIENTATION_RIGHTTOP = 'right-top';
+  const ORIENTATION_RIGHTBOTTOM = 'right-bottom';
+  const ORIENTATION_LEFTBOTTOM = 'left-bottom';
+
+  function __construct($source)
+  {
+    global $conf;
+
+    $this->source_path = $source;
+
+    if (isset($conf['imagick_dir']))
+      $this->imagemagickdir = rtrim($conf['imagick_dir'], ' /\\').(PHP_OS == 'WINNT' ? '\\' : '/');
+
+    $this->tmp_path = @tempnam($conf['local_data_dir'], get_extension($this->source_path));
+
+    $command = $this->imagemagickdir."identify -verbose ".realpath($this->source_path);
+    @exec($command, $returnarray, $returnvalue);
+    if($returnvalue)
+    {
+      die("ImageMagick: Corrupt image");
+    }
+    else
+    {
+      foreach($returnarray as $value)
+      {
+        $arr = explode(':', $value, 2);
+        if (count($arr) == 2)
+        {
+          $this->image_data[trim($arr[0])] = trim($arr[1]);
+        }
+      }
+    }
+  }
+
+  function add_command($command, $params=null)
+  {
+    $this->commands[$command] = $params;
+  }
+
+  function getImageWidth()
+  {
+    preg_match('#^(\d+)x#', $this->image_data['Geometry'], $match);
+    return isset($match[1]) ? $match[1] : false;
+  }
+
+  function getImageHeight()
+  {
+    preg_match('#^\d+x(\d+)(?:\+|$)#', $this->image_data['Geometry'], $match);
+    return isset($match[1]) ? $match[1] : false;
+  }
+
+  function setImageCompressionQuality($quality)
+  {
+    $this->add_command('quality', $quality);
+  }
+
+  function setInterlaceScheme($interlace)
+  {
+    $this->add_command('interlace', $interlace);
+  }
+
+  function resizeImage($width, $height, $filter, $blur)
+  {
+    $this->add_command('resize', $width.'x'.$height.'!');
+    $this->add_command('filter', $filter);
+    //$this->add_command('blur', $blur);
+  }
+
+  function stripImage()
+  {
+    $this->add_command('strip');
+  }
+
+  function rotateImage($background, $rotate)
+  {
+    $this->add_command('rotate', $rotate);
+  }
+
+  function setImageOrientation($orientation)
+  {
+    $this->add_command('orient', $orientation);
+  }
+
+  function cropImage($width, $height, $x, $y)
+  {
+    $this->add_command('crop', $width.'x'.$height.'+'.$x.'+'.$y);
+  }
+
+  function writeImage($destination_filepath)
+  {
+    global $conf;
+
+    $exec = $this->imagemagickdir.'convert';
+
+    foreach ($this->commands as $command => $params)
+    {
+      $exec .= ' -'.$command;
+      if (!empty($params))
+      {
+        $exec .= ' '.$params;
+      }
+    }
+
+    $exec .= ' '.realpath($this->source_path);
+    $exec .= ' '.realpath($this->tmp_path);
+    @exec($exec, $returnarray, $returnvalue);
+    @rename($this->tmp_path, $destination_filepath);
+  }
+
+  function destroy()
+  {
+    @unlink($this->tmp_path);
+    return true;
+  }
+}
+
+class ImagickPixel { }
+
+?>
Index: /extensions/external_ImageMagick/branch/2.1/index.php
===================================================================
--- /extensions/external_ImageMagick/branch/2.1/index.php	(revision 9857)
+++ /extensions/external_ImageMagick/branch/2.1/index.php	(revision 9857)
@@ -0,0 +1,33 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery                           |
+// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | file          : $Id: index.php 1912 2007-03-16 06:30:07Z rub $
+// | last update   : $Date: 2007-03-16 07:30:07 +0100 (ven, 16 mar 2007) $
+// | last modifier : $Author: rub $
+// | revision      : $Revision: 1912 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+// Recursive call
+$url = '../';
+header( 'Request-URI: '.$url );
+header( 'Content-Location: '.$url );
+header( 'Location: '.$url );
+exit();
+?>
Index: /extensions/external_ImageMagick/branch/2.1/main.inc.php
===================================================================
--- /extensions/external_ImageMagick/branch/2.1/main.inc.php	(revision 9857)
+++ /extensions/external_ImageMagick/branch/2.1/main.inc.php	(revision 9857)
@@ -0,0 +1,53 @@
+<?php
+/*
+Plugin Name: External ImageMagick
+Version: auto
+Description: Use external ImageMagick installation
+Plugin URI: auto
+Author: P@t
+Author URI: http://www.gauchon.com
+*/
+
+if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
+
+define('IMAGICK_EXEC_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
+
+add_event_handler('get_admin_plugin_menu_links', 'imagick_exec_init');
+add_event_handler('ws_add_methods', 'ws_add_method_load_imagick_exec_functions');
+
+function imagick_exec_init($menu)
+{
+  global $conf, $page;
+
+  if (in_array($page['page'], array('intro','plugins_list','photos_add')) and !test_imagick_exec())
+  {
+    array_push($page['warnings'], l10n('Unable to locate ImageMagick directory. Please fill $conf[\'imagick_directory\'] in your local configuration file.'));
+  }
+  elseif (($page['page'] == 'photos_add' or script_basename() == 'uploadify') and test_imagick_exec())
+  {
+    include_once(IMAGICK_EXEC_PATH.'functions.inc.php');
+    include_once(IMAGICK_EXEC_PATH.'imagick.class.php');
+  }
+
+  return $menu;
+}
+
+function ws_add_method_load_imagick_exec_functions()
+{
+  if (test_imagick_exec())
+  {
+    include_once(IMAGICK_EXEC_PATH.'functions.inc.php');
+    include_once(IMAGICK_EXEC_PATH.'imagick.class.php');
+  }
+}
+
+function test_imagick_exec()
+{
+  global $conf;
+
+  $imagick_dir = isset($conf['imagick_dir']) ? rtrim($conf['imagick_dir'], ' /\\').(PHP_OS == 'WINNT' ? '\\' : '/') : '';
+  @exec($imagick_dir.'convert', $returnarray, $returnvalue);
+  return (!$returnvalue and !empty($returnarray[0]) and preg_match('/ImageMagick/', $returnarray[0]));
+}
+
+?>
Index: /extensions/external_ImageMagick/branch/2.1/maintain.inc.php
===================================================================
--- /extensions/external_ImageMagick/branch/2.1/maintain.inc.php	(revision 9857)
+++ /extensions/external_ImageMagick/branch/2.1/maintain.inc.php	(revision 9857)
@@ -0,0 +1,9 @@
+<?php
+
+function plugin_activate($plugin_id, $plugin_version, &$errors)
+{
+  if (!function_exists('exec'))
+    array_push($errors, l10n('Function "exec" is not available. This plugin can not be activated.'));
+}
+
+?>
