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: class WsImage
26: {
27: 28: 29: 30:
31: private $_has_gd_extension;
32:
33: 34: 35: 36:
37: private $_image;
38:
39: 40: 41: 42:
43: private $_imagefile;
44:
45:
46: public function __construct()
47: {
48:
49: if (!extension_loaded('gd')) {
50: if (!dl('gd.so')) {
51: $this->_has_gd_extension = false;
52: return;
53: }
54: }
55:
56: $this->_has_gd_extension = true;
57: }
58:
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: public function read($file)
69: {
70: if (!$this->_has_gd_extension) {
71: return false;
72: }
73:
74:
75: $this->_imagefile = WsROOT.'/'.$file;
76: $handle = @fopen($this->_imagefile, 'rb');
77: if ($handle === false) {
78: return false;
79: }
80:
81:
82: $data = '';
83: while (!feof($handle)) {
84: $data .= fread($handle, 4192);
85: }
86: fclose($handle);
87:
88:
89: $this->_image = imagecreatefromstring($data);
90:
91:
92: unset($data, $handle);
93:
94: if ($this->_image === false) {
95: return false;
96: } else {
97: return true;
98: }
99: }
100:
101:
102: 103: 104: 105:
106: public function show()
107: {
108: if (!$this->_has_gd_extension or $this->_image === false) {
109: return false;
110: }
111:
112:
113: $uid = uniqid('wsimg_');
114: $temp_file = WsROOT.'/runtime/'.$uid.basename($this->_imagefile);
115: $temp_url = WsSERVER_ROOT.'/runtime/'.$uid.basename($this->_imagefile);
116:
117: imagepng($this->_image, $temp_file);
118:
119:
120: echo '<img src="'.$temp_url.'"/>';
121:
122:
123: unset($temp_url, $temp_file, $uid);
124: }
125:
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: public function showThumbnail($width = 100, $height = 100, $text = '')
136: {
137: if (!$this->_has_gd_extension or $this->_image === false) {
138: return false;
139: }
140:
141: list($orig_width, $orig_height) = getimagesize($this->_imagefile);
142: $thumb_image = imagecreatetruecolor($width, $height);
143:
144: imagecopyresampled($thumb_image, $this->_image, 0, 0, 0, 0,
145: $width, $height, $orig_width, $orig_height
146: );
147:
148: ob_start();
149: imagepng($thumb_image);
150: $i = ob_get_clean();
151: imagedestroy($thumb_image);
152:
153:
154: $uid = uniqid('wsimg_');
155: $temp_file = WsROOT.'/runtime/'.$uid.basename($this->_imagefile);
156: $temp_url = WsSERVER_ROOT.'/runtime/'.$uid.basename($this->_imagefile);
157:
158: imagepng($this->_image, $temp_file);
159:
160:
161: $img_id = uniqid('thumb_'.round(rand()));
162:
163:
164: echo '<img id="'.$img_id.'" class="img_thumb" src="data:image/png;base64,'
165: .base64_encode($i).'"/>';
166: if (trim($text) != '') {
167: echo '<p style="align: center;">'.$text.'</p>';
168: }
169:
170:
171: echo '<script type="text/javascript">'.PHP_EOL;
172: echo 'var '.$img_id.'=document.getElementById("'.$img_id.'");'.PHP_EOL;
173: echo $img_id.'.onclick = function() {'.PHP_EOL;
174: echo 'window.ws_thumb_show = WsViewThumbnail("'
175: .$temp_url.'");'.PHP_EOL;
176: echo '}'.PHP_EOL;
177: echo '</script>'.PHP_EOL;
178:
179:
180: unset($i, $thumb_image,
181: $orig_height, $orig_width, $temp_url, $temp_file
182: );
183: }
184:
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195:
196: public function addWatermark(
197: $text, $x = 8, $y = 8 , $font = null, $size = 11
198: )
199: {
200: if (!$this->_has_gd_extension or $this->_image === false) {
201: return false;
202: }
203:
204:
205: if (trim($text) === '') {
206: return false;
207: }
208:
209: if ($font == null) {
210: $font = WsROOT.'/public/fonts/FreeSans.ttf';
211: } else {
212: $font = WsROOT.'/'.$font;
213: }
214:
215:
216: $bbox = imagettfbbox($size, 0, $font, 'ky');
217: $y -= $bbox[5];
218:
219: $black = imagecolorallocate($this->_image, 0, 0, 0);
220: $white = imagecolorallocate($this->_image, 255, 255, 255);
221: imagettftext($this->_image, $size, 0, $x+1, $y+1, $black, $font, $text);
222: imagettftext($this->_image, $size, 0, $x, $y+1, $black, $font, $text);
223: imagettftext($this->_image, $size, 0, $x, $y, $white, $font, $text);
224:
225: unset($white, $black, $bbox);
226: }
227:
228:
229: 230: 231: 232: 233: 234:
235: public function getWidth()
236: {
237: if (!$this->_has_gd_extension or $this->_image === false) {
238: return false;
239: }
240:
241: $width = imagesx($this->_image);
242:
243: return $width;
244: }
245:
246:
247: 248: 249: 250: 251: 252:
253: public function getHeight()
254: {
255: if (!$this->_has_gd_extension or $this->_image === false) {
256: return false;
257: }
258:
259: $height = imagesy($this->_image);
260:
261: return $height;
262: }
263: }
264: