1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 23:02:42 +01:00

Add a basic random image generator

Summary: Adds an abstract base class for random image generation. Implements a concrete subclass for generating Mondrian-like images.

Test Plan:
{F40759}
{F40760}
{F40761}
{F40762}
{F40763}

Reviewers: DeedyDas, AnhNhan

Reviewed By: AnhNhan

CC: chad, aran

Differential Revision: https://secure.phabricator.com/D5705
This commit is contained in:
epriestley 2013-04-16 08:19:45 -07:00
parent 5943ae4670
commit de9b00b8be
4 changed files with 125 additions and 6 deletions

View file

@ -1037,8 +1037,10 @@ phutil_register_library_map(array(
'PhabricatorLDAPUnknownUserException' => 'applications/auth/ldap/PhabricatorLDAPUnknownUserException.php',
'PhabricatorLDAPUnlinkController' => 'applications/auth/controller/PhabricatorLDAPUnlinkController.php',
'PhabricatorLintEngine' => 'infrastructure/lint/PhabricatorLintEngine.php',
'PhabricatorLipsumArtist' => 'applications/lipsum/image/PhabricatorLipsumArtist.php',
'PhabricatorLipsumGenerateWorkflow' => 'applications/lipsum/management/PhabricatorLipsumGenerateWorkflow.php',
'PhabricatorLipsumManagementWorkflow' => 'applications/lipsum/management/PhabricatorLipsumManagementWorkflow.php',
'PhabricatorLipsumMondrianArtist' => 'applications/lipsum/image/PhabricatorLipsumMondrianArtist.php',
'PhabricatorLiskDAO' => 'infrastructure/storage/lisk/PhabricatorLiskDAO.php',
'PhabricatorLocalDiskFileStorageEngine' => 'applications/files/engine/PhabricatorLocalDiskFileStorageEngine.php',
'PhabricatorLocalTimeTestCase' => 'view/__tests__/PhabricatorLocalTimeTestCase.php',
@ -2739,6 +2741,7 @@ phutil_register_library_map(array(
'PhabricatorLintEngine' => 'PhutilLintEngine',
'PhabricatorLipsumGenerateWorkflow' => 'PhabricatorLipsumManagementWorkflow',
'PhabricatorLipsumManagementWorkflow' => 'PhutilArgumentWorkflow',
'PhabricatorLipsumMondrianArtist' => 'PhabricatorLipsumArtist',
'PhabricatorLiskDAO' => 'LiskDAO',
'PhabricatorLocalDiskFileStorageEngine' => 'PhabricatorFileStorageEngine',
'PhabricatorLocalTimeTestCase' => 'PhabricatorTestCase',

View file

@ -104,7 +104,7 @@ final class PhabricatorImageTransformer {
$x,
$scaled_y);
return $this->saveImageDataInAnyFormat($img, $file->getMimeType());
return self::saveImageDataInAnyFormat($img, $file->getMimeType());
}
private function crasslyCropTo(PhabricatorFile $file, $top, $left, $w, $h) {
@ -126,7 +126,7 @@ final class PhabricatorImageTransformer {
$w, $h,
$orig_w, $orig_h);
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
return self::saveImageDataInAnyFormat($dst, $file->getMimeType());
}
@ -141,7 +141,7 @@ final class PhabricatorImageTransformer {
}
$dst = $this->applyScaleTo($file, $dx, $dy);
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
return self::saveImageDataInAnyFormat($dst, $file->getMimeType());
}
private function getBlankDestinationFile($dx, $dy) {
@ -254,7 +254,7 @@ final class PhabricatorImageTransformer {
$sdx, $sdy,
$x, $y);
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
return self::saveImageDataInAnyFormat($dst, $file->getMimeType());
}
private function applyMemeTo(
@ -314,7 +314,7 @@ final class PhabricatorImageTransformer {
break;
}
}
return $this->saveImageDataInAnyFormat($img, $file->getMimeType());
return self::saveImageDataInAnyFormat($img, $file->getMimeType());
}
private function makeImageWithTextBorder($img, $font_size, $x, $y,
@ -352,7 +352,7 @@ final class PhabricatorImageTransformer {
);
}
private function saveImageDataInAnyFormat($data, $preferred_mime = '') {
public static function saveImageDataInAnyFormat($data, $preferred_mime = '') {
switch ($preferred_mime) {
case 'image/gif': // Gif doesn't support true color
case 'image/png':

View file

@ -0,0 +1,68 @@
<?php
abstract class PhabricatorLipsumArtist {
protected function getHSBColor($h, $s, $b) {
if ($s == 0) {
$cr = $b;
$cg = $b;
$cb = $b;
} else {
$h /= 60;
$i = (int)$h;
$f = $h - $i;
$p = $b * (1 - $s);
$q = $b * (1 - $s * $f);
$t = $b * (1 - $s * (1 - $f));
switch ($i) {
case 0:
$cr = $b;
$cg = $t;
$cb = $p;
break;
case 1:
$cr = $q;
$cg = $b;
$cb = $p;
break;
case 2:
$cr = $p;
$cg = $b;
$cb = $t;
break;
case 3:
$cr = $p;
$cg = $q;
$cb = $b;
break;
case 4:
$cr = $t;
$cg = $p;
$cb = $b;
break;
default:
$cr = $b;
$cg = $p;
$cb = $q;
break;
}
}
$cr = (int)round($cr * 255);
$cg = (int)round($cg * 255);
$cb = (int)round($cb * 255);
return ($cr << 16) + ($cg << 8) + $cb;
}
public function generate($x, $y) {
$image = imagecreatetruecolor($x, $y);
$this->draw($image, $x, $y);
return PhabricatorImageTransformer::saveImageDataInAnyFormat(
$image,
'image/jpeg');
}
abstract protected function draw($image, $x, $y);
}

View file

@ -0,0 +1,48 @@
<?php
final class PhabricatorLipsumMondrianArtist extends PhabricatorLipsumArtist {
protected function draw($image, $x, $y) {
$c_white = 0xFFFFFF;
$c_black = 0x000000;
imagefill($image, 0, 0, $c_white);
$lines_h = mt_rand(2, 5);
$lines_v = mt_rand(2, 5);
for ($ii = 0; $ii < $lines_h; $ii++) {
$yp = mt_rand(0, $y);
$thickness = mt_rand(2, 3);
for ($jj = 0; $jj < $thickness; $jj++) {
imageline($image, 0, $yp + $jj, $x, $yp + $jj, $c_black);
}
}
for ($ii = 0; $ii < $lines_v; $ii++) {
$xp = mt_rand(0, $x);
$thickness = mt_rand(2, 3);
for ($jj = 0; $jj < $thickness; $jj++) {
imageline($image, $xp + $jj, 0, $xp + $jj, $y, $c_black);
}
}
$fills = mt_rand(3, 8);
for ($ii = 0; $ii < $fills; $ii++) {
$xp = mt_rand(0, $x);
$yp = mt_rand(0, $y);
if (imagecolorat($image, $xp, $yp) != $c_white) {
continue;
}
$c_fill = $this->getHSBColor(
mt_rand(0, 359),
mt_rand(80, 100) / 100,
mt_rand(90, 100) / 100);
imagefill($image, $xp, $yp, $c_fill);
}
}
}