Index: image_resize_filter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image_resize_filter/image_resize_filter.module,v
retrieving revision 1.35
diff -u -p -r1.35 image_resize_filter.module
--- image_resize_filter.module	16 Nov 2009 04:31:35 -0000	1.35
+++ image_resize_filter.module	5 Jan 2010 20:31:27 -0000
@@ -34,6 +34,13 @@ function image_resize_filter_filter($op,
       $settings['class'] = variable_get('image_resize_filter_link_class_'. $format, '');
       $settings['rel'] = variable_get('image_resize_filter_link_rel_'. $format, '');
       $settings['image_locations'] = array_filter(variable_get('image_resize_filter_image_locations_'. $format, array('local')));
+      foreach (array('left', 'right', 'center') as $align) {
+        $settings[$align] = array(
+          'type' => variable_get('image_resize_filter_image_align_'. $align .'_'. $format, ''),
+          'imagecache' => variable_get('image_resize_filter_image_align_'. $align .'_imagecache_'. $format, ''),
+          'width' => variable_get('image_resize_filter_image_align_'. $align .'_width_'. $format, ''),
+        );
+      }
       $images = image_resize_filter_get_images($settings, $text);
       return image_resize_filter_process($images, $text, $settings);
 
@@ -157,6 +164,46 @@ function image_resize_filter_form($forma
     '#default_value' => variable_get('image_resize_filter_link_rel_'. $format, ''),
   );
 
+  $form['image_resize']['align'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Resize by alignment'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+    '#description' => t('Resize image into predefined width or imagecache preset according to alignment.'),
+  );
+
+  $align_choices = array('disabled' => 'Disabled', 'imagecache' => 'Imagecache', 'width' => 'Width');
+  $aligns = array('left', 'right', 'center');
+  $presets = array();
+  
+  foreach (imagecache_presets() as $preset) {
+    $presets[$preset['presetid']] = $preset['presetname'];
+  }
+  
+  foreach ($aligns as $align) {
+    $form['image_resize']['align']['image_resize_filter_image_align_'. $align .'_'. $format] = array(
+      '#type' => 'select',
+      '#title' => t(ucfirst($align)),
+      '#options' => $align_choices,
+      '#default_value' => variable_get('image_resize_filter_image_align_'. $align .'_'. $format, ''),
+      '#description' => t('Disable, by Imagecache preset or by maximal width'),
+    );
+  
+    $form['image_resize']['align']['image_resize_filter_image_align_'. $align .'_imagecache_'. $format] = array(
+      '#type' => 'select',
+      '#title' => t('Imagecache preset'),
+      '#options' => $presets,
+      '#default_value' => variable_get('image_resize_filter_image_align_'. $align .'_imagecache_'. $format, ''),
+    );
+
+    $form['image_resize']['align']['image_resize_filter_image_align_'. $align .'_width_'. $format] = array(
+      '#type' => 'textfield',
+      '#title' => t('Maximal width'),
+      '#size' => '5',
+      '#default_value' => variable_get('image_resize_filter_image_align_'. $align .'_width_'. $format, ''),
+    );
+  }
+
   return $form;
 }
 
@@ -267,8 +314,28 @@ function image_resize_filter_get_images(
       ${'needs_'. $property} = $needs_property;
     }
 
-    // If height and width are both missing, nothing to do here.
+    // jcisio: Find the image align if any.
+    $align = NULL;
+    $preset = NULL;
+    $align_matches = array();
+    preg_match('/align[ ]*=[ ]*"([^"]+)"/i', $img_tag, $align_matches);
+    if (isset($align_matches[1])) {
+      $align = strtolower($align_matches[1]);
+    }
+
     if (!$width && !$height) {
+      if (!$align || ($align != 'left' && $align != 'right')) $align = 'center';
+
+      if ($settings[$align]['type'] == 'imagecache') {
+        $preset = $settings[$align]['imagecache'];
+      }
+      elseif ($settings[$align]['type'] == 'width') {
+        $width = $settings[$align]['width'];
+      }
+    }
+
+    // If height and width are both missing, nothing to do here.
+    if (!$width && !$height && !$preset) {
       continue;
     }
 
@@ -327,80 +394,91 @@ function image_resize_filter_get_images(
         imagecache_build_derivative($preset['actions'], $original_path, imagecache_create_path($preset_name, $original_path));
       }
     }
