You are here

image_resize_filter.module in Image Resize Filter 6

Same filename and directory in other branches
  1. 8 image_resize_filter.module
  2. 7 image_resize_filter.module

image_resize_filter.module

After adding to an input format, this filter will parse the contents of submitted content and automatically scale image files to match the set dimensions of img tags.

Image that have been created take on the ownership of the original file. Making so when the primary node is deleted, the images it provided are deleted also.

File

image_resize_filter.module
View source
<?php

/**
 * @file image_resize_filter.module
 *
 * After adding to an input format, this filter will parse the contents of
 * submitted content and automatically scale image files to match the set
 * dimensions of img tags.
 *
 * Image that have been created take on the ownership of the original file.
 * Making so when the primary node is deleted, the images it provided are
 * deleted also.
 */

/**
 * Implementation of hook_help().
 */
function image_resize_filter_help($path, $arg) {
  switch ($path) {
    case 'admin/help#image_resize_filter':
      $instructions = array(
        t('Visit the <a href="!url">Input formats</a> configuration page. Click "configure" next to the text format you want to enable the image resize filter on.', array(
          '!url' => 'admin/settings/filters',
        )),
        t('Check the box for "Image resize filter" under the list of filters.'),
        t('<strong>Important</strong>: Click the "Rearranage" tab to check the order of the filters.') . '<p>' . t('If using the Image Resize Filter on the "Filtered HTML" text format, you <strong>must</strong> ensure A) that the &lt;img&gt; tag is in the list of allowed tags and B) The "Image resize filter" is run <strong>before</strong> the "HTML filter".') . '</p>' . '<p>' . t('If using the Image Resize Filter with BBCode or some other non-HTML filter, the "Image resize filter" must be run AFTER the BBCode filter.') . '</p>',
        t('Optional. Click "configure" next to the input format the image resize filter has been enabled on, then click the "Configure" tab to set additional configuration for the image resize filter.'),
      );
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The image resize filter module provides a <a href="!url">filter</a> that makes it easy to resize images, especially when combined with a WYSIWYG editor such as tinyMCE or FCKeditor. Users never have to worry about scaling image sizes again, just insert an image and set it\'s height and width properties in HTML and the image is resized on output. To use the image resize filter, follow these steps.', array(
        '!url' => url('admin/help/filter'),
      )) . '</p>';
      $output .= theme('item_list', $instructions, t('Installation and use'), 'ol');
      return $output;
  }
}

/**
 * Implementation of hook_filter().
 */
function image_resize_filter_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Image resize filter'),
      );
    case 'description':
      return t('Resizes images to the exact dimensions specified in the &lt;img&gt; tag.');
    case 'settings':
      return image_resize_filter_form($format);
    case 'process':
      $settings['link'] = variable_get('image_resize_filter_link_' . $format, 0);
      $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',
      )));
      $images = image_resize_filter_get_images($settings, $text);
      return $images ? image_resize_filter_process_images($images, $text, $settings) : $text;
    default:
      return $text;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function image_resize_filter_nodeapi(&$node, $op, $teaser, $page) {
  if (($op == 'presave' || $op == 'delete') && isset($node->files)) {

    // Delete upload.module derivatives.
    foreach ($node->files as $fid => $file) {

      // File is an an object on delete, but array on presave.
      $file = (array) $file;
      if ($file['remove'] || $op == 'delete') {
        image_resize_filter_delete_derivatives($file['filepath']);
      }
    }
  }
}

/**
 * Implementation of hook_file_delete().
 */
function image_resize_filter_file_delete($file) {
  if (isset($file->filepath)) {
    image_resize_filter_delete_derivatives($file->filepath);
  }
}

/**
 * Implementation of hook_theme().
 */
