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: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class Image {
50: 51: 52: 53: 54: 55:
56: private $title;
57:
58: 59: 60: 61: 62: 63:
64: private $source;
65:
66: 67: 68: 69: 70: 71:
72: private $width;
73:
74: 75: 76: 77: 78: 79:
80: private $height;
81:
82: 83: 84: 85: 86: 87:
88: private $sourceType;
89:
90: 91: 92: 93: 94: 95:
96: private $quality;
97:
98: 99: 100: 101: 102: 103: 104:
105: private $scale;
106:
107: 108: 109: 110: 111: 112: 113:
114: private $secure;
115:
116: 117: 118: 119: 120: 121:
122: private $filename;
123:
124: 125: 126: 127: 128: 129:
130: private static $logger = null;
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143:
144: public function __construct($source, $width, $height, $sourceType, $quality=0.75, $scale=false, $secure=false) {
145: self::$logger = new Logger('Image');
146: self::$logger->debug('>>__construct(source=['.$source.'], width=['.$width.'], height=['.$height.'], sourceType=['.$sourceType.'], quality=['.$quality.'], scale=['.$scale.'], secure=['.$secure.'])');
147:
148: global $config;
149:
150: if(file_exists($source))
151: $this->source = $source;
152: else
153: throw new IllegalArguementException('The source file for the Image widget ['.$source.'] cannot be found!');
154:
155: $this->sourceType = new Enum();
156: $this->sourceType->setOptions(array('jpg','png','gif'));
157: $this->sourceType->setValue($sourceType);
158:
159: if($quality < 0.0 || $quality > 1.0)
160: throw new IllegalArguementException('The quality setting of ['.$quality.'] is outside of the allowable range of 0.0 to 1.0');
161:
162: $this->quality = new Double($quality);
163: $this->scale = new Boolean($scale);
164: $this->secure = new Boolean($secure);
165:
166: $this->width = new Integer($width);
167: $this->height = new Integer($height);
168:
169: $this->setFilename();
170:
171: self::$logger->debug('<<__construct');
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181:
182: public function renderHTMLLink($altText='') {
183: global $config;
184:
185: if($this->secure->getBooleanValue()) {
186: $params = AlphaController::generateSecurityFields();
187: return '<img src="'.FrontController::generateSecureURL('act=ViewImage&s='.$this->source.'&w='.$this->width->getValue().'&h='.$this->height->getValue().'&t='.$this->sourceType->getValue().'&q='.$this->quality->getValue().'&sc='.$this->scale->getValue().'&se='.$this->secure->getValue().'&var1='.urlencode($params[0]).'&var2='.urlencode($params[1])).'"'.(empty($altText) ? '' : ' alt="'.$altText.'"').'/>';
188: }else{
189: return '<img src="'.FrontController::generateSecureURL('act=ViewImage&s='.$this->source.'&w='.$this->width->getValue().'&h='.$this->height->getValue().'&t='.$this->sourceType->getValue().'&q='.$this->quality->getValue().'&sc='.$this->scale->getValue().'&se='.$this->secure->getValue()).'"'.(empty($altText) ? '' : ' alt="'.$altText.'"').'/>';
190: }
191: }
192:
193: 194: 195: 196: 197: 198:
199: private function setFilename() {
200: global $config;
201:
202: if(!strpos($this->source, 'attachments/article_')) {
203:
204: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png'))
205: $this->filename = $config->get('app.file.store.dir').'cache/images/'.basename($this->source, ".".$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.png';
206: else
207: $this->filename = $config->get('app.file.store.dir').'cache/images/'.basename($this->source, ".".$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.jpg';
208: }else{
209:
210: $cacheDir = $config->get('app.file.store.dir').'cache/images/article_'.mb_substr($this->source, mb_strpos($this->source, 'attachments/article_')+20, 11);
211: if(!file_exists($cacheDir)) {
212: $success = mkdir($cacheDir);
213:
214: if (!$success) {
215: throw new AlphaException('Unable to create the folder '.$cacheDir.' for the cache image, source file is '.$this->source);
216: }
217:
218:
219: $success = chmod($cacheDir, 0777);
220:
221: if (!$success) {
222: throw new AlphaException('Unable to set write permissions on the folder ['.$cacheDir.'].');
223: }
224: }
225:
226:
227: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png'))
228: $this->filename = $cacheDir.'/'.basename($this->source, ".".$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.png';
229: else
230: $this->filename = $cacheDir.'/'.basename($this->source, ".".$this->sourceType->getValue()).'_'.$this->width->getValue().'x'.$this->height->getValue().'.jpg';
231: }
232:
233: self::$logger->debug('Image filename is ['.$this->filename.']');
234: }
235:
236: 237: 238: 239: 240:
241: public function getFilename() {
242: return $this->filename;
243: }
244:
245: 246: 247: 248: 249:
250: public function renderImage() {
251: global $config;
252:
253: 254: 255: 256:
257: if($config->get('cms.images.widget.secure') && basename($_SERVER["PHP_SELF"]) != 'ViewArticlePDF.php') {
258:
259: $_REQUEST['var1'] = urldecode($_REQUEST['var1']);
260: $_REQUEST['var2'] = urldecode($_REQUEST['var2']);
261: $valid = AlphaController::checkSecurityFields();
262:
263:
264: if(!$valid) {
265: $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
266: $bgc = imagecolorallocate($im, 0, 0, 0);
267: imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
268:
269: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
270: header("Content-Type: image/png");
271: imagepng($im);
272: }else{
273: header("Content-Type: image/jpeg");
274: imagejpeg($im);
275: }
276:
277: imagedestroy($im);
278:
279: self::$logger->warn('The client ['.$_SERVER['HTTP_USER_AGENT'].'] was blocked from accessing the file ['.$this->filename.'] due to bad security tokens being provided');
280: }
281: }
282:
283:
284: if($this->scale->getBooleanValue() && isset($_COOKIE['screenSize'])) {
285: $originalScreenResolution = explode('x', $config->get('sysCMSImagesWidgetScreenResolution'));
286: $originalScreenX = $originalScreenResolution[0];
287: $originalScreenY = $originalScreenResolution[1];
288:
289: $targetScreenResolution = explode('x', $_COOKIE['screenSize']);
290: $targetScreenX = $targetScreenResolution[0];
291: $targetScreenY = $targetScreenResolution[1];
292:
293:
294: $xu = $targetScreenX/$originalScreenX;
295: $yu = $targetScreenY/$originalScreenY;
296:
297: $this->width = new Integer(intval($this->width->getValue()*$xu));
298: $this->height = new Integer(intval($this->height->getValue()*$yu));
299:
300:
301: $this->setFilename();
302: }
303:
304:
305: if ($this->checkCache()) {
306: $this->loadCache();
307: }else{
308:
309: switch ($this->sourceType->getValue()) {
310: case "gif":
311: $old_image = imagecreatefromgif($this->source);
312: break;
313: case "jpg":
314: $old_image = imagecreatefromjpeg($this->source);
315: break;
316: case "png":
317: $old_image = imagecreatefrompng($this->source);
318: break;
319: }
320:
321: if (!$old_image) {
322: $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
323: $bgc = imagecolorallocate($im, 255, 255, 255);
324: $tc = imagecolorallocate($im, 0, 0, 0);
325: imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
326:
327: imagestring($im, 1, 5, 5, "Error loading $this->source", $tc);
328: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
329: header("Content-Type: image/png");
330: imagepng($im);
331: }else{
332: header("Content-Type: image/jpeg");
333: imagejpeg($im);
334: }
335: imagedestroy($im);
336: }else{
337:
338: $oldWidth = imagesx($old_image);
339: $oldHeight = imagesy($old_image);
340:
341:
342: $new_image = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
343:
344:
345: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
346:
347: imagealphablending($new_image, false);
348:
349:
350: $color = imagecolorallocatealpha($new_image, 255, 0, 0, 0);
351:
352:
353: imagefill($new_image, 0, 0, $color);
354:
355:
356: imagesavealpha($new_image, true);
357: }
358:
359: imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $this->width->getValue(), $this->height->getValue(), $oldWidth, $oldHeight);
360:
361: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
362: header("Content-Type: image/png");
363: imagepng($new_image);
364: }else{
365: header("Content-Type: image/jpeg");
366: imagejpeg($new_image, null, 100*$this->quality->getValue());
367: }
368:
369: $this->cache($new_image);
370: imagedestroy($old_image);
371: imagedestroy($new_image);
372: }
373: }
374: }
375:
376: 377: 378: 379: 380: 381:
382: private function cache($image) {
383: global $config;
384:
385: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png'))
386: imagepng($image, $this->filename);
387: else
388: imagejpeg($image, $this->filename, 100*$this->quality->getValue());
389: }
390:
391: 392: 393: 394: 395: 396:
397: private function checkCache() {
398: return file_exists($this->filename);
399: }
400:
401: 402: 403: 404: 405:
406: private function loadCache() {
407: readfile($this->filename);
408: }
409:
410: 411: 412: 413: 414: 415: 416: 417:
418: public static function convertImageURLToPath($imgURL) {
419: global $config;
420:
421: $imgPath = str_replace($config->get('app.url'), '', $imgURL);
422:
423: return $imgPath;
424: }
425: }
426:
427: ?>