image.devel_generate.inc 3.8 KB

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