function image_resize_filter_theme() {
  return array(
    'image_resize_filter_image' => array(
      'arguments' => array(
        'image' => NULL,
        'settings' => NULL,
      ),
    ),
    'image_resize_filter_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_file_download().
 */
function image_resize_filter_file_download($filepath) {

  // If this is a resized image, use the same access as the original image.
  $matches = array();
  if (preg_match('/^resize\\/(.*)?-\\d+x\\d+(\\.png|\\.jpg|\\.jpeg|\\.gif)$/i', $filepath, $matches)) {
    $headers = module_invoke_all('file_download', $matches[1] . $matches[2]);
    if (in_array(-1, $headers)) {
      return -1;
    }
    if (count($headers)) {
      return array(
        'Content-Type: ' . file_get_mimetype($filepath),
        'Content-Length: ' . filesize(file_create_path($filepath)),
      );
    }
  }
}

/**
 * Implementation of hook_form_[form_id]_alter().
 */
function image_resize_filter_form_system_file_system_settings_alter($form, $form_state) {
  $form['#submit'][] = 'image_resize_filter_file_system_settings_submit';
}

/**
 * Additional #submit handler for the system_file_system_settings form.
 */
function image_resize_filter_file_system_settings_submit($form, $form_state) {

  // Clear filter caches when changing file system information.
  cache_clear_all('*', 'cache_filter');
}

/**
 * The form for configuring the Image Resize Filter.
 */
function image_resize_filter_form($format) {
  $form = array();
  $form['image_resize'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image resize settings'),
    '#collapsible' => TRUE,
    '#description' => t('The image resize filter will analyze &lt;img&gt; tags and compare the given height and width attributes to the actual file. If the file dimensions are different than those given in the &lt;img&gt; tag, the image will be copied and the src attribute will be updated to point to the resized image.'),
    '#theme' => 'image_resize_filter_form',
    '#format' => $format,
  );
  $form['image_resize']['image_resize_filter_image_locations_' . $format] = array(
    '#type' => 'checkboxes',
    '#title' => t('Resize images stored'),
    '#options' => array(
      'local' => t('Locally'),
      'remote' => t('On remote servers (note: this copies <em>all</em> remote images locally)'),
    ),
    '#default_value' => variable_get('image_resize_filter_image_locations_' . $format, array(
      'local',
    )),
    '#description' => t('This option will determine which images will be analyzed for &lt;img&gt; tag differences. Enabling resizing of remote images can have performance impacts, as all images in the filtered text needs to be transfered via HTTP each time the filter cache is cleared.'),
  );
  $form['image_resize']['image_resize_filter_link_' . $format] = array(
    '#type' => 'checkbox',
    '#title' => t('If resized, add a link to the original image.'),
    '#default_value' => variable_get('image_resize_filter_link_' . $format, 0),
  );
  $form['image_resize']['image_resize_filter_link_class_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('Optionally, give it the class'),
    '#size' => '10',
    '#default_value' => variable_get('image_resize_filter_link_class_' . $format, ''),
  );
  $form['image_resize']['image_resize_filter_link_rel_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('and/or a rel attribute'),
    '#size' => '10',
    '#default_value' => variable_get('image_resize_filter_link_rel_' . $format, ''),
  );
  return $form;
}

/**
 * Theme callback to theme the Image Resize Filter form.
 */
function theme_image_resize_filter_form($form) {
  $format = $form['#format'];
  $link = 'image_resize_filter_link_' . $format;
  $class = 'image_resize_filter_link_class_' . $format;
  $rel = 'image_resize_filter_link_rel_' . $format;
  $class_element = ' ';
  $class_element .= '<span class="image-resize-filter-class">';
  $class_element .= check_plain($form[$class]['#title']) . ': ';
  $form[$class]['#title'] = NULL;
  $class_element .= drupal_render($form[$class]);
  $class_element .= '</span>';
  $rel_element = ' ';
  $rel_element .= '<span class="image-resize-filter-rel">';
  $rel_element .= check_plain($form[$rel]['#title']) . ': ';
  $form[$rel]['#title'] = NULL;
  $rel_element .= drupal_render($form[$rel]);
  $rel_element .= '</span>';
  $link_element = drupal_render($form[$link]);
  $output = '';
  $output .= '<div class="container-inline image-resize-filter-link-options">';
  $output .= $link_element;
  $output .= $class_element;
  $output .= $rel_element;
  $output .= '</div>';
  $placeholder = array(
    '#type' => 'element',
    '#title' => t('Link to the original'),
    '#children' => $output,
    '#description' => t('Linking to the original can be helpful to give users a full-size view of the image. Adding the class "thickbox" is helpful if you have installed the <a href="http://drupal.org/project/thickbox">thickbox module</a>. The rel attribute may be useful when used with the <a href="http://drupal.org/project/lightbox2">lightbox2</a> and <a href="http://drupal.org/project/shadowbox">shadowbox</a> modules.'),
  );

  // Add a little bit of JavaScript. Not cached since it's only used here.
  drupal_add_js(drupal_get_path('module', 'image_resize_filter') . '/image_resize_filter.js', 'module', 'header', FALSE, FALSE);
  return drupal_render($form) . theme('form_element', $placeholder, $output);
}

/**
 * Parsing function to locate all images in a piece of text that need replacing.
 *
 * @param $settings
 *   An array of settings that will be used to identify which images need
 *   updating. Includes the following:
 *
 *   - image_locations: An array of acceptable image locations. May contain any
 *     of the following values: "remote". Remote image will be downloaded and
 *     saved locally. This procedure is intensive as the images need to
 *     be retrieved to have their dimensions checked.
 *
 * @param $text
 *   The text to be updated with the new img src tags.
 */
function image_resize_filter_get_images($settings, $text) {
  $images = array();

  // Find all image tags, ensuring that they have a src.
  $matches = array();
  preg_match_all('/((<a [^>]*>)[ ]*)?(<img[^>]*?src[ ]*=[ ]*"([^"]+)"[^>]*>)/i', $text, $matches);

  // Loop through matches and find if replacements are necessary.
  // $matches[0]: All complete image tags and preceeding anchors.
  // $matches[1]: The anchor tag of each match (if any).
  // $matches[2]: The anchor tag and trailing whitespace of each match (if any).
  // $matches[3]: The complete img tag.
  // $matches[4]: The src value of each match.
  foreach ($matches[0] as $key => $match) {
    $has_link = (bool) $matches[1][$key];
    $img_tag = $matches[3][$key];
    $src = $matches[4][$key];
    $resize = NULL;
    $image_size = NULL;
    $attributes = array();

    // Find attributes of this image tag.
    $attribute_matches = array();
    preg_match_all('/([a-z]+)[ ]*=[ ]*"([^"]*)"/i', $img_tag, $attribute_matches);
    foreach ($attribute_matches[0] as $key => $match) {
      $attribute = $attribute_matches[1][$key];
      $attribute_value = $attribute_matches[2][$key];
      $attributes[$attribute] = $attribute_value;
    }

    // Height and width need to be matched specifically because they may come as
    // either an HTML attribute or as part of a style attribute. FCKeditor
    // specifically has a habit of using style tags instead of height and width.
    foreach (array(
      'width',
      'height',
    ) as $property) {
      $property_matches = array();
      preg_match_all('/[ \'";]' . $property . '[ ]*([=:])[ ]*"?([0-9]+)(%?)"?/i', $img_tag, $property_matches);

      // If this image uses percentage width or height, do not process it.
      if (in_array('%', $property_matches[3])) {
        $resize = FALSE;
        break;
      }

      // In the odd scenario there is both a style="width: xx" and a width="xx"
      // tag, base our calculations off the style tag, since that's what the
      // browser will display.
      $property_key = 0;
      $property_count = count($property_matches[1]);
      if ($property_count) {
        $property_key = array_search(':', $property_matches[1]);
      }
      $attributes[$property] = !empty($property_matches[2][$property_key]) ? $property_matches[2][$property_key] : '';
    }

    // Determine if this is a local or remote file.
    $base_path = base_path();
    $location = 'unknown';
    if (strpos($src, $base_path) === 0) {
      $location = 'local';
    }
    elseif (preg_match('/http[s]?:\\/\\/' . preg_quote($_SERVER['HTTP_HOST'] . $base_path, '/') . '/', $src)) {
      $location = 'local';
    }
    elseif (strpos($src, 'http') === 0) {
      $location = 'remote';
    }

    // If not resizing images in this location, continue on to the next image.
    if (!in_array($location, $settings['image_locations'])) {
      continue;
    }

    // Convert the URL to a local path.
    $local_path = NULL;
    if ($location == 'local') {

      // Remove the http:// and base path.
      $local_path = preg_replace('/(http[s]?:\\/\\/' . preg_quote($_SERVER['HTTP_HOST'], '/') . ')?' . preg_quote(base_path(), '/') . '/', '', $src, 1);

      // Build a list of acceptable language prefixes.
      $lang_codes = '';
      if (in_array(variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE), array(
        LANGUAGE_NEGOTIATION_PATH,
        LANGUAGE_NEGOTIATION_PATH_DEFAULT,
      ))) {
        $languages = language_list();
        $lang_codes = array();
        foreach ($languages as $key => $language) {
          if ($language->prefix) {
            $lang_codes[$key] = preg_quote($language->prefix, '!');
          }
        }
        $lang_codes = $lang_codes ? '((' . implode('|', $lang_codes) . ')/)?' : '';
      }

      // Convert to a file system path if using private files.
      if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && preg_match('!^(\\?q\\=)?' . $lang_codes . 'system/files/!', $local_path)) {
        $local_path = file_directory_path() . '/' . preg_replace('!^(\\?q\\=)?' . $lang_codes . 'system/files/!', '', $local_path);
      }
      $local_path = rawurldecode($local_path);
    }

    // If this is an ImageCache preset, generate the source image if necessary.
    if ($location == 'local' && !file_exists($local_path) && strpos($local_path, 'imagecache') !== FALSE && function_exists('imagecache_build_derivative')) {
      $imagecache_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '\\/imagecache\\//', '', $local_path);
      $imagecache_args = explode('/', $imagecache_path);
      $preset_name = array_shift($imagecache_args);
      $original_path = implode('/', $imagecache_args);

      // ImageCache can resize images both inside and outside of the files
      // directory. If the image doesn't exist at the given path, check inside
      // the files directory.
      if (!file_exists($original_path)) {
        $original_path = file_directory_path() . '/' . $original_path;
      }
      if (file_exists($original_path) && ($preset = imagecache_preset_by_name($preset_name))) {
        imagecache_build_derivative($preset['actions'], $original_path, imagecache_create_path($preset_name, $original_path));
      }
    }

    // If this is a remote image, retreive it to check its size.
    if ($location == 'remote') {
      $result = drupal_http_request($src);
      if ($result->code == 200) {
        $tmp_file = tempnam(file_directory_temp(), 'image_resize_filter_');
        $handle = fopen($tmp_file, 'w');
        fwrite($handle, $result->data);
        fclose($handle);
        $local_path = $tmp_file;
      }
    }

    // Get the image size.
    if (is_file($local_path)) {
      $image_size = @getimagesize($local_path);
    }

    // All this work and the image isn't even there. Bummer. Next image please.
    if (empty($image_size)) {
      image_resize_filter_delete_temp_file($location, $local_path);
      continue;
    }
    $actual_width = (int) $image_size[0];
    $actual_height = (int) $image_size[1];

    // If either height or width is missing, calculate the other.
    if (!$attributes['width'] && !$attributes['height']) {
      $attributes['width'] = $actual_width;
      $attributes['height'] = $actual_height;
    }
    if (!$attributes['height'] && is_numeric($attributes['width'])) {
      $ratio = $actual_height / $actual_width;
      $attributes['height'] = (int) round($ratio * $attributes['width']);
    }
    elseif (!$attributes['width'] && is_numeric($attributes['height'])) {
      $ratio = $actual_width / $actual_height;
      $attributes['width'] = (int) round($ratio * $attributes['height']);
    }

    // Determine if this image requires a resize.
    if (!isset($resize)) {
      $resize = $actual_width != $attributes['width'] || $actual_height != $attributes['height'];
    }

    // Skip processing if the image is a remote tracking image.
    if ($location == 'remote' && $actual_width == 1 && $actual_height == 1) {
      image_resize_filter_delete_temp_file($location, $local_path);
      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]);
    }
    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',
    ))) {
      image_resize_filter_delete_temp_file($location, $local_path);
      continue;
    }

    // If getting this far, the image exists and is not the right size, needs
    // to be saved locally from a remote server, or needs attributes added.
    // Add all information to a list of images that need resizing.
    $images[] = array(
      'expected_size' => array(
        'width' => $attributes['width'],
        'height' => $attributes['height'],
      ),
      'actual_size' => array(
        'width' => $image_size[0],
        'height' => $image_size[1],
      ),
      'attributes' => $attributes,
      'resize' => $resize,
      'img_tag' => $img_tag,
      'has_link' => $has_link,
      'original' => $src,
      'location' => $location,
      'local_path' => $local_path,
      'mime' => $image_size['mime'],
      'extension' => $extension,
    );
  }
  return $images;
}

