jarednova /
timber
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Changes image to new size, by shrinking/enlarging |
||
| 4 | * then cropping to respect new ratio. |
||
| 5 | * |
||
| 6 | * Arguments: |
||
| 7 | * - width of new image |
||
| 8 | * - height of new image |
||
| 9 | * - crop method |
||
| 10 | */ |
||
| 11 | class TimberImageOperationResize extends TimberImageOperation { |
||
| 12 | |||
| 13 | private $w, $h, $crop; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param int $w width of new image |
||
| 17 | * @param int $h height of new image |
||
| 18 | * @param string $crop cropping method, one of: 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center'. |
||
| 19 | */ |
||
| 20 | function __construct($w, $h, $crop) { |
||
|
0 ignored issues
–
show
|
|||
| 21 | $this->w = $w; |
||
| 22 | $this->h = $h; |
||
| 23 | // Sanitize crop position |
||
| 24 | $allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center' ); |
||
| 25 | if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) { |
||
| 26 | $crop = $allowed_crop_positions[0]; |
||
| 27 | } |
||
| 28 | $this->crop = $crop; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
||
| 33 | * @param string $src_extension the extension (ex: .jpg) |
||
| 34 | * @return string the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg) |
||
| 35 | */ |
||
| 36 | public function filename($src_filename, $src_extension) { |
||
| 37 | $w = 0; |
||
| 38 | $h = 0; |
||
| 39 | if ( $this->w ) { |
||
| 40 | $w = $this->w; |
||
| 41 | } |
||
| 42 | if ( $this->h ) { |
||
| 43 | $h = $this->h; |
||
| 44 | } |
||
| 45 | $result = $src_filename . '-' . $w . 'x' . $h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false) |
||
| 46 | if($src_extension) { |
||
| 47 | $result .= '.'.$src_extension; |
||
| 48 | } |
||
| 49 | return $result; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $load_filename |
||
| 54 | * @param string $save_filename |
||
| 55 | */ |
||
| 56 | protected function run_animated_gif( $load_filename, $save_filename ) { |
||
| 57 | $image = wp_get_image_editor( $load_filename ); |
||
| 58 | $current_size = $image->get_size(); |
||
| 59 | $src_w = $current_size['width']; |
||
|
0 ignored issues
–
show
$src_w is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 60 | $src_h = $current_size['height']; |
||
|
0 ignored issues
–
show
$src_h is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 61 | $w = $this->w; |
||
| 62 | $h = $this->h; |
||
| 63 | if ( !class_exists('Imagick') ) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | $image = new Imagick($load_filename); |
||
| 67 | $image = $image->coalesceImages(); |
||
| 68 | $crop = self::get_target_sizes( $load_filename ); |
||
| 69 | foreach ($image as $frame) { |
||
| 70 | $frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']); |
||
| 71 | $frame->thumbnailImage($w, $h); |
||
| 72 | $frame->setImagePage($w, $h, 0, 0); |
||
| 73 | } |
||
| 74 | $image = $image->deconstructImages(); |
||
| 75 | return $image->writeImages($save_filename, true); |
||
| 76 | } |
||
| 77 | |||
| 78 | protected function get_target_sizes( $load_filename ) { |
||
| 79 | $image = wp_get_image_editor( $load_filename ); |
||
| 80 | $w = $this->w; |
||
| 81 | $h = $this->h; |
||
| 82 | $crop = $this->crop; |
||
| 83 | |||
| 84 | $current_size = $image->get_size(); |
||
| 85 | $src_w = $current_size['width']; |
||
| 86 | $src_h = $current_size['height']; |
||
| 87 | $src_ratio = $src_w / $src_h; |
||
| 88 | if ( !$h ) { |
||
| 89 | $h = round( $w / $src_ratio ); |
||
| 90 | } |
||
| 91 | if ( !$w ) { |
||
| 92 | //the user wants to resize based on constant height |
||
| 93 | $w = round( $h * $src_ratio ); |
||
| 94 | } |
||
| 95 | if ( !$crop ) { |
||
| 96 | return array( |
||
| 97 | 'x' => 0, 'y' => 0, |
||
| 98 | 'src_w' => $src_w, 'src_h' => $src_h, |
||
| 99 | 'target_w' => $w, 'target_h' => $h |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | // Get ratios |
||
| 103 | $dest_ratio = $w / $h; |
||
| 104 | $src_wt = $src_h * $dest_ratio; |
||
| 105 | $src_ht = $src_w / $dest_ratio; |
||
| 106 | $src_x = $src_w / 2 - $src_wt / 2; |
||
| 107 | $src_y = ( $src_h - $src_ht ) / 6; |
||
| 108 | //now specific overrides based on options: |
||
| 109 | switch ( $crop ) { |
||
| 110 | case 'center': |
||
| 111 | // Get source x and y |
||
| 112 | $src_x = round( ( $src_w - $src_wt ) / 2 ); |
||
| 113 | $src_y = round( ( $src_h - $src_ht ) / 2 ); |
||
| 114 | break; |
||
| 115 | |||
| 116 | case 'top': |
||
| 117 | $src_y = 0; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case 'bottom': |
||
| 121 | $src_y = $src_h - $src_ht; |
||
| 122 | break; |
||
| 123 | |||
| 124 | case 'top-center': |
||
| 125 | $src_y = round( ( $src_h - $src_ht ) / 4 ); |
||
| 126 | break; |
||
| 127 | |||
| 128 | case 'bottom-center': |
||
| 129 | $src_y = $src_h - $src_ht - round( ( $src_h - $src_ht ) / 4 ); |
||
| 130 | break; |
||
| 131 | |||
| 132 | case 'left': |
||
| 133 | $src_x = 0; |
||
| 134 | break; |
||
| 135 | |||
| 136 | case 'right': |
||
| 137 | $src_x = $src_w - $src_wt; |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | // Crop the image |
||
| 141 | return ( $dest_ratio > $src_ratio ) |
||
| 142 | ? array( |
||
| 143 | 'x' => 0, 'y' => $src_y, |
||
| 144 | 'src_w' => $src_w, 'src_h' => $src_ht, |
||
| 145 | 'target_w' => $w, 'target_h' => $h |
||
| 146 | ) |
||
| 147 | : array( |
||
| 148 | 'x' => $src_x, 'y' => 0, |
||
| 149 | 'src_w' => $src_wt, 'src_h' => $src_h, |
||
| 150 | 'target_w' => $w, 'target_h' => $h |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Performs the actual image manipulation, |
||
| 156 | * including saving the target file. |
||
| 157 | * |
||
| 158 | * @param string $load_filename filepath (not URL) to source file |
||
| 159 | * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
||
| 160 | * @param string $save_filename filepath (not URL) where result file should be saved |
||
| 161 | * (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg) |
||
| 162 | * @return bool true if everything went fine, false otherwise |
||
|
0 ignored issues
–
show
|
|||
| 163 | */ |
||
| 164 | public function run($load_filename, $save_filename) { |
||
| 165 | //should be resized by gif resizer |
||
| 166 | if ( TimberImageHelper::is_animated_gif($load_filename) ) { |
||
| 167 | //attempt to resize |
||
| 168 | //return if successful |
||
| 169 | //proceed if not |
||
| 170 | $gif = self::run_animated_gif($load_filename, $save_filename); |
||
| 171 | if ($gif) { |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | $image = wp_get_image_editor( $load_filename ); |
||
| 176 | if ( !is_wp_error( $image ) ) { |
||
| 177 | $crop = self::get_target_sizes( $load_filename ); |
||
| 178 | $image->crop( $crop['x'], |
||
| 179 | $crop['y'], |
||
| 180 | $crop['src_w'], |
||
| 181 | $crop['src_h'], |
||
| 182 | $crop['target_w'], |
||
| 183 | $crop['target_h'] |
||
| 184 | ); |
||
| 185 | $result = $image->save( $save_filename ); |
||
| 186 | View Code Duplication | if ( is_wp_error( $result ) ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 187 | // @codeCoverageIgnoreStart |
||
| 188 | TimberHelper::error_log( 'Error resizing image' ); |
||
| 189 | TimberHelper::error_log( $result ); |
||
| 190 | return false; |
||
| 191 | // @codeCoverageIgnoreEnd |
||
| 192 | } else { |
||
| 193 | return true; |
||
| 194 | } |
||
| 195 | View Code Duplication | } else if ( isset( $image->error_data['error_loading_image'] ) ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 196 | // @codeCoverageIgnoreStart |
||
| 197 | TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] ); |
||
| 198 | } else { |
||
| 199 | TimberHelper::error_log( $image ); |
||
| 200 | // @codeCoverageIgnoreEnd |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.