You are here

function image_resize_filter_process_images in Image Resize Filter 6

Same name and namespace in other branches
  1. 7 image_resize_filter.module \image_resize_filter_process_images()

Processing function for image resize filter. Replace img src properties with a URL to a resized image.

Parameters

$images: An array of image information, detailing images that need to be replaced.

$text: The original text of the post that needs src tags updated.

$settings: An array of setting for generating the image tag.

1 call to image_resize_filter_process_images()
image_resize_filter_filter in ./image_resize_filter.module
Implementation of hook_filter().

File

./image_resize_filter.module, line 476
image_resize_filter.module

Code

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