/**
 * Processing function for image resize filter. Replace img src properties
 * with a URL to a resized image.
 *
 * @param $images
 *   An array of image information, detailing images that need to be replaced.
 * @param $text
 *   The original text of the post that needs src tags updated.
 * @param $settings
 *   An array of setting for generating the image tag.
 */
function image_resize_filter_process_images($images, $text, $settings) {
  $file_directory_path = file_directory_path();
  $search = array();
  $replace = array();
  foreach ($images as $image) {

    // Copy remote images locally.
    if ($image['location'] == 'remote') {
      $local_file_path = 'resize/remote/' . md5(file_get_contents($image['local_path'])) . '-' . $image['expected_size']['width'] . 'x' . $image['expected_size']['height'] . '.' . $image['extension'];
      $image['destination'] = $file_directory_path . '/' . $local_file_path;
    }
    elseif (!$image['resize']) {
      $local_file_path = '';
      $image['destination'] = $image['local_path'];
    }
    else {
      $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 . '/' : '';
      $local_file_path = 'resize/' . $local_file_dir . $path_info['filename'] . '-' . $image['expected_size']['width'] . 'x' . $image['expected_size']['height'] . '.' . $path_info['extension'];
      $image['destination'] = $file_directory_path . '/' . $local_file_path;
    }
    if (!file_exists($image['destination'])) {

      // Ensure that the destination directories exist.
      $folders = explode('/', dirname($local_file_path));
      $current_directory = $file_directory_path . '/';
      foreach ($folders as $folder) {
        $current_directory .= $folder . '/';
        $check_directory = $current_directory;

        // Use the "quiet" version of file_check_directory() provided by FileField
        // if it exists. This suppresses "Directory was created" messages.
        $file_check_directory = function_exists('field_file_check_directory') ? 'field_file_check_directory' : 'file_check_directory';
        $file_check_directory($check_directory, FILE_CREATE_DIRECTORY);
      }

      // Move remote images into place if they are already the right size.
      if ($image['location'] == 'remote' && !$image['resize']) {
        $handle = fopen($image['destination'], 'w');
        fwrite($handle, file_get_contents($image['local_path']));
        fclose($handle);
      }
      elseif ($image['resize']) {
        if (module_exists('imageapi') && imageapi_default_toolkit()) {
          $res = imageapi_image_open($image['local_path']);
          imageapi_image_resize($res, $image['expected_size']['width'], $image['expected_size']['height']);
          imageapi_image_close($res, $image['destination']);
        }
        else {
          image_resize($image['local_path'], $image['destination'], $image['expected_size']['width'], $image['expected_size']['height']);
        }
      }
      @chmod($image['destination'], 0664);
    }

    // Delete our temporary file if this is a remote image.
    image_resize_filter_delete_temp_file($image['location'], $image['local_path']);

    // Replace the existing image source with the resized image.
    // Set the image we're currently updating in the callback function.
    $search[] = $image['img_tag'];
    $replace[] = image_resize_filter_image_tag($image, $settings);
  }
  return str_replace($search, $replace, $text);
}

