1: <?php
2: 3: 4: 5: 6:
7:
8: 9: 10: 11: 12: 13: 14: 15: 16:
17: abstract class QImageBase extends QControl {
18:
19:
20:
21:
22:
23: protected $strAlternateText;
24: protected $strImageType = QImageType::Png;
25: protected $intJpegQuality = 100;
26:
27:
28: protected $strCacheFolder = null;
29: protected $strCacheFilename = null;
30:
31:
32: protected $strCachedActualFilePath = null;
33: protected $strControlClassName = 'QImageBase';
34: protected $strImagickTempFilePath = '/tmp';
35:
36:
37:
38:
39: public function ParsePostData() {}
40:
41: 42: 43: 44:
45: public function Validate() {return true;}
46:
47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function __construct($objParentObject, $strControlId = null) {
57: if ($objParentObject)
58: parent::__construct($objParentObject, $strControlId);
59: }
60:
61: public function RenderAsImgSrc($blnDisplayOutput = true) {
62:
63: $strSerialized = $this->Serialize();
64: $strHash = md5($strSerialized);
65:
66:
67: if ($this->strCacheFilename)
68: $strImageFilename = $this->strCacheFilename;
69: else {
70: $strImageFilename = $strHash;
71:
72: switch ($this->strImageType) {
73: case QImageType::Gif:
74: $strImageFilename .= '.gif';
75: break;
76: case QImageType::AnimatedGif:
77: $strImageFilename .= '.gif';
78: break;
79: case QImageType::Jpeg:
80: $strImageFilename .= '.jpg';
81: break;
82: default:
83: $strImageFilename .= '.png';
84: break;
85: }
86: }
87:
88:
89: if ($this->strCacheFolder) {
90: $strFilePath = sprintf('%s%s/%s',
91: __DOCROOT__,
92: str_replace(__VIRTUAL_DIRECTORY__, '', $this->strCacheFolder),
93: $strImageFilename);
94:
95: if (!file_exists($strFilePath))
96: $this->RenderImage($strFilePath);
97:
98: $strPath = sprintf('%s/%s',
99: $this->strCacheFolder,
100: $strImageFilename);
101:
102:
103: $this->strCachedActualFilePath = $strFilePath;
104: } else {
105: $strPath = sprintf('%s/image_base.php/%s/%s?q=%s',
106: __VIRTUAL_DIRECTORY__ . __PHP_ASSETS__,
107: $this->strControlClassName,
108: $strImageFilename,
109: $strSerialized
110: );
111: }
112:
113:
114: if ($blnDisplayOutput)
115: print($strPath);
116: else
117: return $strPath;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: protected function GetControlHtml() {
128: try {
129:
130: $strPath = $this->RenderAsImgSrc(false);
131: } catch (QCallerException $objExc) {
132: $objExc->IncrementOffset();
133: throw $objExc;
134: }
135:
136: if ($this->strCachedActualFilePath) {
137: $objDimensions = getimagesize($this->strCachedActualFilePath);
138:
139:
140:
141: $strBackColor = $this->strBackColor;
142: $strWidth = $this->strWidth;
143: $strHeight = $this->strHeight;
144: $this->strBackColor = null;
145: $this->strWidth = $objDimensions[0];
146: $this->strHeight = $objDimensions[1];
147: $strStyle = $this->GetStyleAttributes();
148: if ($strStyle)
149: $strStyle = sprintf(' style="%s"', $strStyle);
150: $this->strBackColor = $strBackColor;
151: $this->strWidth = $strWidth;
152: $this->strHeight = $strHeight;
153: } else {
154:
155: $strBackColor = $this->strBackColor;
156: $strWidth = $this->strWidth;
157: $strHeight = $this->strHeight;
158: $this->strBackColor = null;
159: $this->strWidth = null;
160: $this->strHeight = null;
161: $strStyle = $this->GetStyleAttributes();
162: if ($strStyle)
163: $strStyle = sprintf(' style="%s"', $strStyle);
164: $this->strBackColor = $strBackColor;
165: $this->strWidth= $strWidth;
166: $this->strHeight = $strHeight;
167: }
168:
169: $strAlt = null;
170: if ($this->strAlternateText)
171: $strAlt = ' alt="' . QApplication::HtmlEntities($this->strAlternateText) . '"';
172:
173:
174: $strToReturn = sprintf('<img src="%s" %s%s%s/>',
175: $strPath,
176: $this->GetAttributes(),
177: $strStyle,
178: $strAlt);
179: return $strToReturn;
180: }
181:
182: public function Serialize() {
183: $objControl = clone($this);
184: $objControl->objForm = null;
185: $objControl->objParentControl = null;
186: $objControl->strCacheFilename = null;
187: $objControl->strCachedActualFilePath = null;
188: $objControl->blnOnPage = null;
189: $objControl->blnModified = null;
190: $objControl->strControlId = null;
191: $objControl->strRenderMethod = null;
192: $objControl->blnOnPage = null;
193: $objControl->blnRendered = null;
194: $objControl->blnRendering = null;
195: $objControl->objActionArray = null;
196: $objControl->objChildControlArray = null;
197: $objControl->blnWrapperModified = null;
198: $objControl->strValidationError = null;
199: $objControl->strWarning = null;
200:
201: $strData = serialize($objControl);
202:
203: if (function_exists('gzcompress'))
204: $strData = base64_encode(gzcompress($strData, 9));
205: else
206: $strData = base64_encode($strData);
207:
208: $strData = str_replace('+', '-', $strData);
209: $strData = str_replace('/', '_', $strData);
210:
211: return $strData;
212: }
213:
214: public static function Run() {
215: $strData = QApplication::QueryString('q');
216: $strData = str_replace('-', '+', $strData);
217: $strData = str_replace('_', '/', $strData);
218:
219: $strData = base64_decode($strData);
220:
221: if (function_exists('gzcompress'))
222: $strData = gzuncompress($strData);
223:
224: $objControl = unserialize($strData);
225: $objControl->RenderImage();
226: }
227:
228: abstract public function RenderImage($strPath = null);
229:
230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
240: protected function RenderImageHelper($objFinalImage, $strPath) {
241:
242: if (!$strPath) {
243:
244: QApplication::$ProcessOutput = false;
245: header('Cache-Control: cache');
246: header('Expires: Wed, 20 Mar 2019 05:00:00 GMT');
247: header('Pragma: cache');
248:
249: switch ($this->strImageType) {
250: case QImageType::Gif:
251: header('Content-Type: image/gif');
252: imagegif($objFinalImage);
253: break;
254: case QImageType::AnimatedGif:
255: header('Content-Type: image/gif');
256: imagegif($objFinalImage);
257: break;
258: case QImageType::Jpeg:
259: header('Content-Type: image/jpeg');
260: imagejpeg($objFinalImage, null, $this->intJpegQuality);
261: break;
262: default:
263: header('Content-Type: image/png');
264: imagepng($objFinalImage);
265: break;
266: }
267: } else {
268:
269: QApplication::MakeDirectory(dirname($strPath), 0777);
270:
271:
272: switch ($this->strImageType) {
273: case QImageType::Gif:
274: imagegif($objFinalImage, $strPath);
275: break;
276: case QImageType::AnimatedGif:
277: imagegif($objFinalImage, $strPath);
278: break;
279: case QImageType::Jpeg:
280: imagejpeg($objFinalImage, $strPath, $this->intJpegQuality);
281: break;
282: default:
283: imagepng($objFinalImage, $strPath);
284: break;
285: }
286: chmod($strPath, 0777);
287: }
288:
289: imagedestroy($objFinalImage);
290: }
291:
292: 293: 294: 295: 296: 297: 298: 299: 300: 301:
302: protected function RenderImageMagickHelper($objFinalImage, $strPath) {
303:
304: if (!$strPath) {
305: $strPath = $this->strImagickTempFilePath . '/image_' . str_replace('.', '_', microtime(true));
306:
307:
308: switch ($this->strImageType) {
309: case QImageType::Gif:
310: $strPath .= '.gif';
311: $objFinalImage->setImageFormat('gif');
312: header('Content-Type: image/gif');
313: break;
314: case QImageType::AnimatedGif:
315: $strPath .= '.gif';
316: $objFinalImage->setImageFormat('gif');
317: header('Content-Type: image/gif');
318: break;
319: case QImageType::Jpeg:
320: $strPath .= '.jpg';
321: $objFinalImage->setImageFormat('jpeg');
322: $objFinalImage->setCompressionQuality($this->intJpegQuality);
323: header('Content-Type: image/jpeg');
324: break;
325: default:
326: $strPath .= '.png';
327: $objFinalImage->setImageFormat('png');
328: header('Content-Type: image/png');
329: break;
330: }
331:
332: if ($this->strImageType == QImageType::AnimatedGif)
333: file_put_contents($strPath, $objFinalImage->GetImagesBlob());
334: else
335: $objFinalImage->writeImage($strPath);
336:
337: QApplication::$ProcessOutput = false;
338: header('Cache-Control: cache');
339: header('Expires: Wed, 20 Mar 2019 05:00:00 GMT');
340: header('Pragma: cache');
341:
342: print(file_get_contents($strPath));
343: unlink($strPath);
344:
345: } else {
346:
347: QApplication::MakeDirectory(dirname($strPath), 0777);
348:
349:
350: switch ($this->strImageType) {
351: case QImageType::Gif:
352: $objFinalImage->setImageFormat('gif');
353: break;
354: case QImageType::AnimatedGif:
355: $objFinalImage->setImageFormat('gif');
356: break;
357: case QImageType::Jpeg:
358: $objFinalImage->setImageFormat('jpeg');
359: $objFinalImage->setCompressionQuality($this->intJpegQuality);
360: break;
361: default:
362: $objFinalImage->setImageFormat('png');
363: break;
364: }
365:
366: $objFinalImage->writeImage($strPath);
367: chmod($strPath, 0777);
368: }
369:
370: $objFinalImage->Destroy();
371: }
372:
373:
374:
375:
376: 377: 378: 379: 380: 381: 382: 383: 384:
385: public function __get($strName) {
386: switch ($strName) {
387:
388: case "CacheFolder": return $this->strCacheFolder;
389: case "CacheFilename": return $this->strCacheFilename;
390: case "AlternateText": return $this->strAlternateText;
391: case "ImageType": return $this->strImageType;
392: case "JpegQuality": return $this->intJpegQuality;
393:
394: default:
395: try {
396: return parent::__get($strName);
397: } catch (QCallerException $objExc) {
398: $objExc->IncrementOffset();
399: throw $objExc;
400: }
401: }
402: }
403:
404:
405:
406:
407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417:
418: public function __set($strName, $mixValue) {
419: $this->blnModified = true;
420:
421: switch ($strName) {
422: case "CacheFolder":
423: try {
424: $this->strCacheFolder = QType::Cast($mixValue, QType::String);
425: break;
426: } catch (QInvalidCastException $objExc) {
427: $objExc->IncrementOffset();
428: throw $objExc;
429: }
430:
431: case "CacheFilename":
432: try {
433: $this->strCacheFilename = QType::Cast($mixValue, QType::String);
434: break;
435: } catch (QInvalidCastException $objExc) {
436: $objExc->IncrementOffset();
437: throw $objExc;
438: }
439:
440: case "AlternateText":
441: try {
442: $this->strAlternateText = QType::Cast($mixValue, QType::String);
443: break;
444: } catch (QInvalidCastException $objExc) {
445: $objExc->IncrementOffset();
446: throw $objExc;
447: }
448:
449: case "ImageType":
450: try {
451: $this->strImageType = QType::Cast($mixValue, QType::String);
452: break;
453: } catch (QInvalidCastException $objExc) {
454: $objExc->IncrementOffset();
455: throw $objExc;
456: }
457:
458: case "JpegQuality":
459: try {
460: $this->intJpegQuality = QType::Cast($mixValue, QType::Integer);
461: break;
462: } catch (QInvalidCastException $objExc) {
463: $objExc->IncrementOffset();
464: throw $objExc;
465: }
466:
467: default:
468: try {
469: parent::__set($strName, $mixValue);
470: } catch (QCallerException $objExc) {
471: $objExc->IncrementOffset();
472: throw $objExc;
473: }
474: break;
475: }
476: }
477: }