create images with orientation (had to use exiv2 hack, imagemagick not helpful)
authorNeil Kandalgaonkar <neilk@users.mediawiki.org>
Tue, 21 Jun 2011 03:40:53 +0000 (03:40 +0000)
committerNeil Kandalgaonkar <neilk@users.mediawiki.org>
Tue, 21 Jun 2011 03:40:53 +0000 (03:40 +0000)
tests/phpunit/includes/api/RandomImageGenerator.php
tests/phpunit/includes/api/generateRandomImages.php

index 121b2d5..639bca4 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Because MediaWiki tests the uniqueness of media upload content, and filenames, it is sometimes useful to generate
  * files that are guaranteed (or at least very likely) to be unique in both those ways.
- * This generates a number of filenames with random names and random content (colored circles)
+ * This generates a number of filenames with random names and random content (colored triangles)
  *
  * It is also useful to have fresh content because our tests currently run in a "destructive" mode, and don't create a fresh new wiki for each
  * test run.
@@ -31,11 +31,45 @@ class RandomImageGenerator {
        private $maxWidth = 800;
        private $minHeight = 400;
        private $maxHeight = 800;
-       private $circlesToDraw = 5;
+       private $shapesToDraw = 5;
        private $imageWriteMethod;
 
+       /**
+        * Orientations: 0th row, 0th column, EXIF orientation code, rotation 2x2 matrix that is opposite of orientation
+        * n.b. we do not handle the 'flipped' orientations, which is why there is no entry for 2, 4, 5, or 7. Those
+        * seem to be rare in real images anyway
+        * (we also would need a non-symmetric shape for the images to test those, like a letter F)
+        */
+       private static $orientations = array(
+               array(  
+                       '0thRow' => 'top',      
+                       '0thCol' => 'left',     
+                       'exifCode' => 1,        
+                       'counterRotation' => array( array( 1, 0 ), array( 0, 1 ) )              
+               ),
+               array( 
+                       '0thRow' => 'bottom',
+                       '0thCol' => 'right',    
+                       'exifCode' => 3,        
+                       'counterRotation' => array( array( -1, 0 ), array( 0, -1 ) )            
+               ),
+               array( 
+                       '0thRow' => 'right', 
+                       '0thCol' => 'top',              
+                       'exifCode' => 6,        
+                       'counterRotation' => array( array( 0, 1 ), array( 1, 0 ) )      
+               ),
+               array( 
+                       '0thRow' => 'left',     
+                       '0thCol' => 'bottom',   
+                       'exifCode' => 8,        
+                       'counterRotation' => array( array( 0, -1 ), array( -1, 0 ) )            
+               )
+       );
+
+
        public function __construct( $options = array() ) {
-               foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight', 'maxHeight', 'circlesToDraw' ) as $property ) {
+               foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight', 'maxHeight', 'shapesToDraw' ) as $property ) {
                        if ( isset( $options[$property] ) ) {
                                $this->$property = $options[$property];
                        }
@@ -136,8 +170,11 @@ class RandomImageGenerator {
                $diagonalLength = sqrt( pow( $spec['width'], 2 ) + pow( $spec['height'], 2 ) );
 
                $draws = array();
-               for ( $i = 0; $i <= $this->circlesToDraw; $i++ ) {
+               for ( $i = 0; $i <= $this->shapesToDraw; $i++ ) {
                        $radius = mt_rand( 0, $diagonalLength / 4 );
+                       if ( $radius == 0 ) {
+                               continue;
+                       }
                        $originX = mt_rand( -1 * $radius, $spec['width'] + $radius );
                        $originY = mt_rand( -1 * $radius, $spec['height'] + $radius );
                        $angle = mt_rand( 0, ( 3.141592/2 ) * $radius ) / $radius;
@@ -179,7 +216,7 @@ class RandomImageGenerator {
        /**
         * Based on image specification, write a very simple SVG file to disk.
         * Ignores the background spec because transparency is cool. :)
-        * @param $spec: spec describing background and circles to draw
+        * @param $spec: spec describing background and shapes to draw
         * @param $format: file format to write (which is obviously always svg here)
         * @param $filename: filename to write to
         */
@@ -211,7 +248,20 @@ class RandomImageGenerator {
         * @param $filename: filename to write to
         */
        public function writeImageWithApi( $spec, $format, $filename ) {
+               // this is a hack because I can't get setImageOrientation() to work. See below. 
+               global $wgExiv2Command;
+
                $image = new Imagick();
+               /**
+                * If the format is 'jpg', will also add a random orientation -- the image will be drawn rotated with triangle points 
+                * facing in some direction (0, 90, 180 or 270 degrees) and a countering rotation should turn the triangle points upward again
+                */
+               $orientation = self::$orientations[0]; // default is normal orientation
+               if ( $format == 'jpg' ) {
+                       $orientation = self::$orientations[ array_rand( self::$orientations ) ];
+                       $spec = self::rotateImageSpec( $spec, $orientation['counterRotation'] ); 
+               }
+                       
                $image->newImage( $spec['width'], $spec['height'], new ImagickPixel( $spec['fill'] ) );
 
                foreach ( $spec['draws'] as $drawSpec ) {
@@ -222,7 +272,81 @@ class RandomImageGenerator {
                }
 
                $image->setImageFormat( $format );
+
+               // this doesn't work, even though it's documented to do so...
+               // $image->setImageOrientation( $orientation['exifCode'] );
+
                $image->writeImage( $filename );
+
+               // because the above setImageOrientation call doesn't work... nor can I get an external imagemagick binary to do this either...
+               // hacking this for now (only works if you have exiv2 installed, a program to read and manipulate exif)
+               if ( $wgExiv2Command ) {
+                       $cmd = wfEscapeShellArg( $wgExiv2Command )
+                               . " -M "
+                               . wfEscapeShellArg( "set Exif.Image.Orientation " . $orientation['exifCode'] )
+                               . " " 
+                               . wfEscapeShellArg( $filename );
+
+                       $retval = 0;
+                       $err = wfShellExec( $cmd, $retval );
+                       if ( $retval !== 0 ) {
+                               print "Error with $cmd: $retval, $err\n";
+                       }
+                }
+
+
+       }
+
+       /**
+        * Given an image specification, produce rotated version
+        * This is used when simulating a rotated image capture with EXIF orientation
+        * @param $spec Object returned by getImageSpec 
+        * @param $matrix 2x2 transformation matrix
+        * @return transformed Spec
+        */
+       private static function rotateImageSpec( &$spec, $matrix ) {
+               $tSpec = array();
+               $dims = self::matrixMultiply2x2( $matrix, $spec['width'], $spec['height'] );
+               $correctionX = 0;
+               $correctionY = 0;
+               if ( $dims['x'] < 0 ) {
+                       $correctionX = abs( $dims['x'] );
+               } 
+               if ( $dims['y'] < 0 ) { 
+                       $correctionY = abs( $dims['y'] );
+               }
+               $tSpec['width'] = abs( $dims['x'] );
+               $tSpec['height'] = abs( $dims['y'] );
+               $tSpec['fill'] = $spec['fill'];
+               $tSpec['draws'] = array();
+               foreach( $spec['draws'] as $draw ) {
+                       $tDraw = array( 
+                               'fill' => $draw['fill'],
+                               'shape' => array()
+                       );
+                       foreach( $draw['shape'] as $point ) {
+                               $tPoint = self::matrixMultiply2x2( $matrix, $point['x'], $point['y'] );
+                               $tPoint['x'] += $correctionX;
+                               $tPoint['y'] += $correctionY;
+                               $tDraw['shape'][] = $tPoint;
+                       }
+                       $tSpec['draws'][] = $tDraw;
+               }
+               return $tSpec;
+       }
+
+       /**
+        * Given a matrix and a pair of images, return new position
+        * @param $matrix: 2x2 rotation matrix 
+        * @param $x: x-coordinate number
+        * @param $y: y-coordinate number
+        * @return Array transformed with properties x, y 
+        */
+       private static function matrixMultiply2x2( $matrix, $x, $y ) {
+               return array( 
+                       'x' => $x * $matrix[0][0] + $y * $matrix[0][1],
+                       'y' => $x * $matrix[1][0] + $y * $matrix[1][1]
+               );
        }
 
 
@@ -235,7 +359,7 @@ class RandomImageGenerator {
                 *      -draw 'fill rgb(99,123,231) circle 59,39 56,57' \
                 *      -draw 'fill rgb(240,12,32)  circle 50,21 50,3'  filename.png
         *
-        * @param $spec: spec describing background and circles to draw
+        * @param $spec: spec describing background and shapes to draw
         * @param $format: file format to write (unused by this method but kept so it has the same signature as writeImageWithApi)
         * @param $filename: filename to write to
         */
index aca52ac..f3a14e5 100644 (file)
@@ -1,26 +1,47 @@
 <?php
+/**
+ * Bootstrapping for test image file generation
+ *
+ * @file
+ */
 
+// Evaluate the include path relative to this file
+$IP = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
+
+// Start up MediaWiki in command-line mode
+require_once( "$IP/maintenance/Maintenance.php" );
 require("RandomImageGenerator.php");
 
-$getOptSpec = array(
-       'dictionaryFile::',
-       'minWidth::',
-       'maxWidth::',
-       'minHeight::',
-       'maxHeight::',
-       'shapesToDraw::',
-       'shape::',
+class GenerateRandomImages extends Maintenance {
+
+       public function execute() {
+
+               $getOptSpec = array(
+                       'dictionaryFile::',
+                       'minWidth::',
+                       'maxWidth::',
+                       'minHeight::',
+                       'maxHeight::',
+                       'shapesToDraw::',
+                       'shape::',
+
+                       'number::',
+                       'format::'
+               );
+               $options = getopt( null, $getOptSpec );
+
+               $format = isset( $options['format'] ) ? $options['format'] : 'jpg';
+               unset( $options['format'] );
+
+               $number = isset( $options['number'] ) ? intval( $options['number'] ) : 10;
+               unset( $options['number'] );
 
-       'number::',
-       'format::'
-);
-$options = getopt( null, $getOptSpec );
+               $randomImageGenerator = new RandomImageGenerator( $options );
+               $randomImageGenerator->writeImages( $number, $format );
+       }
+}
 
-$format = isset( $options['format'] ) ? $options['format'] : 'jpg';
-unset( $options['format'] );
+$maintClass = 'GenerateRandomImages';
+require( RUN_MAINTENANCE_IF_MAIN );
 
-$number = isset( $options['number'] ) ? intval( $options['number'] ) : 10;
-unset( $options['number'] );
 
-$randomImageGenerator = new RandomImageGenerator( $options );
-$randomImageGenerator->writeImages( $number, $format );