Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

SmartCrop   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 4
loc 58
rs 10
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A SetPosition() 0 6 1
B Execute() 4 41 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * SmartCrop
4
 *
5
 * @Author: Jens Kooij
6
 * @Version: 1.0
7
 * @package: JNS MVC
8
 * @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported
9
 */
10
 
11
namespace library\images\methods
12
{
13
	class SmartCrop extends Crop
14
	{
15
		/**
16
		 * Use build-in logic to position the smartcrop
17
		 *
18
		 * @param 	string $x
19
		 * @param 	string $y
20
		 * @return 	self
21
		 */
22
		public function SetPosition($x, $y)
23
		{
24
			$this->SetX($x);
25
			$this->SetY($y);
26
			return $this;
27
		}
28
	
29
		public function Execute($imageResource)
30
		{
31
			// Define the origial width and height
32
			$originalWidth = imagesx($imageResource);
33
			$originalHeight = imagesy($imageResource);
34
			
35
			// Define the ratios
36
			$wRatio = $originalWidth / $this->_width;
37
			$hRatio = $originalHeight / $this->_height;
38
			
39
			// Define which ratio will be used, depending on which is the biggest side
40
			$ratio = $wRatio < $hRatio ? $wRatio : $hRatio;
41
			if ($ratio < 1) $ratio = 1;
42
			
43
			// Calculate the destination width, height, x and y
44
			$this->_destWidth = $originalWidth / $ratio;
45
			$this->_destHeight = $originalHeight / $ratio;
46
			$this->_destX = ($this->_width - $this->_destWidth) / 2;
47
			$this->_destY = ($this->_height - $this->_destHeight) / 2;
48
			
49
			// Define the origial width and height
50
			$originalWidth = imagesx($imageResource);
51
			$originalHeight = imagesy($imageResource);
52
			
53
			// Create the new image
54
			$new = imagecreatetruecolor($this->_width, $this->_height);
55
			
56 View Code Duplication
			if ($this->_backgroundColor !== null) {
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...
57
				$fill = imagecolorallocate($new, $this->_backgroundColor[0], $this->_backgroundColor[1], $this->_backgroundColor[2]);
58
				imagefill($new, 0, 0, $fill);
59
			}
60
61
			// Preserve transparency
62
			imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
63
            imagealphablending($new, false);
64
            imagesavealpha($new, true);
65
			
66
			imagecopyresampled($new, $imageResource, $this->_destX, $this->_destY, $this->_x, $this->_y, $this->_destWidth, $this->_destHeight, $originalWidth, $originalHeight);
67
			
68
			return $new;
69
		}
70
	}
71
}