/**
 * Generate a themed image tag based on an image array.
 *
 * @param $image
 *   An array containing image information and properties.
 * @param $settings
 *   Settings for the input filter.
 */
function image_resize_filter_image_tag($image = NULL, $settings = NULL) {
  global $base_url;
  $src = $image['destination'];

  // Make the new file private if the original was private.
  if (strpos($image['original'], 'system/files/') !== FALSE && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
    $src = file_create_url($src);
  }
  else {
    $pieces = explode('/', $src);
    foreach ($pieces as $part => $piece) {
      $pieces[$part] = rawurlencode($piece);
    }
    $src = $base_url . '/' . implode('/', $pieces);
  }

  // Strip the http:// from the path if the original did not include it.
  if (!preg_match('/^http[s]?:\\/\\/' . preg_quote($_SERVER['HTTP_HOST']) . '/', $image['original'])) {
    $src = preg_replace('/^http[s]?:\\/\\/' . preg_quote($_SERVER['HTTP_HOST']) . '/', '', $src);
  }
  $image['attributes']['src'] = $src;

  // Set the link properties if necessary.
  $image['link'] = FALSE;
  if ($image['resize'] && $settings['link'] && !$image['has_link']) {
    $image['link'] = array();
    $image['link']['attributes'] = array(
      'href' => $image['original'],
    );
    if (!empty($settings['class'])) {
      $image['link']['attributes']['class'] = $settings['class'];
    }
    if (!empty($settings['rel'])) {
      $image['link']['attributes']['rel'] = $settings['rel'];
    }
    if (!empty($image['attributes']['title'])) {
      $image['link']['attributes']['title'] = $image['attributes']['title'];
    }
  }

  // Theme the output and return.
  return theme('image_resize_filter_image', $image, $settings);
}

