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