image.devel_generate.inc 4.0 KB

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