Random::image

public Random::image($destination, $min_resolution, $max_resolution)

Create a placeholder image.

Parameters

string $destination: The absolute file path where the image should be stored.

int $min_resolution:

int $max_resolution:

Return value

string Path to image file.

File

core/lib/Drupal/Component/Utility/Random.php, line 271

Class

Random
Defines a utility class for creating random data.

Namespace

Drupal\Component\Utility

Code

public function image($destination, $min_resolution, $max_resolution) {
  $extension = pathinfo($destination, PATHINFO_EXTENSION);
  $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);
  imageellipse($im, $width / 2, $height / 2, $smaller_dimension, $smaller_dimension, $color);

  $save_function = 'image' . ($extension == 'jpg' ? 'jpeg' : $extension);
  $save_function($im, $destination);
  return $destination;
}
doc_Drupal
2016-10-29 09:36:19
Comments
Leave a Comment

Please login to continue.