/**
 * Generate a themed image tag based on an image array.
 *
 * @param $image
 *   An array containing image information and properties.
 * @param $settings
 *   Settings for the input filter.
 */
function theme_image_resize_filter_image($image, $settings) {
  $output = '<img' . drupal_attributes($image['attributes']) . ' />';
  if ($image['link']) {
    $output = '<a' . drupal_attributes($image['link']['attributes']) . '>' . $output . '</a>';
  }
  return $output;
}

/**
 * A short-cut function to delete temporary remote images.
 */
function image_resize_filter_delete_temp_file($source, $filepath) {
  if ($source == 'remote' && is_file($filepath)) {
    @unlink($filepath);
  }
}

/**
 * Delete all generated image when the original file is removed.
 */
function image_resize_filter_delete_derivatives($original_filepath) {

  // First delete all derivatives in the saved file location.
  $path_info = image_resize_filter_pathinfo($original_filepath);
  $basename = $path_info['filename'];
  $extension = $path_info['extension'];
  $file_directory_path = file_directory_path();
  $local_file_dir = str_replace($file_directory_path, '', $path_info['dirname']);
  $local_file_dir = !empty($local_file_dir) ? $local_file_dir . '/' : '';
  $directory = $file_directory_path . '/resize/' . $local_file_dir;

  // Delete all the derivatives.
  file_scan_directory($directory, quotemeta($basename) . '-[0-9]+[x][0-9]+\\.' . quotemeta($extension), array(
    '.',
    '..',
    'CVS',
  ), 'file_delete');

  // Then work up the directories and delete any empty ones.
  $folders = explode('/', $directory);
  $directories = array();
  $current_directory = '';
  foreach ($folders as $folder) {
    $current_directory .= $folder . '/';
    $directories[] = $current_directory;
  }
  foreach (array_reverse($directories) as $directory) {
    if ($directory == $file_directory_path . '/') {
      break;
    }
    $directory_files = file_scan_directory($directory, '.*');
    if (empty($directory_files)) {
      @rmdir($directory);
    }
    else {
      break;
    }
  }
}

