DummyImageProvider.class.php

  1. cis7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  2. cle7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  3. ecd7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  4. elmsmedia7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  5. harmony7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  6. icor7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  7. meedjum_blog7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
  8. mooc7 sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php

Dummy image support class.

Classes

Namesort descending Description
DummyImageProvider Add support for dummyimage.com.

File

sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/DummyImageProvider.class.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Dummy image support class.
  5. */
  6. /**
  7. * Add support for dummyimage.com.
  8. */
  9. class DummyImageProvider extends DevelImagesProviderBase {
  10. public function __construct($plugin) {
  11. parent::__construct($plugin);
  12. $this->provider_base_url = 'http://dummyimage.com';
  13. }
  14. function settingsForm() {
  15. $form = parent::settingsForm();
  16. $form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_background_color'] = array(
  17. '#type' => 'textfield',
  18. '#title' => t('Custom Background Color'),
  19. '#description' => t('Optional: Enter a background color in hex notation (e.g. #BADA55).'),
  20. '#default_value' => isset($this->settings['devel_image_provider_background_color']) ? $this->settings['devel_image_provider_background_color'] : NULL,
  21. );
  22. $form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_text_color'] = array(
  23. '#type' => 'textfield',
  24. '#title' => t('Custom Text Color'),
  25. '#description' => t('Optional: Enter a text color in hex notation (e.g. #B00B00). Note that text color requires a background color to be specified as well.'),
  26. '#default_value' => isset($this->settings['devel_image_provider_text_color']) ? $this->settings['devel_image_provider_text_color'] : NULL,
  27. );
  28. $form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_include_text'] = array(
  29. '#type' => 'radios',
  30. '#title' => t('Choose the text added inside the generated image'),
  31. '#default_value' => isset($this->settings['devel_image_provider_include_text']) ? $this->settings['devel_image_provider_include_text'] : 'default',
  32. '#options' => array(
  33. 'default' => t('Default (image dimensions)'),
  34. 'random' => t('Random text'),
  35. 'custom' => t('Custom text'),
  36. ),
  37. );
  38. $form['devel_image_provider_' . $this->plugin['name']]['devel_image_provider_custom_text'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Custom Text'),
  41. '#maxlength' => 255,
  42. '#description' => t('Enter some custom text to be rendered instead of the default image dimensions.'),
  43. '#default_value' => isset($this->settings['devel_image_provider_custom_text']) ? $this->settings['devel_image_provider_custom_text'] : NULL,
  44. '#states' => array(
  45. 'visible' => array(
  46. ':input[name="config_providers[devel_image_provider_' . $this->plugin['name'] . '][devel_image_provider_include_text]"]' => array('value' => 'custom'),
  47. ),
  48. ),
  49. );
  50. return $form;
  51. }
  52. public function generateImage($object, $field, $instance, $bundle) {
  53. static $images = array();
  54. $min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
  55. $max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
  56. $extension = 'jpg';
  57. if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX) {
  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. $max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
  64. $max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
  65. $width = rand((int) $min[0], (int) $max[0]);
  66. $height = rand((int) $min[1], (int) $max[1]);
  67. $background_color = isset($this->settings['devel_image_provider_background_color']) ? $this->settings['devel_image_provider_background_color'] : FALSE;
  68. if ($background_color) {
  69. if (preg_match('/^#[a-f0-9]{6}$/i', $background_color)) { // Check for valid hex number
  70. $background_color = "/" . str_replace('#', '', check_plain($background_color)); // Strip out #
  71. }
  72. else {
  73. $background_color = '';
  74. }
  75. }
  76. else {
  77. $background_color = '';
  78. }
  79. $text_color = isset($this->settings['devel_image_provider_text_color']) ? $this->settings['devel_image_provider_text_color'] : FALSE;
  80. if ($text_color) {
  81. // Check for valid hex number.
  82. if (preg_match('/^#[a-f0-9]{6}$/i', $text_color)) {
  83. // Strip out # character.
  84. $text_color = "/" . str_replace('#', '', check_plain($text_color));
  85. }
  86. else {
  87. $text_color = '';
  88. }
  89. }
  90. else {
  91. $text_color = '';
  92. }
  93. $include_text = isset($this->settings['devel_image_provider_include_text']) ? $this->settings['devel_image_provider_include_text'] : FALSE;
  94. switch ($include_text) {
  95. case 'custom':
  96. $custom_text = isset($this->settings['devel_image_provider_custom_text']) ? $this->settings['devel_image_provider_custom_text'] : '';
  97. break;
  98. case 'random':
  99. // Small random text as text size is depending on the image size.
  100. $custom_text = trim(substr(devel_create_greeking(mt_rand(1, 3)), 0, 8));
  101. break;
  102. case 'default':
  103. default:
  104. $custom_text = '';
  105. break;
  106. }
  107. if (!empty($custom_text)) {
  108. //Replace the spaces with + as per provider specifications
  109. $custom_text = "&text=" . str_replace(' ', '+', check_plain($custom_text));
  110. }
  111. $url = "$this->provider_base_url/" . $width . "x" . $height . '/' . $background_color . '/' . $text_color . '&text=' . $custom_text;
  112. $method = isset($this->settings['devel_image_provider_get_method']) ? $this->settings['devel_image_provider_get_method'] : 'file_get_contents';
  113. $path = devel_image_provider_get_file($url, $destination, $method);
  114. $source = new stdClass();
  115. $source->uri = $path;
  116. $source->uid = 1; // TODO: randomize? Use case specific.
  117. $source->filemime = 'image/jpg';
  118. if (!empty($instance['settings']['file_directory'])) {
  119. $instance['settings']['file_directory'] = $instance['settings']['file_directory'] . '/';
  120. }
  121. $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
  122. file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
  123. $destination = $destination_dir . basename($path);
  124. $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
  125. }
  126. else {
  127. return FALSE;
  128. }
  129. }
  130. else {
  131. // Select one of the images we've already generated for this field.
  132. $file = new stdClass();
  133. $file->fid = array_rand($images[$extension][$min_resolution][$max_resolution]);
  134. }
  135. $object_field['fid'] = $file->fid;
  136. $object_field['alt'] = devel_create_greeking(4);
  137. $object_field['title'] = devel_create_greeking(4);
  138. return $object_field;
  139. }
  140. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.