public function 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);
  $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, $destination);
  return $destination;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!Random.php/function/Random::image/8.1.x