Conditions | 10 |
Paths | 57 |
Total Lines | 80 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
89 | public function trim() |
||
90 | { |
||
91 | // Get the image width and height. |
||
92 | $imw = imagesx($this->sourceImage); |
||
93 | $imh = imagesy($this->sourceImage); |
||
94 | |||
95 | // Set the X variables. |
||
96 | $xmin = $imw; |
||
97 | $xmax = 0; |
||
98 | $ymin = null; |
||
99 | $ymax = null; |
||
100 | |||
101 | // Start scanning for the edges. |
||
102 | for ($iy=0; $iy<$imh; $iy++) |
||
103 | { |
||
104 | $first = true; |
||
105 | |||
106 | for ($ix=0; $ix<$imw; $ix++) |
||
107 | { |
||
108 | $ndx = imagecolorat($this->sourceImage, $ix, $iy); |
||
109 | $colors = $this->helper->getIndexedColors($this->sourceImage, $ndx); |
||
110 | |||
111 | if(!$colors->matchesAlpha($this->trimColor)) |
||
112 | { |
||
113 | if ($xmin > $ix) { $xmin = $ix; } |
||
114 | if ($xmax < $ix) { $xmax = $ix; } |
||
115 | if (!isset($ymin)) { $ymin = $iy; } |
||
116 | |||
117 | $ymax = $iy; |
||
118 | |||
119 | if($first) |
||
120 | { |
||
121 | $ix = $xmax; |
||
122 | $first = false; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // no trimming border found |
||
129 | if($ymax === null) { |
||
130 | return $this->sourceImage; |
||
131 | } |
||
132 | |||
133 | // The new width and height of the image. |
||
134 | $imw = 1+$xmax-$xmin; // Image width in pixels |
||
135 | $imh = 1+$ymax-$ymin; // Image height in pixels |
||
136 | |||
137 | // Make another image to place the trimmed version in. |
||
138 | $im2 = $this->helper->createNewImage($imw, $imh); |
||
139 | |||
140 | if($this->trimColor->hasTransparency()) |
||
141 | { |
||
142 | $bg2 = imagecolorallocatealpha( |
||
143 | $im2, |
||
144 | $this->trimColor->getRed()->get8Bit(), |
||
145 | $this->trimColor->getGreen()->get8Bit(), |
||
146 | $this->trimColor->getBlue()->get8Bit(), |
||
147 | $this->trimColor->getAlpha()->get7Bit() |
||
148 | ); |
||
149 | |||
150 | imagecolortransparent($im2, $bg2); |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | $bg2 = imagecolorallocate( |
||
155 | $im2, |
||
156 | $this->trimColor->getRed()->get8Bit(), |
||
157 | $this->trimColor->getGreen()->get8Bit(), |
||
158 | $this->trimColor->getBlue()->get8Bit() |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | // Make the background of the new image the same as the background of the old one. |
||
163 | imagefill($im2, 0, 0, $bg2); |
||
164 | |||
165 | // Copy it over to the new image. |
||
166 | imagecopy($im2, $this->sourceImage, 0, 0, $xmin, $ymin, $imw, $imh); |
||
167 | |||
168 | return $im2; |
||
169 | } |
||
171 |