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 CloudControl\Cms\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) { |
|
|
|
|
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
|
|
|
} |
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.