@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | $this->w = $w; |
| 22 | 22 | $this->h = $h; |
| 23 | 23 | // Sanitize crop position |
| 24 | - $allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right' ); |
|
| 25 | - if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) { |
|
| 24 | + $allowed_crop_positions = array('default', 'center', 'top', 'bottom', 'left', 'right'); |
|
| 25 | + if ( $crop !== false && !in_array($crop, $allowed_crop_positions) ) { |
|
| 26 | 26 | $crop = $allowed_crop_positions[0]; |
| 27 | 27 | } |
| 28 | 28 | $this->crop = $crop; |
@@ -34,15 +34,15 @@ discard block |
||
| 34 | 34 | * @return string the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg) |
| 35 | 35 | */ |
| 36 | 36 | public function filename($src_filename, $src_extension) { |
| 37 | - $result = $src_filename . '-' . $this->w . 'x' . $this->h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false) |
|
| 38 | - if($src_extension) { |
|
| 39 | - $result .= '.'.$src_extension; |
|
| 37 | + $result = $src_filename . '-' . $this->w . 'x' . $this->h . '-c-' . ($this->crop ? $this->crop : 'f'); // Crop will be either user named or f (false) |
|
| 38 | + if ( $src_extension ) { |
|
| 39 | + $result .= '.' . $src_extension; |
|
| 40 | 40 | } |
| 41 | 41 | return $result; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - protected function run_animated_gif( $load_filename, $save_filename ) { |
|
| 45 | - $image = wp_get_image_editor( $load_filename ); |
|
| 44 | + protected function run_animated_gif($load_filename, $save_filename) { |
|
| 45 | + $image = wp_get_image_editor($load_filename); |
|
| 46 | 46 | $current_size = $image->get_size(); |
| 47 | 47 | $src_w = $current_size['width']; |
| 48 | 48 | $src_h = $current_size['height']; |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | } |
| 54 | 54 | $image = new Imagick($load_filename); |
| 55 | 55 | $image = $image->coalesceImages(); |
| 56 | - $crop = self::get_target_sizes( $load_filename ); |
|
| 56 | + $crop = self::get_target_sizes($load_filename); |
|
| 57 | 57 | foreach ($image as $frame) { |
| 58 | 58 | $frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']); |
| 59 | 59 | $frame->thumbnailImage($w, $h); |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | return $image->writeImages($save_filename, true); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - protected function get_target_sizes( $load_filename ) { |
|
| 67 | - $image = wp_get_image_editor( $load_filename ); |
|
| 66 | + protected function get_target_sizes($load_filename) { |
|
| 67 | + $image = wp_get_image_editor($load_filename); |
|
| 68 | 68 | $w = $this->w; |
| 69 | 69 | $h = $this->h; |
| 70 | 70 | $crop = $this->crop; |
@@ -74,11 +74,11 @@ discard block |
||
| 74 | 74 | $src_h = $current_size['height']; |
| 75 | 75 | $src_ratio = $src_w / $src_h; |
| 76 | 76 | if ( !$h ) { |
| 77 | - $h = round( $w / $src_ratio ); |
|
| 77 | + $h = round($w / $src_ratio); |
|
| 78 | 78 | } |
| 79 | 79 | if ( !$w ) { |
| 80 | 80 | //the user wants to resize based on constant height |
| 81 | - $w = round( $h * $src_ratio ); |
|
| 81 | + $w = round($h * $src_ratio); |
|
| 82 | 82 | } |
| 83 | 83 | if ( !$crop ) { |
| 84 | 84 | return array( |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | $src_wt = $src_h * $dest_ratio; |
| 93 | 93 | $src_ht = $src_w / $dest_ratio; |
| 94 | 94 | $src_x = $src_w / 2 - $src_wt / 2; |
| 95 | - $src_y = ( $src_h - $src_ht ) / 6; |
|
| 95 | + $src_y = ($src_h - $src_ht) / 6; |
|
| 96 | 96 | //now specific overrides based on options: |
| 97 | 97 | if ( $crop == 'center' ) { |
| 98 | 98 | // Get source x and y |
| 99 | - $src_x = round( ( $src_w - $src_wt ) / 2 ); |
|
| 100 | - $src_y = round( ( $src_h - $src_ht ) / 2 ); |
|
| 99 | + $src_x = round(($src_w - $src_wt) / 2); |
|
| 100 | + $src_y = round(($src_h - $src_ht) / 2); |
|
| 101 | 101 | } else if ( $crop == 'top' ) { |
| 102 | 102 | $src_y = 0; |
| 103 | 103 | } else if ( $crop == 'bottom' ) { |
@@ -139,35 +139,35 @@ discard block |
||
| 139 | 139 | //return if successful |
| 140 | 140 | //proceed if not |
| 141 | 141 | $gif = self::run_animated_gif($load_filename, $save_filename); |
| 142 | - if ($gif) { |
|
| 142 | + if ( $gif ) { |
|
| 143 | 143 | return true; |
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | - $image = wp_get_image_editor( $load_filename ); |
|
| 147 | - if ( !is_wp_error( $image ) ) { |
|
| 148 | - $crop = self::get_target_sizes( $load_filename ); |
|
| 149 | - $image->crop( $crop['x'], |
|
| 146 | + $image = wp_get_image_editor($load_filename); |
|
| 147 | + if ( !is_wp_error($image) ) { |
|
| 148 | + $crop = self::get_target_sizes($load_filename); |
|
| 149 | + $image->crop($crop['x'], |
|
| 150 | 150 | $crop['y'], |
| 151 | 151 | $crop['src_w'], |
| 152 | 152 | $crop['src_h'], |
| 153 | 153 | $crop['target_w'], |
| 154 | 154 | $crop['target_h'] |
| 155 | 155 | ); |
| 156 | - $result = $image->save( $save_filename ); |
|
| 157 | - if ( is_wp_error( $result ) ) { |
|
| 156 | + $result = $image->save($save_filename); |
|
| 157 | + if ( is_wp_error($result) ) { |
|
| 158 | 158 | // @codeCoverageIgnoreStart |
| 159 | - TimberHelper::error_log( 'Error resizing image' ); |
|
| 160 | - TimberHelper::error_log( $result ); |
|
| 159 | + TimberHelper::error_log('Error resizing image'); |
|
| 160 | + TimberHelper::error_log($result); |
|
| 161 | 161 | return false; |
| 162 | 162 | // @codeCoverageIgnoreEnd |
| 163 | 163 | } else { |
| 164 | 164 | return true; |
| 165 | 165 | } |
| 166 | - } else if ( isset( $image->error_data['error_loading_image'] ) ) { |
|
| 166 | + } else if ( isset($image->error_data['error_loading_image']) ) { |
|
| 167 | 167 | // @codeCoverageIgnoreStart |
| 168 | - TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] ); |
|
| 168 | + TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']); |
|
| 169 | 169 | } else { |
| 170 | - TimberHelper::error_log( $image ); |
|
| 170 | + TimberHelper::error_log($image); |
|
| 171 | 171 | // @codeCoverageIgnoreEnd |
| 172 | 172 | } |
| 173 | 173 | } |