-
-    // Get the image size.
-    if ($location == 'local') {
-      $image_size = @getimagesize($local_path);
+    
+    if ($preset) {
+      // imagecache preset, don't care about other things
+      $images[] = array(
+        'title' => $title,
+        'has_link' => $has_link,
+        'original' => $src,
+        'preset' => $preset,
+      );
     }
     else {
-      $image_size = @getimagesize($src);
-    }
+      // Get the image size.
+      if ($location == 'local') {
+        $image_size = @getimagesize($local_path);
+      }
+      else {
+        $image_size = @getimagesize($src);
+      }
 
-    // All this work and the image isn't even there. Bummer. Next image please.
-    if ($image_size == FALSE) {
-      continue;
-    }
+      // All this work and the image isn't even there. Bummer. Next image please.
+      if ($image_size == FALSE) {
+        continue;
+      }
 
-    $actual_width = $image_size[0];
-    $actual_height = $image_size[1];
+      $actual_width = $image_size[0];
+      $actual_height = $image_size[1];
 
-    // If either height or width is missing, calculate the other.
-    if (!$height) {
-      $ratio = $actual_height/$actual_width;
-      $height = round($ratio * $width);
-    }
-    elseif (!$width) {
-      $ratio = $actual_width/$actual_height;
-      $width = round($ratio * $height);
-    }
+      // If either height or width is missing, calculate the other.
+      if (!$height) {
+        $ratio = $actual_height/$actual_width;
+        $height = round($ratio * $width);
+      }
+      elseif (!$width) {
+        $ratio = $actual_width/$actual_height;
+        $width = round($ratio * $height);
+      }
 
-    // Finally, if width and heights match up, don't do anything.
-    if ($actual_width == $width && $actual_height == $height) {
-      continue;
-    }
+      // Finally, if width and heights match up, don't do anything.
+      if ($actual_width == $width && $actual_height == $height) {
+        continue;
+      }
 
-    // Check the image extension by name.
-    $extension_matches = array();
-    preg_match('/\.([a-zA-Z0-9]+)$/', $src, $extension_matches);
-    if (!empty($extension_matches)) {
-      $extension = strtolower($extension_matches[1]);
-    }
-    // If the name extension failed (such as an image generated by a script),
-    // See if we can determine an extension by MIME type.
-    elseif (isset($image_size['mime'])) {
-      switch ($image_size['mime']) {
-        case 'image/png':
-          $extension = 'png';
-          break;
-        case 'image/gif':
-          $extension = 'gif';
-          break;
-        case 'image/jpeg':
-        case 'image/pjpeg':
-          $extension = 'jpg';
-          break;
+      // Check the image extension by name.
+      $extension_matches = array();
+      preg_match('/\.([a-zA-Z0-9]+)$/', $src, $extension_matches);
+      if (!empty($extension_matches)) {
+        $extension = strtolower($extension_matches[1]);
+      }
+      // If the name extension failed (such as an image generated by a script),
+      // See if we can determine an extension by MIME type.
+      elseif (isset($image_size['mime'])) {
+        switch ($image_size['mime']) {
+          case 'image/png':
+            $extension = 'png';
+            break;
+          case 'image/gif':
+            $extension = 'gif';
+            break;
+          case 'image/jpeg':
+          case 'image/pjpeg':
+            $extension = 'jpg';
+            break;
+        }
       }
-    }
 
-    // If we're not certain we can resize this image, skip it.
-    if (!isset($extension) || !in_array(strtolower($extension), array('png', 'jpg', 'jpeg', 'gif'))) {
-      continue;
-    }
+      // If we're not certain we can resize this image, skip it.
+      if (!isset($extension) || !in_array(strtolower($extension), array('png', 'jpg', 'jpeg', 'gif'))) {
+        continue;
+      }
 
-    // If getting this far, the image exists and is not the right size.
-    // Add all information to a list of images that need resizing.
-    $images[] = array(
-      'expected_size' => array('width' => $width, 'height' => $height),
-      'actual_size' => array('width' => $image_size[0], 'height' => $image_size[1]),
-      'add_properties' => array('width' => $needs_width, 'height' => $needs_height),
-      'title' => $title,
-      'has_link' => $has_link,
-      'original' => $src,
-      'location' => $location,
-      'local_path' => $local_path,
-      'mime' => $image_size['mime'],
-      'extension' => $extension,
-    );
+      // If getting this far, the image exists and is not the right size.
+      // Add all information to a list of images that need resizing.
+      $images[] = array(
+        'expected_size' => array('width' => $width, 'height' => $height),
+        'actual_size' => array('width' => $image_size[0], 'height' => $image_size[1]),
+        'add_properties' => array('width' => $needs_width, 'height' => $needs_height),
+        'title' => $title,
+        'has_link' => $has_link,
+        'original' => $src,
+        'location' => $location,
+        'local_path' => $local_path,
+        'mime' => $image_size['mime'],
+        'extension' => $extension,
+      );
+    }
   }
 
   return $images;
@@ -438,7 +516,7 @@ function image_resize_filter_process($im
         continue;
       }
     }
-    else {
+    elseif (!$image['preset']) {
       $path_info = image_resize_filter_pathinfo($image['local_path']);
       $local_file_dir = preg_replace('/^'. preg_quote($file_directory_path, '/') .'\/?/', '', $path_info['dirname']);
       $local_file_dir = !empty($local_file_dir) ? $local_file_dir . '/' : '';
@@ -446,6 +524,14 @@ function image_resize_filter_process($im
       $image['destination'] = $file_directory_path .'/'. $local_file_path;
     }
 
+    if ($image['preset']) {
+      $preset = imagecache_preset($image['preset']);
+      $image['destination'] = imagecache_create_url($preset['presetname'], substr($image['original'], 1));
+      image_resize_filter_update_tag(NULL, $image, $settings);
+      $text = preg_replace_callback('/(<img[^>]*?src[ ]*=[ ]*")'. preg_quote($image['original'] ,'/') .'("[^>]*?)(\/?>)/i', 'image_resize_filter_update_tag', $text, 1);
+      continue;
+    }
+
     // Ensure that the destination directories exist.
     $folders = explode('/', dirname($local_file_path));
     $current_directory = $file_directory_path .'/';
@@ -504,7 +590,7 @@ function image_resize_filter_update_tag(
   if (strpos($image['original'], 'system/files/') !== FALSE && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
     $src = file_create_url($src);
   }
-  else {
+  elseif (! $image['preset']) {
     $src = $base_url . '/' . $src;
   }
   // Strip the http:// from the path if the original did not include it.
