You are here

function _image_devel_generate in Devel 7

1 string reference to '_image_devel_generate'
image_devel_generate in devel_generate/image.devel_generate.inc

File

devel_generate/image.devel_generate.inc, line 20

Code

function _image_devel_generate($object, $field, $instance, $bundle) {
  $object_field = array();
  static $images = array();
  $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
  $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
  $extensions = array_intersect(explode(' ', $instance['settings']['file_extensions']), array(
    'png',
    'gif',
    'jpg',
    'jpeg',
  ));
  $extension = array_rand(drupal_map_assoc($extensions));

  // Generate a max of 5 different images.
  if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX) {
    if ($path = devel_generate_image($extension, $min_resolution, $max_resolution)) {
      $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
      file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
      if ($uri = file_unmanaged_move($path, $destination_dir)) {
        $file = new stdClass();
        $file->fid = NULL;
        $file->uri = $uri;
        $file->filename = drupal_basename($uri);
        $file->filemime = file_get_mimetype($file->uri);

        // @todo Randomize file owner.
        $file->uid = 1;
        $file = file_save($file);
        $images[$extension][$min_resolution][$max_resolution][$file->fid] = $file;
      }
      else {
        return FALSE;
      }
    }
    else {
      return FALSE;
    }
  }
  else {

    // Select one of the images we've already generated for this field.
    $file = new stdClass();
    $file->fid = array_rand($images[$extension][$min_resolution][$max_resolution]);
  }
  $object_field['fid'] = $file->fid;
  $object_field['alt'] = devel_create_greeking(4);
  $object_field['title'] = devel_create_greeking(4);
  return $object_field;
}