Index: trunk/extensions/LilyPond/LilyPond.class.php |
— | — | @@ -226,60 +226,57 @@ |
227 | 227 | $width = imagesx( $srcImage ); |
228 | 228 | $height = imagesy( $srcImage ); |
229 | 229 | |
230 | | - $xmin = 0; |
231 | | - $found = false; |
232 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
233 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
234 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
235 | | - if ( $rgb != $bgColour ) { |
236 | | - $xmin = $x; |
237 | | - $found = true; |
238 | | - } |
239 | | - } |
240 | | - } |
| 230 | + $xmin = self::findMin( $width, $height, $srcImage, $bgColour ); |
| 231 | + $xmax = self::findMax( $width, $height, $xmin, $srcImage, $bgColour ); |
241 | 232 | |
242 | | - $xmax = $xmin; |
243 | | - $found = false; |
244 | | - for ( $x = $width -1; $x > $xmin && !$found; $x-- ) { |
245 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
246 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
247 | | - if ( $rgb != $bgColour ) { |
248 | | - $xmax = $x; |
249 | | - $found = true; |
250 | | - } |
251 | | - } |
252 | | - } |
| 233 | + $ymin = self::findMin( $height, $width, $srcImage, $bgColour ); |
| 234 | + $ymax = self::findMax( $height, $width, $ymin, $srcImage, $bgColour ); |
253 | 235 | |
254 | | - $ymin = 0; |
255 | | - $found = false; |
256 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
257 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
| 236 | + $newWidth = $xmax - $xmin + 1; |
| 237 | + $newHeight = $ymax - $ymin + 1; |
| 238 | + |
| 239 | + $dstImage = imagecreatetruecolor( $newWidth, $newHeight ); |
| 240 | + imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight ); |
| 241 | + imagepng( $dstImage, $dest ); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @param $outer int |
| 246 | + * @param $inner int |
| 247 | + * @param $srcImage |
| 248 | + * @param $bgColour |
| 249 | + * @return int |
| 250 | + */ |
| 251 | + static function findMin( $outer, $inner, $srcImage, $bgColour ) { |
| 252 | + for ( $x = 0; $x < $outer; $x++ ) { |
| 253 | + for ( $y = 0; $y < $inner; $y++ ) { |
258 | 254 | $rgb = imagecolorat( $srcImage, $x, $y ); |
259 | 255 | if ( $rgb != $bgColour ) { |
260 | | - $ymin = $y; |
261 | | - $found = true; |
| 256 | + return $x; |
262 | 257 | } |
263 | 258 | } |
264 | 259 | } |
| 260 | + return 0; |
| 261 | + } |
265 | 262 | |
266 | | - $ymax = $ymin; |
267 | | - $found = false; |
268 | | - for ( $y = $height -1; $y > $ymin && !$found; $y-- ) { |
269 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
| 263 | + /** |
| 264 | + * @param $outer int |
| 265 | + * @param $inner int |
| 266 | + * @param $min int |
| 267 | + * @param $srcImage |
| 268 | + * @param $bgColour |
| 269 | + * @return int |
| 270 | + */ |
| 271 | + static function findMax( $outer, $inner, $min, $srcImage, $bgColour ) { |
| 272 | + for ( $x = $outer - 1; $x > $min; $x-- ) { |
| 273 | + for ( $y = 0; $y < $inner; $y++ ) { |
270 | 274 | $rgb = imagecolorat( $srcImage, $x, $y ); |
271 | 275 | if ( $rgb != $bgColour ) { |
272 | | - $ymax = $y; |
273 | | - $found = true; |
| 276 | + return $x; |
274 | 277 | } |
275 | 278 | } |
276 | 279 | } |
277 | | - |
278 | | - $newWidth = $xmax - $xmin + 1; |
279 | | - $newHeight = $ymax - $ymin + 1; |
280 | | - |
281 | | - $dstImage = imagecreatetruecolor( $newWidth, $newHeight ); |
282 | | - imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight ); |
283 | | - imagepng( $dstImage, $dest ); |
| 280 | + return 0; |
284 | 281 | } |
285 | 282 | |
286 | 283 | /** |