You are here

function devel_generate_image in Devel 7

Private function for creating a random image.

This function only works with the GD toolkit. ImageMagick is not supported.

1 call to devel_generate_image()
_image_devel_generate in devel_generate/image.devel_generate.inc

File

devel_generate/image.devel_generate.inc, line 70

Code

function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
  if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
    $destination = $tmp_file . '.' . $extension;
    file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY);
    $min = explode('x', $min_resolution);
    $max = explode('x', $max_resolution);
    $width = rand((int) $min[0], (int) $max[0]);
    $height = rand((int) $min[1], (int) $max[1]);

    // Make an image split into 4 sections with random colors.
    $im = imagecreate($width, $height);
    for ($n = 0; $n < 4; $n++) {
      $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
      $x = $width / 2 * ($n % 2);
      $y = $height / 2 * (int) ($n >= 2);
      imagefilledrectangle($im, $x, $y, $x + $width / 2, $y + $height / 2, $color);
    }

    // Make a perfect circle in the image middle.
    $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
    $smaller_dimension = min($width, $height);
    $smaller_dimension = $smaller_dimension % 2 ? $smaller_dimension : $smaller_dimension;
    imageellipse($im, $width / 2, $height / 2, $smaller_dimension, $smaller_dimension, $color);
    $save_function = 'image' . ($extension == 'jpg' ? 'jpeg' : $extension);
    $save_function($im, drupal_realpath($destination));
  }
  return $destination;
}