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='.$params[0].'&var2='.$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_'.substr($this->source, 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: $valid = AlphaController::checkSecurityFields();
259:
260:
261: if(!$valid) {
262: $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
263: $bgc = imagecolorallocate($im, 0, 0, 0);
264: imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
265:
266: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
267: header("Content-Type: image/png");
268: imagepng($im);
269: }else{
270: header("Content-Type: image/jpeg");
271: imagejpeg($im);
272: }
273:
274: imagedestroy($im);
275:
276: self::$logger->warn('The client ['.$_SERVER['HTTP_USER_AGENT'].'] was blocked from accessing the file ['.$this->filename.'] due to bad security tokens being provided');
277: }
278: }
279:
280:
281: if($this->scale->getBooleanValue() && isset($_COOKIE['screenSize'])) {
282: $originalScreenResolution = explode('x', $config->get('sysCMSImagesWidgetScreenResolution'));
283: $originalScreenX = $originalScreenResolution[0];
284: $originalScreenY = $originalScreenResolution[1];
285:
286: $targetScreenResolution = explode('x', $_COOKIE['screenSize']);
287: $targetScreenX = $targetScreenResolution[0];
288: $targetScreenY = $targetScreenResolution[1];
289:
290:
291: $xu = $targetScreenX/$originalScreenX;
292: $yu = $targetScreenY/$originalScreenY;
293:
294: $this->width = new Integer(intval($this->width->getValue()*$xu));
295: $this->height = new Integer(intval($this->height->getValue()*$yu));
296:
297:
298: $this->setFilename();
299: }
300:
301:
302: if ($this->checkCache()) {
303: $this->loadCache();
304: }else{
305:
306: switch ($this->sourceType->getValue()) {
307: case "gif":
308: $old_image = imagecreatefromgif($this->source);
309: break;
310: case "jpg":
311: $old_image = imagecreatefromjpeg($this->source);
312: break;
313: case "png":
314: $old_image = imagecreatefrompng($this->source);
315: break;
316: }
317:
318: if (!$old_image) {
319: $im = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
320: $bgc = imagecolorallocate($im, 255, 255, 255);
321: $tc = imagecolorallocate($im, 0, 0, 0);
322: imagefilledrectangle($im, 0, 0, $this->width->getValue(), $this->height->getValue(), $bgc);
323:
324: imagestring($im, 1, 5, 5, "Error loading $this->source", $tc);
325: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
326: header("Content-Type: image/png");
327: imagepng($im);
328: }else{
329: header("Content-Type: image/jpeg");
330: imagejpeg($im);
331: }
332: imagedestroy($im);
333: }else{
334:
335: $oldWidth = imagesx($old_image);
336: $oldHeight = imagesy($old_image);
337:
338:
339: $new_image = imagecreatetruecolor($this->width->getValue(), $this->height->getValue());
340:
341:
342: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
343:
344: imagealphablending($new_image, false);
345:
346:
347: $color = imagecolorallocatealpha($new_image, 255, 0, 0, 0);
348:
349:
350: imagefill($new_image, 0, 0, $color);
351:
352:
353: imagesavealpha($new_image, true);
354: }
355:
356: imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $this->width->getValue(), $this->height->getValue(), $oldWidth, $oldHeight);
357:
358: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png')) {
359: header("Content-Type: image/png");
360: imagepng($new_image, basename($this->filename));
361: }else{
362: header("Content-Type: image/jpeg");
363: imagejpeg($new_image, basename($this->filename), 100*$this->quality->getValue());
364: }
365:
366: $this->cache($new_image);
367: imagedestroy($old_image);
368: imagedestroy($new_image);
369: }
370: }
371: }
372:
373: 374: 375: 376: 377: 378:
379: private function cache($image) {
380: global $config;
381:
382: if($this->sourceType->getValue() == 'png' && $config->get('cms.images.perserve.png'))
383: imagepng($image, $this->filename);
384: else
385: imagejpeg($image, $this->filename, 100*$this->quality->getValue());
386: }
387:
388: 389: 390: 391: 392: 393:
394: private function checkCache() {
395: return file_exists($this->filename);
396: }
397:
398: 399: 400: 401: 402:
403: private function loadCache() {
404: readfile($this->filename);
405: }
406:
407: 408: 409: 410: 411: 412: 413: 414:
415: public static function convertImageURLToPath($imgURL) {
416: global $config;
417:
418: $imgPath = str_replace($config->get('app.url'), '', $imgURL);
419:
420: return $imgPath;
421: }
422: }
423:
424: ?>