1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Resize |
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
|
|
|
use \library\images\IMethod; |
14
|
|
|
|
15
|
|
|
class Resize extends IMethod |
16
|
|
|
{ |
17
|
|
|
protected $_width; |
18
|
|
|
protected $_height; |
19
|
|
|
protected $_preserveAspectRatio = true; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set the width |
23
|
|
|
* |
24
|
|
|
* @param int $width |
25
|
|
|
* @return self |
26
|
|
|
*/ |
27
|
|
|
public function SetWidth($width) |
28
|
|
|
{ |
29
|
|
|
$this->_width = intval($width); |
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the height |
35
|
|
|
* |
36
|
|
|
* @param int $height |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
|
|
public function SetHeight($height) |
40
|
|
|
{ |
41
|
|
|
$this->_height = intval($height); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets wheter or not the aspect ratio of the original |
47
|
|
|
* image needs to preserved |
48
|
|
|
* |
49
|
|
|
* @param bool $bool |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function SetPreserveAspectRatio($bool) |
53
|
|
|
{ |
54
|
|
|
$this->_preserveAspectRatio = (bool) $bool; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function Execute($imageResource) |
59
|
|
|
{ |
60
|
|
|
// Define the origial width and height |
61
|
|
|
$originalWidth = imagesx($imageResource); |
62
|
|
|
$originalHeight = imagesy($imageResource); |
63
|
|
|
|
64
|
|
|
// Define the ratio and adjust the width and height |
65
|
|
|
if ($this->_preserveAspectRatio) { |
66
|
|
|
$ratio = min($this->_width/$originalWidth, $this->_height/$originalHeight); |
67
|
|
|
$this->_width = $originalWidth * $ratio; |
68
|
|
|
$this->_height = $originalHeight * $ratio; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Create the new image |
72
|
|
|
$new = imagecreatetruecolor($this->_width, $this->_height); |
73
|
|
|
|
74
|
|
|
// Preserve transparency |
75
|
|
|
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
76
|
|
|
imagealphablending($new, false); |
77
|
|
|
imagesavealpha($new, true); |
78
|
|
|
|
79
|
|
|
// Do the actual resizing |
80
|
|
|
imagecopyresampled($new, $imageResource, 0, 0, 0, 0, $this->_width, $this->_height, $originalWidth, $originalHeight); |
81
|
|
|
|
82
|
|
|
return $new; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |