You are here

class LocalFolderProvider in Devel images provider 8

Same name and namespace in other branches
  1. 7 plugins/devel_image_provider/provider/LocalFolderProvider.class.php \LocalFolderProvider

Add support for local images.

Hierarchy

Expanded class hierarchy of LocalFolderProvider

1 string reference to 'LocalFolderProvider'
localfolder.inc in plugins/devel_image_provider/provider/localfolder.inc

File

plugins/devel_image_provider/provider/LocalFolderProvider.class.php, line 10
Local folder support class.

View source
class LocalFolderProvider extends DevelImagesProviderBase {
  function availableMethods() {

    // No fetch method as it grabs the images from local folder.
    return array();
  }
  function settingsForm() {
    $form = parent::settingsForm();
    unset($form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_gray']);

    // Provider specific settings.
    $form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path to folder'),
      '#description' => t('Enter full path of the folder. Example: /home/user1011/images'),
      '#size' => 90,
      '#default_value' => isset($this->settings['devel_image_provider_path']) ? $this->settings['devel_image_provider_path'] : '',
    );
    return $form;
  }
  public function settingsFormValidate(&$form, &$form_state) {
    if (empty($form_state['values']['config_providers']['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_path'])) {
      form_error($form['config_providers']['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_path'], t('Path cannot be empty'));
      return FALSE;
    }
    if (!is_readable($form_state['values']['config_providers']['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_path'])) {
      form_error($form['config_providers']['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_path'], t('Specified folder is not valid or not readable'));
    }
  }
  public function generateImage($object, $field, $instance, $bundle) {
    $object_field = array();
    static $available_images = array();
    if (empty($available_images)) {
      $available_images = $this
        ->getImages();
    }
    if (empty($available_images)) {
      $args = func_get_args();
      return call_user_func_array('_image_devel_generate', $args);
    }
    $extension = array_rand(array(
      'jpg' => 'jpg',
      'png' => 'png',
    ));
    $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
    $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
    if (FALSE === ($tmp_file = drupal_tempnam('temporary://', 'imagefield_'))) {
      return FALSE;
    }
    $destination = $tmp_file . '.' . $extension;
    file_unmanaged_move($tmp_file, $destination, FILE_EXISTS_REPLACE);
    $min = explode('x', $min_resolution);
    $max = explode('x', $max_resolution);
    $max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
    $max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
    $width = rand((int) $min[0], (int) $max[0]);
    $height = rand((int) $min[1], (int) $max[1]);
    $rand_file = array_rand($available_images);
    $image = image_load($rand_file);
    if (!image_scale_and_crop($image, $width, $height)) {
      return FALSE;
    }

    // Use destination image type.
    $image->info['extension'] = $extension;
    if (!image_save($image, $destination)) {
      return FALSE;
    }
    $source = new stdClass();
    $source->uri = $destination;
    $source->uid = 1;

    // TODO: randomize? Use case specific.
    $source->filemime = $image->info['mime_type'];
    $source->filename = drupal_basename($image->source);
    $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
    file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
    $destination = $destination_dir . '/' . basename($destination);
    $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
    $object_field['fid'] = $file->fid;
    $object_field['alt'] = devel_create_greeking(4);
    $object_field['title'] = devel_create_greeking(4);
    return $object_field;
  }

  /**
   * Helper function to get all the images from the configured folder.
   */
  private function getImages() {
    $files = array();
    $count = 1;

    // Limiting number of images to find to 100.
    // @TODO: add this as a setting.
    $max_count = 100;

    // Remove trailing slash.
    $dir = rtrim($this->settings['devel_image_provider_path'], '/');
    if (is_dir($dir) && ($handle = opendir($dir))) {
      while (FALSE !== ($filename = readdir($handle)) && $count <= $max_count) {
        $path = "{$dir}/{$filename}";
        if ($filename[0] != '.' && preg_match('/.*\\.(jpg|jpeg|png)$/i', $filename) && FALSE !== image_get_info($path)) {
          $file = new stdClass();
          $file->uri = file_stream_wrapper_uri_normalize($path);
          $file->filename = $filename;
          $file->name = pathinfo($filename, PATHINFO_FILENAME);
          $files[$file->uri] = $file;
          $count++;
        }
      }
      closedir($handle);
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DevelImagesProviderBase::$provider_base_url protected property The provider url.
DevelImagesProviderBase::$settings protected property Defined settings for the method.
DevelImagesProviderBase::settingsFormSubmit public function Generates a settings form submit function for this handler. Overrides DevelImagesProviderInterface::settingsFormSubmit
DevelImagesProviderBase::__construct public function 7
LocalFolderProvider::availableMethods function Sets the available methods to get the images from this provider. Overrides DevelImagesProviderBase::availableMethods
LocalFolderProvider::generateImage public function Image generation handler. Overrides DevelImagesProviderBase::generateImage
LocalFolderProvider::getImages private function Helper function to get all the images from the configured folder.
LocalFolderProvider::settingsForm function Generates a settings form for this handler. Overrides DevelImagesProviderBase::settingsForm
LocalFolderProvider::settingsFormValidate public function Generates a settings form validate function for this handler. Overrides DevelImagesProviderBase::settingsFormValidate