52 'counterRotation' => [ [ 1, 0 ], [ 0, 1 ] ]
58 'counterRotation' => [ [ -1, 0 ], [ 0, -1 ] ]
64 'counterRotation' => [ [ 0, 1 ], [ 1, 0 ] ]
70 'counterRotation' => [ [ 0, -1 ], [ -1, 0 ] ]
75 foreach ( [
'dictionaryFile',
'minWidth',
'minHeight',
76 'maxWidth',
'maxHeight',
'shapesToDraw' ]
as $property
84 if ( !isset( $this->dictionaryFile ) ) {
87 '/usr/share/dict/words',
89 __DIR__ .
'/words.txt'
98 if ( !isset( $this->dictionaryFile ) ) {
99 throw new Exception(
"RandomImageGenerator: dictionary file not "
100 .
"found or not specified properly" );
116 foreach ( $filenames
as $filename ) {
117 $this->{$imageWriteMethod}( $this->
getImageSpec(), $format, $filename );
133 if ( $format ===
'svg' ) {
139 return 'writeImageWithApi';
144 return 'writeImageWithCommandLine';
147 throw new Exception(
"RandomImageGenerator: could not find a suitable "
148 .
"method to write images in '$format' format" );
161 if ( is_null( $dir ) ) {
166 $basename = $pair[0] .
'_' . $pair[1];
167 if ( !is_null( $extension ) ) {
168 $basename .=
'.' . $extension;
170 $basename = preg_replace(
'/\s+/',
'', $basename );
171 $filenames[] =
"$dir/$basename";
188 $spec[
'width'] = mt_rand( $this->minWidth, $this->maxWidth );
189 $spec[
'height'] = mt_rand( $this->minHeight, $this->maxHeight );
192 $diagonalLength = sqrt( $spec[
'width'] ** 2 + $spec[
'height'] ** 2 );
196 $radius = mt_rand( 0, $diagonalLength / 4 );
197 if ( $radius == 0 ) {
200 $originX = mt_rand( -1 * $radius, $spec[
'width'] + $radius );
201 $originY = mt_rand( -1 * $radius, $spec[
'height'] + $radius );
202 $angle = mt_rand( 0, ( 3.141592 / 2 ) * $radius ) / $radius;
203 $legDeltaX = round( $radius * sin( $angle ) );
204 $legDeltaY = round( $radius * cos( $angle ) );
209 [
'x' => $originX,
'y' => $originY - $radius ],
210 [
'x' => $originX + $legDeltaX,
'y' => $originY + $legDeltaY ],
211 [
'x' => $originX - $legDeltaX,
'y' => $originY + $legDeltaY ],
212 [
'x' => $originX,
'y' => $originY - $radius ]
217 $spec[
'draws'] = $draws;
231 foreach ( $shape
as $point ) {
232 $points[] = $point[
'x'] .
',' . $point[
'y'];
235 return implode(
" ",
$points );
248 public function writeSvg( $spec, $format, $filename ) {
249 $svg =
new SimpleXmlElement(
'<svg/>' );
250 $svg->addAttribute(
'xmlns',
'http://www.w3.org/2000/svg' );
251 $svg->addAttribute(
'version',
'1.1' );
252 $svg->addAttribute(
'width', $spec[
'width'] );
253 $svg->addAttribute(
'height', $spec[
'height'] );
254 $g = $svg->addChild(
'g' );
255 foreach ( $spec[
'draws']
as $drawSpec ) {
256 $shape = $g->addChild(
'polygon' );
257 $shape->addAttribute(
'fill', $drawSpec[
'fill'] );
258 $shape->addAttribute(
'points', self::shapePointsToString( $drawSpec[
'shape'] ) );
261 $fh = fopen( $filename,
'w' );
263 throw new Exception(
"couldn't open $filename for writing" );
265 fwrite( $fh, $svg->asXML() );
266 if ( !fclose( $fh ) ) {
267 throw new Exception(
"couldn't close $filename" );
288 $orientation = self::$orientations[0];
289 if ( $format ==
'jpg' ) {
290 $orientation = self::$orientations[array_rand( self::$orientations )];
294 $image->newImage( $spec[
'width'], $spec[
'height'],
new ImagickPixel( $spec[
'fill'] ) );
296 foreach ( $spec[
'draws']
as $drawSpec ) {
297 $draw =
new ImagickDraw();
298 $draw->setFillColor( $drawSpec[
'fill'] );
299 $draw->polygon( $drawSpec[
'shape'] );
300 $image->drawImage( $draw );
303 $image->setImageFormat( $format );
308 $image->writeImage( $filename );
317 "set Exif.Image.Orientation {$orientation['exifCode']}",
322 $retval =
$result->getExitCode();
323 if ( $retval !== 0 ) {
324 print
"Error with $command: $retval, {$result->getStdout()}\n";
341 if ( $dims[
'x'] < 0 ) {
342 $correctionX = abs( $dims[
'x'] );
344 if ( $dims[
'y'] < 0 ) {
345 $correctionY = abs( $dims[
'y'] );
347 $tSpec[
'width'] = abs( $dims[
'x'] );
348 $tSpec[
'height'] = abs( $dims[
'y'] );
349 $tSpec[
'fill'] = $spec[
'fill'];
350 $tSpec[
'draws'] = [];
351 foreach ( $spec[
'draws']
as $draw ) {
353 'fill' => $draw[
'fill'],
356 foreach ( $draw[
'shape']
as $point ) {
358 $tPoint[
'x'] += $correctionX;
359 $tPoint[
'y'] += $correctionY;
360 $tDraw[
'shape'][] = $tPoint;
362 $tSpec[
'draws'][] = $tDraw;
377 'x' => $x * $matrix[0][0] + $y * $matrix[0][1],
378 'y' => $x * $matrix[1][0] + $y * $matrix[1][1]
405 $spec[
'width'] .
'x' . $spec[
'height'],
406 "xc:{$spec['fill']}",
408 foreach ( $spec[
'draws']
as $draw ) {
409 $fill = $draw[
'fill'];
411 $drawCommand =
"fill $fill polygon $polygon";
413 $args[] = $drawCommand;
419 return (
$result->getExitCode() === 0 );
429 for ( $i = 0; $i <= 2; $i++ ) {
430 $components[] = mt_rand( 0, 255 );
433 return 'rgb(' . implode(
', ', $components ) .
')';
448 for ( $i = 0; $i < $count; $i += 2 ) {
470 for ( $i = 0; $i < $number_desired; $i++ ) {
479 $fh = fopen( $filepath,
"r" );
481 throw new Exception(
"couldn't open $filepath" );
484 $max_index = $number_desired - 1;
485 while ( !feof( $fh ) ) {
486 $line = fgets( $fh );
487 if (
$line !==
false ) {
490 if ( mt_rand( 0, $line_number ) <= $max_index ) {
496 if ( $line_number < $number_desired ) {
497 throw new Exception(
"not enough lines in $filepath" );