You are here

function _filefield_generate_file in FileField 6.3

Generate an image based on the properties of a field.

This is made to work with ImageField, and inspects the minimum and maximum image sizes and makes sure the generated image matches the requirements.

Return value

The path to the new file, in the temporary directory.

1 call to _filefield_generate_file()
_filefield_content_generate in ./filefield.devel.inc
Private function used by filefield_content_generate().

File

./filefield.devel.inc, line 37
Utility functions for generating FileField content. Note that image generation support requires the GD toolkit.

Code

function _filefield_generate_file($field) {
  if (empty($field['widget']['file_extensions'])) {
    $field['widget']['file_extensions'] = 'png jpg txt';
  }
  $extensions = array_intersect(explode(' ', $field['widget']['file_extensions']), array(
    'png',
    'jpg',
    'txt',
  ));
  $extension = array_rand(drupal_map_assoc($extensions));
  if ($extension == 'txt') {
    $filesize = empty($field['widget']['max_filesize_per_file']) ? 1024 : parse_size($field['widget']['max_filesize_per_file']);
    return _filefield_generate_textfile($filesize);
  }
  elseif (in_array($extension, array(
    'png',
    'jpg',
  )) && function_exists('imagecreate')) {
    $min_resolution = empty($field['widget']['min_resolution']) ? '100x100' : $field['widget']['min_resolution'];
    $max_resolution = empty($field['widget']['max_resolution']) ? '600x600' : $field['widget']['max_resolution'];
    return _filefield_generate_image($extension, $min_resolution, $max_resolution);
  }
  return FALSE;
}