1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: namespace MOC\Module\Office\Image;
39: use MOC\Api;
40: use MOC\Generic\Device\Module;
41: use MOC\Module\Office\Image\Resource;
42:
43: 44: 45:
46: class Resize implements Module {
47: 48: 49: 50: 51: 52:
53: public static function InterfaceChangelog() {
54: return Api::Core()->Changelog();
55: }
56:
57: 58: 59: 60: 61: 62:
63: public static function InterfaceDepending() {
64: return Api::Core()->Depending();
65: }
66:
67: 68: 69: 70: 71: 72:
73: public static function InterfaceInstance() {
74: return new Resize();
75: }
76:
77:
78: private $Resource = null;
79:
80: 81: 82: 83: 84:
85: public function UseResource( Resource $Resource ) {
86: $this->Resource = $Resource;
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function PixelRelative( $Width = null, $Height = null ) {
97: $ResourceWidth = $this->GetWidth();
98: $ResourceHeight = $this->GetHeight();
99: if( ( $Width !== null ) || ( $Height !== null ) ) {
100: $Width = ( $Width === null ? ( ( 100 / $ResourceHeight * $Height ) / 100 * $ResourceWidth ) : $Width );
101: $Height = ( $Height === null ? ( ( 100 / $ResourceWidth * $Width ) / 100 * $ResourceHeight ) : $Height );
102: }
103: $Ratio = $ResourceWidth / $ResourceHeight;
104: if( ( $Height * $Ratio ) > $Width ) {
105: $Height = $Width / $Ratio;
106: } else {
107: $Width = $Height * $Ratio;
108: }
109: return $this->PercentAbsolute( round( $Width ), round( $Height ) );
110: }
111:
112: 113: 114: 115: 116: 117: public function PixelAbsolute( $Width = null, $Height = null ) {
118: $ResourceWidth = $this->GetWidth();
119: $ResourceHeight = $this->GetHeight();
120: if( ! ( $Width !== null ) && ( $Height !== null ) ) {
121: $Width = ( $Width === null ? $ResourceWidth : $Width );
122: $Height = ( $Height === null ? $ResourceHeight : $Height );
123: }
124: $Resource = Resource::InterfaceInstance()->Create( $Width, $Height );
125: imagecopyresampled( $Resource->Get(), $this->Resource->Get(), 0, 0, 0, 0, $Width, $Height, $ResourceWidth, $ResourceHeight );
126: return $this;
127: }
128:
129: 130: 131: 132: 133: 134: public function PercentRelative( $Width = null, $Height = null ) {
135: $Width = ( $Width === null ? 1 : $Width );
136: $Height = ( $Height === null ? 1 : $Height );
137: return $this->PixelRelative( $this->GetWidth() * $Width, $this->GetHeight() * $Height );
138: }
139:
140: 141: 142: 143: 144: 145: public function PercentAbsolute( $Width = null, $Height = null ) {
146: $Width = ( $Width === null ? 1 : $Width );
147: $Height = ( $Height === null ? 1 : $Height );
148: return $this->PixelAbsolute( $this->GetWidth() * $Width, $this->GetHeight() * $Height );
149: }
150:
151: 152: 153:
154: public function GetWidth() {
155: return imagesx( $this->Resource->Get() );
156: }
157:
158: 159: 160:
161: public function GetHeight() {
162: return imagesy( $this->Resource->Get() );
163: }
164: }
165: