Completed
Push — master ( f1f579...75c263 )
by Jared
03:05
created

TimberImageOperationResize::filename()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 8.8571
cc 5
eloc 11
nc 16
nop 2
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;
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 1 tabs, found 0
Loading history...
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'.
19
	 */
20
	function __construct($w, $h, $crop) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
		$this->w = $w;
22
		$this->h = $h;
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 ) ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
47
			$result .= '.'.$src_extension;
48
		}
49
		return $result;
50
	}
51
52
	protected function run_animated_gif( $load_filename, $save_filename ) {
53
		$image = wp_get_image_editor( $load_filename );
54
		$current_size = $image->get_size();
55
		$src_w = $current_size['width'];
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
		$src_h = $current_size['height'];
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
		$w = $this->w;
58
		$h = $this->h;
59
		if ( !class_exists('Imagick') ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
60
			return false;
61
		}
62
		$image = new Imagick($load_filename);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
63
		$image = $image->coalesceImages();
64
		$crop = self::get_target_sizes( $load_filename );
65
		foreach ($image as $frame) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
66
			$frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
67
			$frame->thumbnailImage($w, $h);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
68
			$frame->setImagePage($w, $h, 0, 0);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
69
		}
70
		$image = $image->deconstructImages();
71
		return $image->writeImages($save_filename, true);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
72
	}
73
74
	protected function get_target_sizes( $load_filename ) {
75
		$image = wp_get_image_editor( $load_filename );
76
		$w = $this->w;
77
		$h = $this->h;
78
		$crop = $this->crop;
79
80
		$current_size = $image->get_size();
81
		$src_w = $current_size['width'];
82
		$src_h = $current_size['height'];
83
		$src_ratio = $src_w / $src_h;
84
		if ( !$h ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
85
			$h = round( $w / $src_ratio );
86
		}
87
		if ( !$w ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
88
			//the user wants to resize based on constant height
89
			$w = round( $h * $src_ratio );
90
		}
91 View Code Duplication
		if ( !$crop ) {
0 ignored issues
show
Duplication introduced by
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...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
92
			return array(
93
				'x' => 0, 'y' => 0,
94
				'src_w' => $src_w, 'src_h' => $src_h,
95
				'target_w' => $w, 'target_h' => $h
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
96
			);
97
		}
98
		// Get ratios
99
		$dest_ratio = $w / $h;
100
		$src_wt = $src_h * $dest_ratio;
101
		$src_ht = $src_w / $dest_ratio;
102
		$src_x = $src_w / 2 - $src_wt / 2;
103
		$src_y = ( $src_h - $src_ht ) / 6;
104
		//now specific overrides based on options:
105
		if ( $crop == 'center' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
106
			// Get source x and y
107
			$src_x = round( ( $src_w - $src_wt ) / 2 );
108
			$src_y = round( ( $src_h - $src_ht ) / 2 );
109
		} else if ( $crop == 'top' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
110
			$src_y = 0;
111
		} else if ( $crop == 'bottom' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
112
			$src_y = $src_h - $src_ht;
113
		} else if ( $crop == 'left' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
114
			$src_x = 0;
115
		} else if ( $crop == 'right' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
116
			$src_x = $src_w - $src_wt;
117
		}
118
		// Crop the image
119 View Code Duplication
		if ( $dest_ratio > $src_ratio ) {
0 ignored issues
show
Duplication introduced by
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...
120
			return array(
121
				'x' => 0, 'y' => $src_y,
122
				'src_w' => $src_w, 'src_h' => $src_ht,
123
				'target_w' => $w, 'target_h' => $h
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
124
			);
125
		}
126
		return array(
127
			'x' => $src_x, 'y' => 0,
128
			'src_w' => $src_wt, 'src_h' => $src_h,
129
			'target_w' => $w, 'target_h' => $h
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
130
		);
131
	}
132
133
	/**
134
	 * Performs the actual image manipulation,
135
	 * including saving the target file.
136
	 *
137
	 * @param  string $load_filename filepath (not URL) to source file
138
	 *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
139
	 * @param  string $save_filename filepath (not URL) where result file should be saved
140
	 *                               (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg)
141
	 * @return bool                  true if everything went fine, false otherwise
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
142
	 */
143
	public function run($load_filename, $save_filename) {
144
		//should be resized by gif resizer
145
		if ( TimberImageHelper::is_animated_gif($load_filename) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
146
			//attempt to resize
147
			//return if successful
148
			//proceed if not
149
			$gif = self::run_animated_gif($load_filename, $save_filename);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
150
			if ($gif) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
151
				return true;
152
			}
153
		}
154
		$image = wp_get_image_editor( $load_filename );
155
		if ( !is_wp_error( $image ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
156
			$crop = self::get_target_sizes( $load_filename );
157
			$image->crop( 	$crop['x'],
158
							$crop['y'],
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 28.
Loading history...
159
							$crop['src_w'],
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 28.
Loading history...
160
							$crop['src_h'],
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 28.
Loading history...
161
							$crop['target_w'],
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 28.
Loading history...
162
							$crop['target_h']
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 28.
Loading history...
163
			);
164
			$result = $image->save( $save_filename );
165 View Code Duplication
			if ( is_wp_error( $result ) ) {
0 ignored issues
show
Duplication introduced by
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...
166
				// @codeCoverageIgnoreStart
167
				TimberHelper::error_log( 'Error resizing image' );
168
				TimberHelper::error_log( $result );
169
				return false;
170
				// @codeCoverageIgnoreEnd
171
			} else {
172
				return true;
173
			}
174 View Code Duplication
		} else if ( isset( $image->error_data['error_loading_image'] ) ) {
0 ignored issues
show
Duplication introduced by
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...
175
			// @codeCoverageIgnoreStart
176
			TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] );
177
		} else {
178
			TimberHelper::error_log( $image );
179
			// @codeCoverageIgnoreEnd
180
		}
181
	}
182
}
183