TimberImageOperationLetterbox   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 97
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A filename() 0 6 1
B run() 0 58 7
1
<?php
2
/*
3
 * Changes image to new size, by shrinking/enlarging then padding with colored bands,
4
 * so that no part of the image is cropped or stretched.
5
 *
6
 * Arguments:
7
 * - width of new image
8
 * - height of new image
9
 * - color of padding
10
 */
11
class TimberImageOperationLetterbox extends TimberImageOperation {
12
13
	private $w, $h, $color;
14
15
	/**
16
	 * @param int    $w     width of result image
17
	 * @param int    $h     height
18
	 * @param string $color hex string, for color of padding bands
19
	 */
20
	function __construct($w, $h, $color) {
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
		$this->color = $color;
24
	}
25
26
	/**
27
	 * @param   string    $src_filename     the basename of the file (ex: my-awesome-pic)
28
	 * @param   string    $src_extension    the extension (ex: .jpg)
29
	 * @return  string    the final filename to be used
30
	 *                    (ex: my-awesome-pic-lbox-300x200-FF3366.jpg)
31
	 */
32
	public function filename($src_filename, $src_extension) {
33
		$color = str_replace( '#', '', $this->color );
34
		$newbase = $src_filename . '-lbox-' . $this->w . 'x' . $this->h . '-' . $color;
35
		$new_name = $newbase . '.' . $src_extension;
36
		return $new_name;
37
	}
38
39
	/**
40
	 * Performs the actual image manipulation,
41
	 * including saving the target file.
42
	 *
43
	 * @param  string $load_filename filepath (not URL) to source file
44
	 *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
45
	 * @param  string $save_filename filepath (not URL) where result file should be saved
46
	 *                               (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg)
47
	 * @return bool                  true if everything went fine, false otherwise
48
	 */
49
	public function run($load_filename, $save_filename) {
50
		$w = $this->w;
51
		$h = $this->h;
52
53
		$bg = imagecreatetruecolor( $w, $h );
54
		$c = self::hexrgb( $this->color );
55
		$bgColor = imagecolorallocate( $bg, $c['red'], $c['green'], $c['blue'] );
56
		imagefill( $bg, 0, 0, $bgColor );
57
		$image = wp_get_image_editor( $load_filename );
58
		if ( !is_wp_error( $image ) ) {
59
			$current_size = $image->get_size();
60
			$quality = $image->get_quality();
61
			$ow = $current_size['width'];
62
			$oh = $current_size['height'];
63
			$new_aspect = $w / $h;
64
			$old_aspect = $ow / $oh;
65
			if ( $new_aspect > $old_aspect ) {
66
				//taller than goal
67
				$h_scale = $h / $oh;
68
				$owt = $ow * $h_scale;
69
				$y = 0;
70
				$x = $w / 2 - $owt / 2;
71
				$oht = $h;
72
				$image->crop( 0, 0, $ow, $oh, $owt, $oht );
73
			} else {
74
				$w_scale = $w / $ow;
75
				$oht = $oh * $w_scale;
76
				$x = 0;
77
				$y = $h / 2 - $oht / 2;
78
				$owt = $w;
79
				$image->crop( 0, 0, $ow, $oh, $owt, $oht );
80
			}
81
			$result = $image->save( $save_filename );
0 ignored issues
show
Unused Code introduced by
$result 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...
82
			$func = 'imagecreatefromjpeg';
83
			$save_func = 'imagejpeg';
84
			$ext = pathinfo( $save_filename, PATHINFO_EXTENSION );
85
			if ( $ext == 'gif' ) {
86
				$func = 'imagecreatefromgif';
87
				$save_func = 'imagegif';
88
			} else if ( $ext == 'png' ) {
89
				$func = 'imagecreatefrompng';
90
				$save_func = 'imagepng';
91
				if ($quality > 9) {
92
					$quality = $quality/10;
93
					$quality = round(10 - $quality);
94
				}
95
			}
96
			$image = $func( $save_filename );
97
			imagecopy( $bg, $image, $x, $y, 0, 0, $owt, $oht );
98
			if ($save_func === 'imagegif') {
99
				return $save_func( $bg, $save_filename );
100
			}
101
			return $save_func( $bg, $save_filename, $quality );
102
		} else {
103
			TimberHelper::error_log( $image );
104
		}
105
		return false;
106
	}
107
}
108