3 define('DEVEL_GENERATE_IMAGE_MAX', 5);
5 function image_devel_generate($object, $field, $instance, $bundle) {
6 if (function_exists('imagejpeg')) {
7 $devel_generate_image_function = variable_get('devel_generate_image_function', '_image_devel_generate');
8 if (!function_exists($devel_generate_image_function)) {
9 $devel_generate_image_function = '_image_devel_generate';
11 if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM
) {
12 return devel_generate_multiple($devel_generate_image_function, $object, $field, $instance, $bundle);
15 return $devel_generate_image_function($object, $field, $instance, $bundle);
20 function _image_devel_generate($object, $field, $instance, $bundle) {
21 $object_field = array();
22 static
$images = array();
24 $min_resolution = empty($instance['settings']['min_resolution']) ?
'100x100' : $instance['settings']['min_resolution'];
25 $max_resolution = empty($instance['settings']['max_resolution']) ?
'600x600' : $instance['settings']['max_resolution'];
26 $extensions = array_intersect(explode(' ', $instance['settings']['file_extensions']), array('png', 'gif', 'jpg', 'jpeg'));
27 $extension = array_rand(drupal_map_assoc($extensions));
29 // Generate a max of 5 different images.
30 if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX
) {
31 if ($path = devel_generate_image($extension, $min_resolution, $max_resolution)) {
32 $source = entity_create('file', array());
34 $source->uid
= 1; // TODO: randomize? Use case specific.
35 $source->filemime
= 'image/' .
pathinfo($path, PATHINFO_EXTENSION
);
36 $source->filename
= drupal_basename($path);
37 $destination_dir = $field['settings']['uri_scheme'] .
'://' .
$instance['settings']['file_directory'];
38 file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY
);
39 $destination = $destination_dir .
'/' .
basename($path);
40 $file = file_move($source, $destination, FILE_CREATE_DIRECTORY
);
41 $images[$extension][$min_resolution][$max_resolution][$file->fid
] = $file;
48 // Select one of the images we've already generated for this field.
49 $file = new
stdClass();
50 $file->fid
= array_rand($images[$extension][$min_resolution][$max_resolution]);
53 $object_field['fid'] = $file->fid
;
54 $object_field['alt'] = devel_create_greeking(4);
55 $object_field['title'] = devel_create_greeking(4);
60 * Private function for creating a random image.
62 * This function only works with the GD toolkit. ImageMagick is not supported.
64 function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
65 if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
66 $destination = $tmp_file .
'.' .
$extension;
67 file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY
);
69 $min = explode('x', $min_resolution);
70 $max = explode('x', $max_resolution);
72 $width = rand((int)$min[0], (int)$max[0]);
73 $height = rand((int)$min[1], (int)$max[1]);
75 // Make an image split into 4 sections with random colors.
76 $im = imagecreate($width, $height);
77 for ($n = 0; $n < 4; $n++) {
78 $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
79 $x = $width/2 * ($n %
2);
80 $y = $height/2 * (int) ($n >= 2);
81 imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
84 // Make a perfect circle in the image middle.
85 $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
86 $smaller_dimension = min($width, $height);
87 $smaller_dimension = ($smaller_dimension %
2) ?
$smaller_dimension : $smaller_dimension;
88 imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
90 $save_function = 'image'.
($extension == 'jpg' ?
'jpeg' : $extension);
91 $save_function($im, drupal_realpath($destination));