/**
 * Delete the entire set of cached images.
 */
function image_resize_filter_delete_all() {
  $directory = file_directory_path() . '/resize';
  image_resize_filter_delete_recursive($directory);
  cache_clear_all('*', 'cache_filter');
}

/**
 * Recursive deletion function for clearing out resized images directory.
 */
function image_resize_filter_delete_recursive($path) {
  if (is_file($path) || is_link($path)) {
    unlink($path);
  }
  elseif (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir
      ->read()) !== false) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
      image_resize_filter_delete_recursive($entry_path);
    }
    rmdir($path);
  }
}

/**
 * Utility function to return path information.
 */
function image_resize_filter_pathinfo($path) {
  $info = pathinfo($path);

  // Filename was added in PHP 5.2, add it for older PHP versions.
  if (!isset($info['filename'])) {
    $info['filename'] = basename($path, '.' . $info['extension']);
  }
  return $info;
}

Functions

Namesort descending Description
image_resize_filter_delete_all Delete the entire set of cached images.
image_resize_filter_delete_derivatives Delete all generated image when the original file is removed.
image_resize_filter_delete_recursive Recursive deletion function for clearing out resized images directory.
image_resize_filter_delete_temp_file A short-cut function to delete temporary remote images.
image_resize_filter_file_delete Implementation of hook_file_delete().
image_resize_filter_file_download Implementation of hook_file_download().
image_resize_filter_file_system_settings_submit Additional #submit handler for the system_file_system_settings form.
image_resize_filter_filter Implementation of hook_filter().
image_resize_filter_form The form for configuring the Image Resize Filter.
image_resize_filter_form_system_file_system_settings_alter Implementation of hook_form_[form_id]_alter().
image_resize_filter_get_images Parsing function to locate all images in a piece of text that need replacing.
image_resize_filter_help Implementation of hook_help().
image_resize_filter_image_tag Generate a themed image tag based on an image array.
image_resize_filter_nodeapi Implementation of hook_nodeapi().
image_resize_filter_pathinfo Utility function to return path information.
image_resize_filter_process_images Processing function for image resize filter. Replace img src properties with a URL to a resized image.
image_resize_filter_theme Implementation of hook_theme().
theme_image_resize_filter_form Theme callback to theme the Image Resize Filter form.
theme_image_resize_filter_image Generate a themed image tag based on an image array.