summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--devel_generate/devel_generate.fields.inc103
-rw-r--r--devel_generate/file.devel_generate.inc38
-rw-r--r--devel_generate/image.devel_generate.inc71
3 files changed, 94 insertions, 118 deletions
diff --git a/devel_generate/devel_generate.fields.inc b/devel_generate/devel_generate.fields.inc
index 1190d7a..43c5f45 100644
--- a/devel_generate/devel_generate.fields.inc
+++ b/devel_generate/devel_generate.fields.inc
@@ -92,105 +92,4 @@ function devel_generate_multiple($function, $object, $field, $instance, $bundle)
}
}
return $object_field;
-}
-
-/**
- * Generate an image based on the properties of a field.
- *
- * Inspect the minimum and maximum image sizes and makes sure the generated
- * image matches the requirements.
- *
- * @return
- * The path to the new file, in the temporary directory.
- */
-function devel_generate_file($instance) {
- if (empty($instance['settings']['file_extensions'])) {
- $instance['settings']['file_extensions'] = 'png jpg jpeg txt';
- }
-
- $extensions = array_intersect(explode(' ', $instance['settings']['file_extensions']), array('png', 'jpg', 'jpeg', 'txt'));
- $extension = array_rand(drupal_map_assoc($extensions));
-
- if ($extension == 'txt') {
- $filesize = empty($instance['settings']['max_filesize']) ? 1024 : parse_size($instance['settings']['max_filesize']);
- return devel_generate_textfile($filesize);
- }
- elseif (in_array($extension, array('png', 'jpg', 'jpeg')) && function_exists('imagecreate')) {
- $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
- $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
- return devel_generate_image($extension, $min_resolution, $max_resolution);
- }
-
- return FALSE;
-}
-
-/**
- * Private function for generating a random text file.
- */
-function devel_generate_textfile($filesize = 1024) {
- static $filesizes = array();
-
- $temp_file = FALSE;
- if (isset($filesizes[$filesize])) {
- $temp_file = $filesizes[$filesize];
- }
- elseif ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
- $destination = $tmp_file . '.txt';
- file_unmanaged_move($tmp_file, $destination);
-
- $fp = fopen($destination, 'w');
- fwrite($fp, str_repeat('01', $filesize/2));
- fclose($fp);
- $filesizes[$filesize] = $destination;
- }
-
- return $destination;
-}
-
-/**
- * Private function for creating a random image.
- *
- * This function only works with the GD toolkit. ImageMagick is not supported.
- */
-function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
- static $images = array();
-
- // Generate a max of 5 different images.
- if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) < 5) {
- if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
- $destination = $tmp_file . '.' . $extension;
- file_unmanaged_move($tmp_file, $destination);
-
- $min = explode('x', $min_resolution);
- $max = explode('x', $max_resolution);
-
- $width = rand((int)$min[0], (int)$max[0]);
- $height = rand((int)$min[0], (int)$max[0]);
-
- // Make a image split into 4 sections with random colors.
- $im = imagecreate($width, $height);
- for ($n = 0; $n < 4; $n++) {
- $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
- $x = $width/2 * ($n % 2);
- $y = $height/2 * (int) ($n >= 2);
- imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
- }
-
- // Make a perfect circle in the image middle.
- $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
- $smaller_dimension = min($width, $height);
- $smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension;
- imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
-
- $save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension);
- $save_function($im, drupal_realpath($destination));
-
- $images[$extension][$min_resolution][$max_resolution][$destination] = $destination;
- }
- }
- // Select one of the images we've already generated for this field.
- else {
- $destination = array_rand($images[$extension][$min_resolution][$max_resolution]);
- }
- return $destination;
-}
+} \ No newline at end of file
diff --git a/devel_generate/file.devel_generate.inc b/devel_generate/file.devel_generate.inc
index f26035b..b12aa94 100644
--- a/devel_generate/file.devel_generate.inc
+++ b/devel_generate/file.devel_generate.inc
@@ -11,17 +11,37 @@ function file_devel_generate($object, $field, $instance, $bundle) {
}
function _file_devel_generate($object, $field, $instance, $bundle) {
- $object_field = array();
+ static $file;
- if ($path = devel_generate_file($instance)) {
- $source->uri = $path;
- $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
- $file = file_move($source, $destination);
-
- $object_field['fid'] = $file->fid;
- $object_field['display'] = $field['settings']['display_default'];
- $object_field['description'] = devel_create_greeking(10);
+ if (empty($file)) {
+ if ($path = devel_generate_textfile()) {
+ $source->uri = $path;
+ $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
+ $file = file_move($source, $destination);
+ }
+ else {
+ return FALSE;
+ }
}
+ $object_field['fid'] = $file->fid;
+ $object_field['display'] = $field['settings']['display_default'];
+ $object_field['description'] = devel_create_greeking(10);
return $object_field;
}
+
+/**
+ * Private function for generating a random text file.
+ */
+function devel_generate_textfile($filesize = 1024) {
+ if ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
+ $destination = $tmp_file . '.txt';
+ file_unmanaged_move($tmp_file, $destination);
+
+ $fp = fopen($destination, 'w');
+ fwrite($fp, str_repeat('01', $filesize/2));
+ fclose($fp);
+
+ return $destination;
+ }
+}
diff --git a/devel_generate/image.devel_generate.inc b/devel_generate/image.devel_generate.inc
index f2e11f2..03af1f6 100644
--- a/devel_generate/image.devel_generate.inc
+++ b/devel_generate/image.devel_generate.inc
@@ -1,6 +1,8 @@
<?php
// $Id$
+define('DEVEL_GENERATE_IMAGE_MAX', 5);
+
function image_devel_generate($object, $field, $instance, $bundle) {
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
return devel_generate_multiple('_image_devel_generate', $object, $field, $instance, $bundle);
@@ -12,16 +14,71 @@ function image_devel_generate($object, $field, $instance, $bundle) {
function _image_devel_generate($object, $field, $instance, $bundle) {
$object_field = array();
+ static $images = array();
- if ($path = devel_generate_file($instance)) {
- $source->uri = $path;
- $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
- $file = file_move($source, $destination);
+ $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', 'jpg'));
+ $extension = array_rand(drupal_map_assoc($extensions));
- $object_field['fid'] = $file->fid;
- $object_field['alt'] = devel_create_greeking(4);
- $object_field['title'] = devel_create_greeking(10);
+ // 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)) {
+ $source->uri = $path;
+ $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
+ $file = file_move($source, $destination);
+ $images[$extension][$min_resolution][$max_resolution][$file->fid] = $file;
+ }
+ else {
+ return FALSE;
+ }
+ }
+ else {
+ // Select one of the images we've already generated for this field.
+ $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(10);
return $object_field;
}
+
+/**
+ * Private function for creating a random image.
+ *
+ * This function only works with the GD toolkit. ImageMagick is not supported.
+ */
+function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
+ if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
+ $destination = $tmp_file . '.' . $extension;
+ file_unmanaged_move($tmp_file, $destination);
+
+ $min = explode('x', $min_resolution);
+ $max = explode('x', $max_resolution);
+
+ $width = rand((int)$min[0], (int)$max[0]);
+ $height = rand((int)$min[0], (int)$max[0]);
+
+ // Make a image split into 4 sections with random colors.
+ $im = imagecreate($width, $height);
+ for ($n = 0; $n < 4; $n++) {
+ $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
+ $x = $width/2 * ($n % 2);
+ $y = $height/2 * (int) ($n >= 2);
+ imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
+ }
+
+ // Make a perfect circle in the image middle.
+ $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
+ $smaller_dimension = min($width, $height);
+ $smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension;
+ imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
+
+ $save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension);
+ $save_function($im, drupal_realpath($destination));
+
+ $images[$extension][$min_resolution][$max_resolution][$destination] = $destination;
+ }
+ return $destination;
+}