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 int $x |
19
|
|
|
* @param int $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) { |
42
|
|
|
$ratio = 1; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Calculate the destination width, height, x and y |
46
|
|
|
$this->_destWidth = $originalWidth / $ratio; |
47
|
|
|
$this->_destHeight = $originalHeight / $ratio; |
48
|
|
|
$this->_destX = ($this->_width - $this->_destWidth) / 2; |
49
|
|
|
$this->_destY = ($this->_height - $this->_destHeight) / 2; |
50
|
|
|
|
51
|
|
|
// Define the origial width and height |
52
|
|
|
$originalWidth = imagesx($imageResource); |
53
|
|
|
$originalHeight = imagesy($imageResource); |
54
|
|
|
|
55
|
|
|
// Create the new image |
56
|
|
|
$new = imagecreatetruecolor($this->_width, $this->_height); |
57
|
|
|
|
58
|
|
|
if ($this->_backgroundColor !== null) { |
59
|
|
|
$fill = imagecolorallocate($new, $this->_backgroundColor[0], $this->_backgroundColor[1], |
|
|
|
|
60
|
|
|
$this->_backgroundColor[2]); |
61
|
|
|
imagefill($new, 0, 0, $fill); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Preserve transparency |
65
|
|
|
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
|
|
|
|
66
|
|
|
imagealphablending($new, false); |
|
|
|
|
67
|
|
|
imagesavealpha($new, true); |
|
|
|
|
68
|
|
|
|
69
|
|
|
imagecopyresampled($new, $imageResource, $this->_destX, $this->_destY, $this->_x, $this->_y, |
|
|
|
|
70
|
|
|
$this->_destWidth, $this->_destHeight, $originalWidth, $originalHeight); |
71
|
|
|
|
72
|
|
|
return $new; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |