image.devel_generate.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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', 'gif', 'jpg', 'jpeg'));
  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. $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
  28. file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
  29. if ($uri = file_unmanaged_move($path, $destination_dir)) {
  30. $file = new stdClass();
  31. $file->fid = NULL;
  32. $file->uri = $uri;
  33. $file->filename = drupal_basename($uri);
  34. $file->filemime = file_get_mimetype($file->uri);
  35. // @todo Randomize file owner.
  36. $file->uid = 1;
  37. $file = file_save($file);
  38. $images[$extension][$min_resolution][$max_resolution][$file->fid] = $file;
  39. }
  40. else {
  41. return FALSE;
  42. }
  43. }
  44. else {
  45. return FALSE;
  46. }
  47. }
  48. else {
  49. // Select one of the images we've already generated for this field.
  50. $file = new stdClass();
  51. $file->fid = array_rand($images[$extension][$min_resolution][$max_resolution]);
  52. }
  53. $object_field['fid'] = $file->fid;
  54. $object_field['alt'] = devel_create_greeking(4);
  55. $object_field['title'] = devel_create_greeking(4);
  56. return $object_field;
  57. }
  58. /**
  59. * Private function for creating a random image.
  60. *
  61. * This function only works with the GD toolkit. ImageMagick is not supported.
  62. */
  63. function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
  64. if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
  65. $destination = $tmp_file . '.' . $extension;
  66. file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY);
  67. $min = explode('x', $min_resolution);
  68. $max = explode('x', $max_resolution);
  69. $width = rand((int)$min[0], (int)$max[0]);
  70. $height = rand((int)$min[1], (int)$max[1]);
  71. // Make an image split into 4 sections with random colors.
  72. $im = imagecreate($width, $height);
  73. for ($n = 0; $n < 4; $n++) {
  74. $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
  75. $x = $width/2 * ($n % 2);
  76. $y = $height/2 * (int) ($n >= 2);
  77. imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
  78. }
  79. // Make a perfect circle in the image middle.
  80. $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
  81. $smaller_dimension = min($width, $height);
  82. $smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension;
  83. imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
  84. $save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension);
  85. $save_function($im, drupal_realpath($destination));
  86. }
  87. return $destination;
  88. }