generate random triangles (so we can test EXIF orientation, eventually)
authorNeil Kandalgaonkar <neilk@users.mediawiki.org>
Fri, 17 Jun 2011 23:26:45 +0000 (23:26 +0000)
committerNeil Kandalgaonkar <neilk@users.mediawiki.org>
Fri, 17 Jun 2011 23:26:45 +0000 (23:26 +0000)
tests/phpunit/includes/api/RandomImageGenerator.php
tests/phpunit/includes/api/generateRandomImages.php

index 1e88124..75b729f 100644 (file)
@@ -122,7 +122,7 @@ class RandomImageGenerator {
 
        /**
         * Generate data representing an image of random size (within limits),
-        * consisting of randomly colored and sized circles against a random background color
+        * consisting of randomly colored and sized upward pointing triangles against a random background color
         * (This data is used in the writeImage* methods).
         * @return {Mixed}
         */
@@ -140,17 +140,17 @@ class RandomImageGenerator {
                        $radius = mt_rand( 0, $diagonalLength / 4 );
                        $originX = mt_rand( -1 * $radius, $spec['width'] + $radius );
                        $originY = mt_rand( -1 * $radius, $spec['height'] + $radius );
-                       $perimeterX = $originX + $radius;
-                       $perimeterY = $originY + $radius;
+                       $angle = mt_rand( 0, ( 3.141592/2 ) * $radius ) / $radius;
+                       $legDeltaX = round( $radius * sin( $angle ) );
+                       $legDeltaY = round( $radius * cos( $angle ) );
 
                        $draw = array();
                        $draw['fill'] = $this->getRandomColor();
-                       $draw['circle'] = array(
-                               'originX' => $originX,
-                               'originY' => $originY,
-                               'radius' => $radius,
-                               'perimeterX' => $perimeterX,
-                               'perimeterY' => $perimeterY
+                       $draw['shape'] = array(
+                               array( 'x' => $originX,                 'y' => $originY - $radius ),
+                               array( 'x' => $originX + $legDeltaX,    'y' => $originY + $legDeltaY ),
+                               array( 'x' => $originX - $legDeltaX,    'y' => $originY + $legDeltaY ),
+                               array( 'x' => $originX,                 'y' => $originY - $radius )
                        );
                        $draws[] = $draw;
 
@@ -177,12 +177,9 @@ class RandomImageGenerator {
                $svg->addAttribute( 'height', $spec['height'] );
                $g = $svg->addChild( 'g' );
                foreach ( $spec['draws'] as $drawSpec ) {
-                       $circle = $g->addChild( 'circle' );
-                       $circle->addAttribute( 'fill', $drawSpec['fill'] );             
-                       $circleSpec = $drawSpec['circle'];
-                       $circle->addAttribute( 'cx', $circleSpec['originX'] );          
-                       $circle->addAttribute( 'cy', $circleSpec['originY'] );          
-                       $circle->addAttribute( 'r', $circleSpec['radius'] );            
+                       $shape = $g->addChild( 'polygon' );
+                       $shape->addAttribute( 'fill', $drawSpec['fill'] );              
+                       $shape->addAttribute( 'points', shapePointsToString( $drawSpec['shape'] ) );
                };
                if ( ! $fh = fopen( $filename, 'w' ) ) {
                        throw new Exception( "couldn't open $filename for writing" );
@@ -193,6 +190,14 @@ class RandomImageGenerator {
                }
        }
 
+       public function shapePointsToString( $shape ) {
+               $points = array();
+               foreach ( $shape as $point ) { 
+                       $points[] = $point['x'] . ',' . $point['y'];
+               }
+               return join( " ", $points );
+       }
+
        /**
         * Based on an image specification, write such an image to disk, using Imagick PHP extension
         * @param $spec: spec describing background and circles to draw
@@ -206,8 +211,7 @@ class RandomImageGenerator {
                foreach ( $spec['draws'] as $drawSpec ) {
                        $draw = new ImagickDraw();
                        $draw->setFillColor( $drawSpec['fill'] );
-                       $circle = $drawSpec['circle'];
-                       $draw->circle( $circle['originX'], $circle['originY'], $circle['perimeterX'], $circle['perimeterY'] );
+                       $draw->polygon( $drawSpec['shape'] );
                        $image->drawImage( $draw );
                }
 
@@ -221,7 +225,7 @@ class RandomImageGenerator {
         *
         * Sample command line:
         *    $ convert -size 100x60 xc:rgb(90,87,45)  \
-                *      -draw 'fill rgb(12,34,56)   circle 41,39 44,57' \
+                *      -draw 'fill rgb(12,34,56)   polygon 41,39 44,57 50,57 41,39' \
                 *      -draw 'fill rgb(99,123,231) circle 59,39 56,57' \
                 *      -draw 'fill rgb(240,12,32)  circle 50,21 50,3'  filename.png
         *
@@ -236,11 +240,8 @@ class RandomImageGenerator {
                $args[] = wfEscapeShellArg( "xc:" . $spec['fill'] );
                foreach( $spec['draws'] as $draw ) {
                        $fill = $draw['fill'];
-                       $originX = $draw['circle']['originX'];
-                       $originY = $draw['circle']['originY'];
-                       $perimeterX = $draw['circle']['perimeterX'];
-                       $perimeterY = $draw['circle']['perimeterY'];
-                       $drawCommand = "fill $fill  circle $originX,$originY $perimeterX,$perimeterY";
+                       $polygon = shapePointsToString( $draw['shape'] );
+                       $drawCommand = "fill $fill  polygon $polygon";
                        $args[] = '-draw ' . wfEscapeShellArg( $drawCommand );
                }
                $args[] = wfEscapeShellArg( $filename );
index cab1ec7..aca52ac 100644 (file)
@@ -8,7 +8,8 @@ $getOptSpec = array(
        'maxWidth::',
        'minHeight::',
        'maxHeight::',
-       'circlesToDraw::',
+       'shapesToDraw::',
+       'shape::',
 
        'number::',
        'format::'