image.devel_generate.inc

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/devel/devel_generate/image.devel_generate.inc

Functions & methods

NameDescription
devel_generate_imagePrivate function for creating a random image.
image_devel_generate
_image_devel_generate

Constants

NameDescription
DEVEL_GENERATE_IMAGE_MAX

File

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