BaconmockupProvider.class.php

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

Baconmockup support class.

Classes

Namesort descending Description
BaconmockupProvider Add support for baconmockup.com.

File

sites/all/modules/ulmus/devel_image_provider/plugins/devel_image_provider/provider/BaconmockupProvider.class.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Baconmockup support class.
  5. */
  6. /**
  7. * Add support for baconmockup.com.
  8. */
  9. class BaconmockupProvider extends DevelImagesProviderBase {
  10. public function __construct($plugin) {
  11. parent::__construct($plugin);
  12. $this->provider_base_url = 'http://baconmockup.com';
  13. }
  14. public function generateImage($object, $field, $instance, $bundle) {
  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. $extension = 'jpg';
  19. if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX) {
  20. if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
  21. $destination = $tmp_file . '.' . $extension;
  22. file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY);
  23. $min = explode('x', $min_resolution);
  24. $max = explode('x', $max_resolution);
  25. $max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
  26. $max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
  27. $width = rand((int) $min[0], (int) $max[0]);
  28. $height = rand((int) $min[1], (int) $max[1]);
  29. $url = "$this->provider_base_url/$width/$height";
  30. $method = isset($this->settings['devel_image_provider_get_method']) ? $this->settings['devel_image_provider_get_method'] : 'file_get_contents';
  31. $path = devel_image_provider_get_file($url, $destination, $method);
  32. $source = new stdClass();
  33. $source->uri = $path;
  34. $source->uid = 1; // TODO: randomize? Use case specific.
  35. $source->filemime = 'image/jpg';
  36. if (!empty($instance['settings']['file_directory'])) {
  37. $instance['settings']['file_directory'] = $instance['settings']['file_directory'] . '/';
  38. }
  39. $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
  40. file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
  41. $destination = $destination_dir . basename($path);
  42. $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
  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. }