Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r42228 r42343  
    4040         */
    4141        public static function test( $args = array() ) {
    42                 if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
     42                if ( ! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' ) ) {
    4343                        return false;
     44                }
    4445
    4546                // On some setups GD library does not provide imagerotate() - Ticket #11536
    4647                if ( isset( $args['methods'] ) &&
    4748                        in_array( 'rotate', $args['methods'] ) &&
    48                         ! function_exists('imagerotate') ){
     49                        ! function_exists( 'imagerotate' ) ) {
    4950
    5051                                return false;
     
    6667        public static function supports_mime_type( $mime_type ) {
    6768                $image_types = imagetypes();
    68                 switch( $mime_type ) {
     69                switch ( $mime_type ) {
    6970                        case 'image/jpeg':
    70                                 return ($image_types & IMG_JPG) != 0;
     71                                return ( $image_types & IMG_JPG ) != 0;
    7172                        case 'image/png':
    72                                 return ($image_types & IMG_PNG) != 0;
     73                                return ( $image_types & IMG_PNG ) != 0;
    7374                        case 'image/gif':
    74                                 return ($image_types & IMG_GIF) != 0;
     75                                return ( $image_types & IMG_GIF ) != 0;
    7576                }
    7677
     
    8687         */
    8788        public function load() {
    88                 if ( $this->image )
     89                if ( $this->image ) {
    8990                        return true;
    90 
    91                 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
    92                         return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
     91                }
     92
     93                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
     94                        return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file );
     95                }
    9396
    9497                // Set artificially high because GD uses uncompressed images in memory.
     
    97100                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    98101
    99                 if ( ! is_resource( $this->image ) )
    100                         return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file );
     102                if ( ! is_resource( $this->image ) ) {
     103                        return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
     104                }
    101105
    102106                $size = @getimagesize( $this->file );
    103                 if ( ! $size )
    104                         return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
     107                if ( ! $size ) {
     108                        return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
     109                }
    105110
    106111                if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
     
    125130         */
    126131        protected function update_size( $width = false, $height = false ) {
    127                 if ( ! $width )
     132                if ( ! $width ) {
    128133                        $width = imagesx( $this->image );
    129 
    130                 if ( ! $height )
     134                }
     135
     136                if ( ! $height ) {
    131137                        $height = imagesy( $this->image );
     138                }
    132139
    133140                return parent::update_size( $width, $height );
     
    150157         */
    151158        public function resize( $max_w, $max_h, $crop = false ) {
    152                 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
     159                if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) {
    153160                        return true;
     161                }
    154162
    155163                $resized = $this->_resize( $max_w, $max_h, $crop );
     
    160168                        return true;
    161169
    162                 } elseif ( is_wp_error( $resized ) )
     170                } elseif ( is_wp_error( $resized ) ) {
    163171                        return $resized;
    164 
    165                 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
    166         }
    167 
    168         /**
    169          *
     172                }
     173
     174                return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
     175        }
     176
     177        /**
    170178         * @param int $max_w
    171179         * @param int $max_h
     
    176184                $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
    177185                if ( ! $dims ) {
    178                         return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
     186                        return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
    179187                }
    180188                list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
     
    188196                }
    189197
    190                 return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
     198                return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
    191199        }
    192200
     
    214222         */
    215223        public function multi_resize( $sizes ) {
    216                 $metadata = array();
     224                $metadata  = array();
    217225                $orig_size = $this->size;
    218226
     
    233241                        }
    234242
    235                         $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
     243                        $image     = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    236244                        $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    237245
     
    243251                                if ( ! is_wp_error( $resized ) && $resized ) {
    244252                                        unset( $resized['path'] );
    245                                         $metadata[$size] = $resized;
     253                                        $metadata[ $size ] = $resized;
    246254                                }
    247255                        }
     
    270278                // If destination width/height isn't specified, use same as
    271279                // width/height from source.
    272                 if ( ! $dst_w )
     280                if ( ! $dst_w ) {
    273281                        $dst_w = $src_w;
    274                 if ( ! $dst_h )
     282                }
     283                if ( ! $dst_h ) {
    275284                        $dst_h = $src_h;
     285                }
    276286
    277287                $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
     
    282292                }
    283293
    284                 if ( function_exists( 'imageantialias' ) )
     294                if ( function_exists( 'imageantialias' ) ) {
    285295                        imageantialias( $dst, true );
     296                }
    286297
    287298                imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
     
    294305                }
    295306
    296                 return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file );
     307                return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
    297308        }
    298309
     
    307318         */
    308319        public function rotate( $angle ) {
    309                 if ( function_exists('imagerotate') ) {
     320                if ( function_exists( 'imagerotate' ) ) {
    310321                        $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
    311                         $rotated = imagerotate( $this->image, $angle, $transparency );
     322                        $rotated      = imagerotate( $this->image, $angle, $transparency );
    312323
    313324                        if ( is_resource( $rotated ) ) {
     
    320331                        }
    321332                }
    322                 return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file );
     333                return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file );
    323334        }
    324335
     
    333344         */
    334345        public function flip( $horz, $vert ) {
    335                 $w = $this->size['width'];
    336                 $h = $this->size['height'];
     346                $w   = $this->size['width'];
     347                $h   = $this->size['height'];
    337348                $dst = wp_imagecreatetruecolor( $w, $h );
    338349
    339350                if ( is_resource( $dst ) ) {
    340                         $sx = $vert ? ($w - 1) : 0;
    341                         $sy = $horz ? ($h - 1) : 0;
     351                        $sx = $vert ? ( $w - 1 ) : 0;
     352                        $sy = $horz ? ( $h - 1 ) : 0;
    342353                        $sw = $vert ? -$w : $w;
    343354                        $sh = $horz ? -$h : $h;
     
    349360                        }
    350361                }
    351                 return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file );
     362                return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
    352363        }
    353364
     
    365376
    366377                if ( ! is_wp_error( $saved ) ) {
    367                         $this->file = $saved['path'];
     378                        $this->file      = $saved['path'];
    368379                        $this->mime_type = $saved['mime-type'];
    369380                }
     
    381392                list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
    382393
    383                 if ( ! $filename )
     394                if ( ! $filename ) {
    384395                        $filename = $this->generate_filename( null, null, $extension );
     396                }
    385397
    386398                if ( 'image/gif' == $mime_type ) {
    387                         if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) )
    388                                 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    389                 }
    390                 elseif ( 'image/png' == $mime_type ) {
     399                        if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
     400                                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
     401                        }
     402                } elseif ( 'image/png' == $mime_type ) {
    391403                        // convert from full colors to index colors, like original PNG.
    392                         if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) )
     404                        if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
    393405                                imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
    394 
    395                         if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) )
    396                                 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    397                 }
    398                 elseif ( 'image/jpeg' == $mime_type ) {
    399                         if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) )
    400                                 return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    401                 }
    402                 else {
    403                         return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
     406                        }
     407
     408                        if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
     409                                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
     410                        }
     411                } elseif ( 'image/jpeg' == $mime_type ) {
     412                        if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
     413                                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
     414                        }
     415                } else {
     416                        return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    404417                }
    405418
    406419                // Set correct file permissions
    407                 $stat = stat( dirname( $filename ) );
     420                $stat  = stat( dirname( $filename ) );
    408421                $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
    409422                @ chmod( $filename, $perms );
     
    460473         */
    461474        protected function make_image( $filename, $function, $arguments ) {
    462                 if ( wp_is_stream( $filename ) )
     475                if ( wp_is_stream( $filename ) ) {
    463476                        $arguments[1] = null;
     477                }
    464478
    465479                return parent::make_image( $filename, $function, $arguments );
Note: See TracChangeset for help on using the changeset viewer.