You are here

public function LocalFolderProvider::generateImage in Devel images provider 8

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

Image generation handler.

Overrides DevelImagesProviderBase::generateImage

File

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

Class

LocalFolderProvider
Add support for local images.

Code

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