You are here

function image_resize_filter_process_images in Image Resize Filter 7

Same name and namespace in other branches
  1. 6 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_process_filter in ./image_resize_filter.module
Filter callback function.

File

./image_resize_filter.module, line 493
After adding to a text format, this filter will parse the contents of submitted content and automatically scale image files to match the set dimensions of img tags.

Code

function image_resize_filter_process_images($images, $text, $settings) {
  $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'] = variable_get('file_default_scheme', 'public') . '://' . $local_file_path;
    }
    elseif (!$image['resize']) {
      $image['destination'] = $image['local_path'];
    }
    else {
      $path_info = image_resize_filter_pathinfo($image['local_path']);
      $local_file_dir = file_uri_target($path_info['dirname']);
      $local_file_path = 'resize/' . ($local_file_dir ? $local_file_dir . '/' : '') . $path_info['filename'] . '-' . $image['expected_size']['width'] . 'x' . $image['expected_size']['height'] . '.' . $path_info['extension'];
      $image['destination'] = $path_info['scheme'] . '://' . $local_file_path;
    }
    if (!file_exists($image['destination'])) {

      // Basic flood prevention of resizing.
      $resize_threshold = variable_get('image_resize_filter_threshold', 10);
      if (!flood_is_allowed('image_resize_filter_resize', $resize_threshold, 120)) {
        drupal_set_message(t('Image resize threshold of @count per minute reached. Some images have not been resized. Resave the content to resize remaining images.', array(
          '@count' => floor($resize_threshold / 2),
        )), 'error', FALSE);
        continue;
      }
      flood_register_event('image_resize_filter_resize', 120);

      // Create the resize directory.
      $directory = dirname($image['destination']);
      file_prepare_directory($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']) {
        $res = image_load($image['local_path']);
        if ($res) {

          // Image loaded successfully; resize.
          image_resize($res, $image['expected_size']['width'], $image['expected_size']['height']);
          image_save($res, $image['destination']);
        }
        else {

          // Image failed to load - type doesn't match extension or invalid; keep original file.
          $handle = fopen($image['destination'], 'w');
          fwrite($handle, file_get_contents($image['local_path']));
          fclose($handle);
        }
      }
      @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);
}