Make WordPress Core

Ticket #26823: 26823.diff

File 26823.diff, 19.1 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/class-wp-image-editor-gd.php

     
    205205                $orig_size = $this->size;
    206206
    207207                foreach ( $sizes as $size => $size_data ) {
    208                         if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )
     208                        if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) )
    209209                                continue;
    210210
    211                         if ( ! isset( $size_data['crop'] ) )
     211                        if ( ! isset( $size_data['width'] ) ) {
     212                                $size_data['width'] = 0;
     213                        }
     214
     215                        if ( ! isset( $size_data['height'] ) ) {
     216                                $size_data['height'] = 0;
     217                        }
     218
     219                        if ( ! isset( $size_data['crop'] ) ) {
    212220                                $size_data['crop'] = false;
     221                        }
    213222
    214223                        $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    215224
  • src/wp-includes/class-wp-image-editor-imagick.php

     
    272272                        if ( ! $this->image )
    273273                                $this->image = $orig_image->getImage();
    274274
    275                         if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )
     275                        if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) )
    276276                                continue;
    277277
     278                        if ( ! isset( $size_data['width'] ) ) {
     279                                $size_data['width'] = 0;
     280                        }
     281
     282                        if ( ! isset( $size_data['height'] ) ) {
     283                                $size_data['height'] = 0;
     284                        }
     285
    278286                        if ( ! isset( $size_data['crop'] ) )
    279287                                $size_data['crop'] = false;
    280288
  • tests/phpunit/tests/image/editor_gd.php

     
    1818
    1919        /**
    2020         * Check support for GD compatible mime types.
    21          * 
     21         *
    2222         */
    2323        public function test_supports_mime_type() {
    2424                $gd_image_editor = new WP_Image_Editor_GD( null );
     
    3030
    3131        /**
    3232         * Test resizing an image, not using crop
    33          *
    3433         */
    3534        public function test_resize() {
     35                $file = DIR_TESTDATA . '/images/waffles.jpg';
     36                $gd_image_editor = new WP_Image_Editor_GD( $file );
     37                $gd_image_editor->load();
     38                $gd_image_editor->resize( 100, 50 );
    3639
    37                 $file = DIR_TESTDATA . '/images/gradient-square.jpg';
     40                $this->assertEquals( array( 'width' => 75, 'height' => 50 ), $gd_image_editor->get_size() );
     41        }
    3842
     43        /**
     44         * Test resizing an multi resizong image, not using crop
     45         */
     46        public function test_multi_resize() {
     47                $file = DIR_TESTDATA . '/images/waffles.jpg';
    3948                $gd_image_editor = new WP_Image_Editor_GD( $file );
    4049                $gd_image_editor->load();
    4150
    42                 $gd_image_editor->resize( 100, 50 );
     51                $sizes_array = array( array('width' => 50, 'height' => 50 ) );
     52                $resized = $gd_image_editor->multi_resize( $sizes_array );
    4353
    44                 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() );
     54                $expected_array = array( array(
     55                        'file' => 'waffles-50x33.jpg',
     56                        'width' => 50,
     57                        'height' => 33,
     58                        'mime-type' => 'image/jpeg'
     59                ) );
     60
     61                $this->assertEquals( $expected_array, $resized );
    4562        }
    4663
    4764        /**
     65         * Test resizing an multi resizong image, and then load the image to check size
     66         */
     67        public function test_multi_resize_check_is_resized() {
     68                $file = DIR_TESTDATA . '/images/waffles.jpg';
     69                $gd_image_editor = new WP_Image_Editor_GD( $file );
     70                $gd_image_editor->load();
     71
     72                $sizes_array = array( array('width' => 50, 'height' => 50 ) );
     73                $resized = $gd_image_editor->multi_resize( $sizes_array );
     74
     75                $gd_image_output = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/'. $resized[0]['file'] );
     76                $gd_image_output->load();
     77
     78                $this->assertEquals( array( 'width' => 50, 'height' => 33  ), $gd_image_output->get_size() );
     79        }
     80
     81        /**
     82         * Test resizing an multi resizong image array in array out
     83         */
     84        public function test_multi_resize_array() {
     85                $file = DIR_TESTDATA . '/images/waffles.jpg';
     86                $gd_image_editor = new WP_Image_Editor_GD( $file );
     87                $gd_image_editor->load();
     88
     89                $sizes_array = array(
     90                // #1 - resizes to 100x100 pixel, square-cropped image
     91                array('width' => 10, 'height' => 10, 'crop' => false ),
     92                // #2 - resizes to 200 pixel max width/height, non-cropped image
     93                array('width' => 75, 'height' => 50, 'crop' => true ),
     94                // #3 - resizes to 200 pixel max height, non-cropped image
     95                array('width' => 9999, 'height' => 20, 'crop' => false ),
     96                // #3 - resizes to 450 pixel max width, non-cropped image
     97                array('width' => 45, 'height' => 9999, 'crop' => true )
     98            );
     99
     100            $resized = $gd_image_editor->multi_resize( $sizes_array );
     101
     102            $expected_array = array(
     103                // #1 - resizes to 100x100 pixel, square-cropped image
     104                array(
     105                                'file' => 'waffles-10x6.jpg',
     106                                'width' => 10,
     107                                'height' => 6 ,
     108                                'mime-type' => 'image/jpeg'
     109                        ),
     110                // #2 - resizes to 200 pixel max width/height, square-cropped image
     111                array(
     112                                'file' => 'waffles-75x50.jpg',
     113                                'width' => 75,
     114                                'height' => 50,
     115                                'mime-type' => 'image/jpeg'
     116                        ),
     117                // #3 - resizes to 200 pixel max height, non-cropped image
     118                array(
     119                                'file' => 'waffles-30x20.jpg',
     120                                'width' => 30,
     121                                'height' => 20,
     122                                'mime-type' => 'image/jpeg'
     123                        ),
     124                // #4 - resizes to 450 pixel max width, non-cropped image
     125                array(
     126                                'file' => 'waffles-45x400.jpg',
     127                                'width' => 45,
     128                                'height' => 400,
     129                                'mime-type' => 'image/jpeg'
     130                        )
     131            );
     132                $this->assertEquals( $expected_array, $resized );
     133        }
     134
     135        /**
     136         * Test resizing an image, no height
     137         */
     138        public function test_multi_resize_mising_height() {
     139                $file = DIR_TESTDATA . '/images/waffles.jpg';
     140                $gd_image_editor = new WP_Image_Editor_GD( $file );
     141                $gd_image_editor->load();
     142                $sizes_array = array( array( 'width' => 50 ) );
     143                $resized = $gd_image_editor->multi_resize( $sizes_array );
     144
     145                $expected_array = array( array(
     146                        'file' => 'waffles-50x33.jpg',
     147                        'width' => 50,
     148                        'height' => 33,
     149                        'mime-type' => 'image/jpeg'
     150                ) );
     151                $this->assertEquals( $expected_array, $resized );
     152        }
     153        /**
     154         * Test resizing an multi resize image, no width
     155         */
     156        public function test_multi_resize_mising_width() {
     157                $file = DIR_TESTDATA . '/images/waffles.jpg';
     158                $gd_image_editor = new WP_Image_Editor_GD( $file );
     159                $gd_image_editor->load();
     160
     161                $sizes_array = array( array( 'height' => 50 ) );
     162                $resized = $gd_image_editor->multi_resize( $sizes_array );
     163
     164                $expected_array = array( array(
     165                        'file' => 'waffles-75x50.jpg',
     166                        'width' => 75,
     167                        'height' => 50,
     168                        'mime-type' => 'image/jpeg'
     169                ) );
     170                $this->assertEquals( $expected_array, $resized );
     171        }
     172
     173        /**
     174         * Test resizing an multi resize image, null input
     175         */
     176        public function test_multi_resize_width_set_to_null() {
     177                $file = DIR_TESTDATA . '/images/waffles.jpg';
     178                $gd_image_editor = new WP_Image_Editor_GD( $file );
     179                $gd_image_editor->load();
     180
     181                $sizes_array = array( array( 'height' => 50,'width' => null ) );
     182                $resized =  $gd_image_editor->multi_resize( $sizes_array );
     183
     184                $expected_array = array( array(
     185                        'file' => 'waffles-75x50.jpg',
     186                        'width' => 75,
     187                        'height' => 50,
     188                        'mime-type' => 'image/jpeg'
     189                ) );
     190                $this->assertEquals( $expected_array, $resized );
     191        }
     192        /**
     193         * Test resizing an multi resize image, Null import
     194         */
     195        public function test_multi_resize_height_set_to_null() {
     196                $file = DIR_TESTDATA . '/images/waffles.jpg';
     197                $gd_image_editor = new WP_Image_Editor_GD( $file );
     198                $gd_image_editor->load();
     199
     200                $sizes_array = array( array( 'height' => null,'width' => 50 ) );
     201                $resized = $gd_image_editor->multi_resize( $sizes_array );
     202
     203                $expected_array = array( array(
     204                        'file' => 'waffles-50x33.jpg',
     205                        'width' => 50,
     206                        'height' => 33,
     207                        'mime-type' => 'image/jpeg'
     208                ) );
     209                $this->assertEquals( $expected_array, $resized );
     210        }
     211
     212        /**
     213         * Test resizing an multi resize image, missus value
     214         */
     215        public function test_multi_resize_width_set_to_missus() {
     216                $file = DIR_TESTDATA . '/images/waffles.jpg';
     217                $gd_image_editor = new WP_Image_Editor_GD( $file );
     218                $gd_image_editor->load();
     219
     220                $sizes_array = array( array( 'height' => 50,'width' => -50 ) );
     221                $resized = $gd_image_editor->multi_resize( $sizes_array );
     222
     223                $expected_array = array( array(
     224                        'file' => 'waffles-75x50.jpg',
     225                        'width' => 75,
     226                        'height' => 50,
     227                        'mime-type' => 'image/jpeg'
     228                ) );
     229                $this->assertEquals( $expected_array, $resized );
     230        }
     231        /**
     232         * Test resizing an multi resize image, missus value
     233         */
     234        public function test_multi_resize_height_set_to_missus() {
     235                $file = DIR_TESTDATA . '/images/waffles.jpg';
     236                $gd_image_editor = new WP_Image_Editor_GD( $file );
     237                $gd_image_editor->load();
     238
     239                $sizes_array = array( array( 'height' => -65119,'width' => 200 ) );
     240                $resized = $gd_image_editor->multi_resize( $sizes_array );
     241
     242                $expected_array = array( array(
     243                        'file' => 'waffles-200x133.jpg',
     244                        'width' => 200,
     245                        'height' => 133,
     246                        'mime-type' => 'image/jpeg'
     247                ) );
     248                $this->assertEquals( $expected_array, $resized );
     249        }
     250
     251        /**
    48252         * Test resizing an image including cropping
    49          * 
     253         *
    50254         */
    51255        public function test_resize_and_crop() {
    52 
    53256                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    54 
    55257                $gd_image_editor = new WP_Image_Editor_GD( $file );
    56258                $gd_image_editor->load();
    57 
    58259                $gd_image_editor->resize( 100, 50, true );
    59260
    60261                $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $gd_image_editor->get_size() );
     
    64265         * Test cropping an image
    65266         */
    66267        public function test_crop() {
    67 
    68268                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    69 
    70269                $gd_image_editor = new WP_Image_Editor_GD( $file );
    71270                $gd_image_editor->load();
    72 
    73271                $gd_image_editor->crop( 0, 0, 50, 50 );
    74272
    75273                $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() );
    76 
    77274        }
    78275
    79276        /**
     
    80277         * Test rotating an image 180 deg
    81278         */
    82279        public function test_rotate() {
    83 
    84280                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    85 
    86281                $gd_image_editor = new WP_Image_Editor_GD( $file );
    87282                $gd_image_editor->load();
    88283
     
    100295         * Test flipping an image
    101296         */
    102297        public function test_flip() {
    103 
    104298                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    105 
    106299                $gd_image_editor = new WP_Image_Editor_GD( $file );
    107300                $gd_image_editor->load();
    108301
     
    118311
    119312        /**
    120313         * Test the image created with WP_Image_Edior_GD preserves alpha when resizing
    121          * 
     314         *
    122315         * @ticket 23039
    123316         */
    124317        public function test_image_preserves_alpha_on_resize() {
    125 
    126318                $file = DIR_TESTDATA . '/images/transparent.png';
    127 
    128319                $editor = wp_get_image_editor( $file );
    129320                $editor->load();
    130321                $editor->resize(5,5);
     322
    131323                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    132                
    133324                $editor->save( $save_to_file );
    134325
    135326                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
     327        }
    136328
    137         }
    138        
    139329        /**
    140330         * Test the image created with WP_Image_Edior_GD preserves alpha with no resizing etc
    141          * 
     331         *
    142332         * @ticket 23039
    143333         */
    144334        public function test_image_preserves_alpha() {
    145 
    146335                $file = DIR_TESTDATA . '/images/transparent.png';
    147 
    148336                $editor = wp_get_image_editor( $file );
    149337                $editor->load();
    150338
    151339                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    152                
    153340                $editor->save( $save_to_file );
    154341
    155342                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
    156343        }
    157 
    158 }
     344}
     345 No newline at end of file
  • tests/phpunit/tests/image/editor_imagick.php

     
    2525
    2626        /**
    2727         * Check support for Image Magick compatible mime types.
    28          * 
     28         *
    2929         */
    3030        public function test_supports_mime_type() {
    3131
     
    3838
    3939        /**
    4040         * Test resizing an image, not using crop
    41          * 
     41         *
    4242         */
    4343        public function test_resize() {
    4444
     
    5151
    5252                $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() );
    5353        }
     54        /**
     55         * Test resizing an multi resizong image, not using crop
     56         */
     57        public function test_multi_resize() {
     58                $file = DIR_TESTDATA . '/images/waffles.jpg';
     59                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     60                $gd_image_editor->load();
    5461
     62                $sizes_array = array( array( 'width' => 50, 'height' => 50 ) );
     63                $resized = $gd_image_editor->multi_resize( $sizes_array );
     64
     65                $expected_array = array( array(
     66                        'file' => 'waffles-50x33.jpg',
     67                        'width' => 50,
     68                        'height' => 33,
     69                        'mime-type' => 'image/jpeg'
     70                ) );
     71                $this->assertEquals( $expected_array, $resized );
     72        }
     73
    5574        /**
     75         * Test resizing an multi resizong image, and then load the image to check size
     76         */
     77        public function test_multi_resize_check_is_resized() {
     78                $file = DIR_TESTDATA . '/images/waffles.jpg';
     79                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     80                $gd_image_editor->load();
     81
     82                $sizes_array = array( array('width' => 50, 'height' => 50 ) );
     83                $resized = $gd_image_editor->multi_resize( $sizes_array );
     84
     85                $gd_image_output = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/'. $resized[0]['file'] );
     86                $gd_image_output->load();
     87                $this->assertEquals( array( 'width' => 50, 'height' => 33 ), $gd_image_output->get_size() );
     88        }
     89
     90        /**
     91         * Test resizing an multi resizong image array in array out
     92         */
     93        public function test_multi_resize_array() {
     94                $file = DIR_TESTDATA . '/images/waffles.jpg';
     95                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     96                $gd_image_editor->load();
     97
     98                $sizes_array = array(
     99                // #1 - resizes to 100x100 pixel, square-cropped image
     100                array('width' => 10, 'height' => 10, 'crop' => false ),
     101                // #2 - resizes to 200 pixel max width/height, non-cropped image
     102                array('width' => 75, 'height' => 50, 'crop' => true ),
     103                // #3 - resizes to 200 pixel max height, non-cropped image
     104                array('width' => 9999, 'height' => 20, 'crop' => false ),
     105                // #4 - resizes to 450 pixel max width, non-cropped image
     106                array('width' => 45, 'height' => 9999, 'crop' => true )
     107            );
     108
     109            $resized = $gd_image_editor->multi_resize( $sizes_array );
     110               
     111            $expected_array = array(
     112                // #1 - resizes to 100x100 pixel, square-cropped image
     113                array(
     114                                'file' => 'waffles-10x6.jpg',
     115                                'width' => 10,
     116                                'height' => 6 ,
     117                                'mime-type' => 'image/jpeg'
     118                        ),
     119                // #2 - resizes to 200 pixel max width/height, square-cropped image
     120                array(
     121                                'file' => 'waffles-75x50.jpg',
     122                                'width' => 75,
     123                                'height' => 50,
     124                                'mime-type' => 'image/jpeg'
     125                        ),
     126                // #3 - resizes to 200 pixel max height, non-cropped image
     127                array(
     128                                'file' => 'waffles-30x20.jpg',
     129                                'width' => 30,
     130                                'height' => 20,
     131                                'mime-type' => 'image/jpeg'
     132                        ),
     133                // #4 - resizes to 450 pixel max width, non-cropped image
     134                array(
     135                                'file' => 'waffles-45x400.jpg',
     136                                'width' => 45,
     137                                'height' => 400,
     138                                'mime-type' => 'image/jpeg'
     139                        )
     140            );
     141                $this->assertEquals( $expected_array, $resized );
     142        }
     143
     144        /**
     145         * Test resizing an image, no height
     146         */
     147        public function test_multi_resize_mising_height() {
     148                $file = DIR_TESTDATA . '/images/waffles.jpg';
     149                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     150                $gd_image_editor->load();
     151
     152                $sizes_array = array( array( 'width' => 50 ) );
     153                $resized = $gd_image_editor->multi_resize( $sizes_array );
     154
     155                $expected_array = array( array(
     156                        'file' => 'waffles-50x33.jpg',
     157                        'width' => 50,
     158                        'height' => 33,
     159                        'mime-type' => 'image/jpeg'
     160                ) );
     161                $this->assertEquals( $expected_array, $resized );
     162        }
     163        /**
     164         * Test resizing an multi resize image, no width
     165         */
     166        public function test_multi_resize_mising_width() {
     167                $file = DIR_TESTDATA . '/images/waffles.jpg';
     168                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     169                $gd_image_editor->load();
     170
     171                $sizes_array = array( array( 'height' => 50 ) );
     172                $resized = $gd_image_editor->multi_resize( $sizes_array );
     173
     174                $expected_array = array( array(
     175                        'file' => 'waffles-75x50.jpg',
     176                        'width' => 75,
     177                        'height' => 50,
     178                        'mime-type' => 'image/jpeg'
     179                ) );
     180                $this->assertEquals( $expected_array, $resized );
     181        }
     182
     183        /**
     184         * Test resizing an multi resize image, null input
     185         */
     186        public function test_multi_resize_width_set_to_null() {
     187                $file = DIR_TESTDATA . '/images/waffles.jpg';
     188                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     189                $gd_image_editor->load();
     190
     191                $sizes_array = array( array( 'height' => 50, 'width' => null ) );
     192                $resized = $gd_image_editor->multi_resize( $sizes_array );
     193
     194                $expected_array = array( array(
     195                        'file' => 'waffles-75x50.jpg',
     196                        'width' => 75,
     197                        'height' => 50,
     198                        'mime-type' => 'image/jpeg'
     199                ) );
     200                $this->assertEquals( $expected_array, $resized );
     201        }
     202        /**
     203         * Test resizing an multi resize image, Null import
     204         */
     205        public function test_multi_resize_height_set_to_null() {
     206                $file = DIR_TESTDATA . '/images/waffles.jpg';
     207                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     208                $gd_image_editor->load();
     209
     210                $sizes_array = array( array( 'height' => null, 'width' => 50 ) );
     211                $resized = $gd_image_editor->multi_resize( $sizes_array );
     212
     213                $expected_array = array( array(
     214                        'file' => 'waffles-50x33.jpg',
     215                        'width' => 50,
     216                        'height' => 33,
     217                        'mime-type' => 'image/jpeg'
     218                ) );
     219                $this->assertEquals( $expected_array, $resized );
     220        }
     221
     222        /**
     223         * Test resizing an multi resize image, missus value
     224         *
     225         */
     226        public function test_multi_resize_width_set_to_missus() {
     227                $file = DIR_TESTDATA . '/images/waffles.jpg';
     228                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     229                $gd_image_editor->load();
     230
     231                $sizes_array = array( array( 'height' => 50, 'width' => -50 ) );
     232                $resized = $gd_image_editor->multi_resize( $sizes_array );
     233
     234                $expected_array = array( array(
     235                        'file' => 'waffles-75x50.jpg',
     236                        'width' => 75,
     237                        'height' => 50,
     238                        'mime-type' => 'image/jpeg'
     239                ) );
     240
     241                $this->assertEquals( $expected_array, $resized );
     242        }
     243        /**
     244         * Test resizing an multi resize image, missus value
     245         */
     246        public function test_multi_resize_height_set_to_missus() {
     247                $file = DIR_TESTDATA . '/images/waffles.jpg';
     248                $gd_image_editor = new WP_Image_Editor_Imagick( $file );
     249                $gd_image_editor->load();
     250
     251                $sizes_array = array( array( 'height' => -65119, 'width' => 200 ) );
     252                $resized = $gd_image_editor->multi_resize( $sizes_array );
     253
     254                $expected_array = array( array(
     255                        'file' => 'waffles-200x133.jpg',
     256                        'width' => 200,
     257                        'height' => 133,
     258                        'mime-type' => 'image/jpeg'
     259                ) );
     260                $this->assertEquals( $expected_array, $resized );
     261        }
     262
     263        /**
    56264         * Test resizing an image including cropping
    57          * 
     265         *
    58266         */
    59267        public function test_resize_and_crop() {
    60268
     
    137345                $editor->load();
    138346                $editor->resize(5,5);
    139347                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    140                
     348
    141349                $editor->save( $save_to_file );
    142350
    143351                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
    144352
    145353        }
    146        
     354
    147355        /**
    148356         * Test the image created with WP_Image_Edior_Imagick preserves alpha with no resizing etc
    149357         *
     
    157365                $editor->load();
    158366
    159367                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    160                
     368
    161369                $editor->save( $save_to_file );
    162370
    163371                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );