00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 class tslib_gifBuilder extends t3lib_stdGraphic {
00103
00104
00105 var $im = '';
00106 var $w = 0;
00107 var $h = 0;
00108 var $map;
00109 var $workArea;
00110 var $setup = Array ();
00111 var $combinedTextStrings = array();
00112 var $combinedFileNames = array();
00113 var $data = Array();
00114 var $objBB = Array();
00115 var $myClassName = 'gifbuilder';
00116 var $charRangeMap=array();
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 function start($conf,$data) {
00130
00131 if (is_array($conf)) {
00132 $this->setup = $conf;
00133 $this->data = $data;
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'])) {
00145 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'] as $_funcRef) {
00146 $_params = $this->setup;
00147 $this->setup = t3lib_div::callUserFunction($_funcRef,$_params,$this);
00148 }
00149 }
00150
00151
00152 $this->charRangeMap = array();
00153 if (is_array($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'])) {
00154 foreach($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'] as $cRMcfgkey => $cRMcfg) {
00155 if (is_array($cRMcfg)) {
00156
00157
00158 $cRMkey = $GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'][substr($cRMcfgkey,0,-1)];
00159 $this->charRangeMap[$cRMkey] = array();
00160 $this->charRangeMap[$cRMkey]['charMapConfig'] = $cRMcfg['charMapConfig.'];
00161 $this->charRangeMap[$cRMkey]['cfgKey'] = substr($cRMcfgkey,0,-1);
00162 $this->charRangeMap[$cRMkey]['multiplicator'] = (double)$cRMcfg['fontSizeMultiplicator'];
00163 $this->charRangeMap[$cRMkey]['pixelSpace'] = intval($cRMcfg['pixelSpaceFontSizeRef']);
00164 }
00165 }
00166 }
00167
00168
00169 $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
00170
00171
00172 if ($conf['backColor.'] || $conf['backColor']) {
00173 $cObj =t3lib_div::makeInstance('tslib_cObj');
00174 $cObj->start($this->data);
00175 $this->setup['backColor'] = trim($cObj->stdWrap($this->setup['backColor'], $this->setup['backColor.']));
00176 }
00177 if (!$this->setup['backColor']) { $this->setup['backColor']='white'; }
00178
00179 if ($conf['transparentColor.'] || $conf['transparentColor']) {
00180 $cObj =t3lib_div::makeInstance('tslib_cObj');
00181 $cObj->start($this->data);
00182 $this->setup['transparentColor_array'] = explode('|', trim($cObj->stdWrap($this->setup['transparentColor'], $this->setup['transparentColor.'])));
00183 }
00184
00185
00186
00187 if (($this->setup['transparentBackground'] || is_array($this->setup['transparentColor_array'])) && ($this->gifExtension=='gif' || !$this->png_truecolor || isset($this->setup['reduceColors']))) {
00188 $this->truecolor = false;
00189 }
00190
00191
00192 if (!$this->setup['XY']) {$this->setup['XY']='120,50';}
00193
00194
00195
00196
00197 foreach($sKeyArray as $theKey) {
00198 $theValue = $this->setup[$theKey];
00199
00200 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
00201
00202 switch($theValue) {
00203 case 'TEXT':
00204 if ($this->setup[$theKey.'.'] = $this->checkTextObj($conf)) {
00205
00206
00207 if ($this->setup[$theKey.'.']['maxWidth']) {
00208 $this->setup[$theKey.'.']['fontSize'] = $this->fontResize($this->setup[$theKey.'.']);
00209 }
00210
00211
00212 $txtInfo=$this->calcBBox($this->setup[$theKey.'.']);
00213 $this->setup[$theKey.'.']['BBOX'] = $txtInfo;
00214 $this->objBB[$theKey] = $txtInfo;
00215 $this->setup[$theKey.'.']['imgMap'] = 0;
00216 }
00217 break;
00218 case 'IMAGE':
00219 $fileInfo = $this->getResource($conf['file'],$conf['file.']);
00220 if ($fileInfo) {
00221 $this->combinedFileNames[] = preg_replace('/\.[[:alnum:]]+$/','',basename($fileInfo[3]));
00222 $this->setup[$theKey.'.']['file'] = $fileInfo[3];
00223 $this->setup[$theKey.'.']['BBOX'] = $fileInfo;
00224 $this->objBB[$theKey] = $fileInfo;
00225 if ($conf['mask']) {
00226 $maskInfo = $this->getResource($conf['mask'],$conf['mask.']);
00227 if ($maskInfo) {
00228 $this->setup[$theKey.'.']['mask'] = $maskInfo[3];
00229 } else {
00230 $this->setup[$theKey.'.']['mask'] = '';
00231 }
00232 }
00233 } else {
00234 unset($this->setup[$theKey.'.']);
00235 }
00236 break;
00237 }
00238
00239 if ($conf['if.']) {
00240 $cObj =t3lib_div::makeInstance('tslib_cObj');
00241 $cObj->start($this->data);
00242
00243 if (!$cObj->checkIf($conf['if.'])) {
00244 unset($this->setup[$theKey]);
00245 unset($this->setup[$theKey.'.']);
00246 }
00247 }
00248 }
00249 }
00250
00251
00252 $this->setup['XY'] = $this->calcOffset($this->setup['XY']);
00253 $this->setup['offset'] = $this->calcOffset($this->setup['offset']);
00254 $this->setup['workArea'] = $this->calcOffset($this->setup['workArea']);
00255
00256 foreach ($sKeyArray as $theKey) {
00257 $theValue=$this->setup[$theKey];
00258
00259 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
00260 switch($theValue) {
00261 case 'TEXT':
00262 case 'IMAGE':
00263 if ($this->setup[$theKey.'.']['offset']) {
00264 $this->setup[$theKey.'.']['offset'] = $this->calcOffset($this->setup[$theKey.'.']['offset']);
00265 }
00266 break;
00267 case 'BOX':
00268 if ($this->setup[$theKey.'.']['dimensions']) {
00269 $this->setup[$theKey.'.']['dimensions'] = $this->calcOffset($this->setup[$theKey.'.']['dimensions']);
00270 }
00271 break;
00272 case 'WORKAREA':
00273 if ($this->setup[$theKey.'.']['set']) {
00274 $this->setup[$theKey.'.']['set'] = $this->calcOffset($this->setup[$theKey.'.']['set']);
00275 }
00276 break;
00277 case 'CROP':
00278 if ($this->setup[$theKey.'.']['crop']) {
00279 $this->setup[$theKey.'.']['crop'] = $this->calcOffset($this->setup[$theKey.'.']['crop']);
00280 }
00281 break;
00282 case 'SCALE':
00283 if ($this->setup[$theKey.'.']['width']) {
00284 $this->setup[$theKey.'.']['width'] = $this->calcOffset($this->setup[$theKey.'.']['width']);
00285 }
00286 if ($this->setup[$theKey.'.']['height']) {
00287 $this->setup[$theKey.'.']['height'] = $this->calcOffset($this->setup[$theKey.'.']['height']);
00288 }
00289 break;
00290 case 'ELLIPSE':
00291 if ($this->setup[$theKey . '.']['dimensions']) {
00292 $this->setup[$theKey . '.']['dimensions'] = $this->calcOffset($this->setup[$theKey . '.']['dimensions']);
00293 }
00294 break;
00295 }
00296 }
00297 }
00298
00299 $XY = t3lib_div::intExplode(',',$this->setup['XY']);
00300 $maxWidth = intval($this->setup['maxWidth']);
00301 $maxHeight = intval($this->setup['maxHeight']);
00302
00303 $XY[0] = t3lib_div::intInRange($XY[0],1, $maxWidth?$maxWidth:2000);
00304 $XY[1] = t3lib_div::intInRange($XY[1],1, $maxHeight?$maxHeight:2000);
00305 $this->XY = $XY;
00306 $this->w = $XY[0];
00307 $this->h = $XY[1];
00308 $this->OFFSET = t3lib_div::intExplode(',',$this->setup['offset']);
00309
00310 $this->setWorkArea($this->setup['workArea']);
00311 $this->defaultWorkArea = $this->workArea;
00312 }
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 function gifBuild() {
00324 if ($this->setup) {
00325 $gifFileName = $this->fileName('GB/');
00326 if (!file_exists($gifFileName)) {
00327
00328
00329 $this->createTempSubDir('GB/');
00330
00331
00332 $this->make();
00333 $this->output($gifFileName);
00334 $this->destroy();
00335 }
00336 return $gifFileName;
00337 }
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 function make() {
00352
00353 $XY = $this->XY;
00354
00355
00356 $this->im = imagecreatetruecolor($XY[0], $XY[1]);
00357 $this->w = $XY[0];
00358 $this->h = $XY[1];
00359
00360
00361 $BGcols = $this->convertColor($this->setup['backColor']);
00362 $Bcolor = ImageColorAllocate($this->im, $BGcols[0],$BGcols[1],$BGcols[2]);
00363 ImageFilledRectangle($this->im, 0, 0, $XY[0], $XY[1], $Bcolor);
00364
00365
00366 if (is_array($this->setup)) {
00367 $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
00368 foreach($sKeyArray as $theKey) {
00369 $theValue=$this->setup[$theKey];
00370
00371 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
00372 switch($theValue) {
00373
00374 case 'IMAGE':
00375 if ($conf['mask']) {
00376 $this->maskImageOntoImage($this->im,$conf,$this->workArea);
00377 } else {
00378 $this->copyImageOntoImage($this->im,$conf,$this->workArea);
00379 }
00380 break;
00381
00382
00383 case 'TEXT':
00384 if (!$conf['hide']) {
00385 if (is_array($conf['shadow.'])) {
00386 $this->makeShadow($this->im,$conf['shadow.'],$this->workArea,$conf);
00387 }
00388 if (is_array($conf['emboss.'])) {
00389 $this->makeEmboss($this->im,$conf['emboss.'],$this->workArea,$conf);
00390 }
00391 if (is_array($conf['outline.'])) {
00392 $this->makeOutline($this->im,$conf['outline.'],$this->workArea,$conf);
00393 }
00394 $conf['imgMap']=1;
00395 $this->makeText($this->im,$conf,$this->workArea);
00396 }
00397 break;
00398
00399
00400 case 'OUTLINE':
00401 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
00402 $this->makeOutline($this->im,$conf,$this->workArea,$txtConf);
00403 }
00404 break;
00405 case 'EMBOSS':
00406 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
00407 $this->makeEmboss($this->im,$conf,$this->workArea,$txtConf);
00408 }
00409 break;
00410 case 'SHADOW':
00411 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
00412 $this->makeShadow($this->im,$conf,$this->workArea,$txtConf);
00413 }
00414 break;
00415
00416
00417 case 'BOX':
00418 $this->makeBox($this->im,$conf,$this->workArea);
00419 break;
00420 case 'EFFECT':
00421 $this->makeEffect($this->im,$conf);
00422 break;
00423 case 'ADJUST':
00424 $this->adjust($this->im,$conf);
00425 break;
00426 case 'CROP':
00427 $this->crop($this->im,$conf);
00428 break;
00429 case 'SCALE':
00430 $this->scale($this->im,$conf);
00431 break;
00432 case 'WORKAREA':
00433 if ($conf['set']) {
00434 $this->setWorkArea($conf['set']);
00435 }
00436 if (isset($conf['clear'])) {
00437 $this->workArea = $this->defaultWorkArea;
00438 }
00439 break;
00440 case 'ELLIPSE':
00441 $this->makeEllipse($this->im, $conf, $this->workArea);
00442 break;
00443 }
00444 }
00445 }
00446 }
00447
00448
00449 if ($this->setup['transparentBackground']) {
00450
00451 $Bcolor = ImageColorExact($this->im, $BGcols[0],$BGcols[1],$BGcols[2]);
00452 imagecolortransparent($this->im, $Bcolor);
00453 } elseif (is_array($this->setup['transparentColor_array'])) {
00454
00455 $Tcolor = $this->unifyColors($this->im, $this->setup['transparentColor_array'], intval($this->setup['transparentColor.']['closest']));
00456 if ($Tcolor>=0) {
00457 imagecolortransparent($this->im, $Tcolor);
00458 }
00459 }
00460
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498 function checkTextObj($conf) {
00499 $conf['fontFile']=$this->checkFile($conf['fontFile']);
00500 if (!$conf['fontFile']){$conf['fontFile']='t3lib/fonts/nimbus.ttf';}
00501 if (!$conf['iterations']){$conf['iterations'] = 1;}
00502 if (!$conf['fontSize']){$conf['fontSize']=12;}
00503 if ($conf['spacing'] || $conf['wordSpacing']) {
00504 $conf['angle']=0;
00505 }
00506 if (!isset($conf['antiAlias'])){$conf['antiAlias']=1;}
00507 $cObj =t3lib_div::makeInstance('tslib_cObj');
00508 $cObj->start($this->data);
00509
00510 $conf['fontColor'] = trim($cObj->stdWrap($conf['fontColor'], $conf['fontColor.']));
00511 $conf['text']=$cObj->stdWrap($conf['text'],$conf['text.']);
00512
00513 if (!$conf['doNotStripHTML']) {
00514 $conf['text'] = strip_tags($conf['text']);
00515 }
00516 $this->combinedTextStrings[] = strip_tags($conf['text']);
00517
00518
00519 if (!isset($conf['breakWidth']) || !$conf['breakWidth']) {
00520 $tlen = (intval($conf['textMaxLength']) ? intval($conf['textMaxLength']) : 100);
00521 $conf['text'] = substr($conf['text'], 0, $tlen);
00522 }
00523 if ((string)$conf['text']!='') {
00524
00525
00526 $fontBaseName = basename($conf['fontFile']);
00527 if (is_array($this->charRangeMap[$fontBaseName])) {
00528
00529
00530 if (!is_array($conf['splitRendering.'])) {
00531 $conf['splitRendering.'] = array();
00532 }
00533
00534 $cfgK = $this->charRangeMap[$fontBaseName]['cfgKey'];
00535 if (!isset($conf['splitRendering.'][$cfgK])) {
00536
00537 $conf['splitRendering.'][$cfgK] = 'charRange';
00538 $conf['splitRendering.'][$cfgK.'.'] = $this->charRangeMap[$fontBaseName]['charMapConfig'];
00539
00540
00541 if ($this->charRangeMap[$fontBaseName]['multiplicator']) {
00542 $conf['splitRendering.'][$cfgK.'.']['fontSize'] = round($conf['fontSize'] * $this->charRangeMap[$fontBaseName]['multiplicator']);
00543 }
00544
00545 if ($this->charRangeMap[$fontBaseName]['pixelSpace']) {
00546 $travKeys = array('xSpaceBefore','xSpaceAfter','ySpaceBefore','ySpaceAfter');
00547 foreach($travKeys as $pxKey) {
00548 if (isset($conf['splitRendering.'][$cfgK.'.'][$pxKey])) {
00549 $conf['splitRendering.'][$cfgK.'.'][$pxKey] = round($conf['splitRendering.'][$cfgK.'.'][$pxKey] * ($conf['fontSize'] / $this->charRangeMap[$fontBaseName]['pixelSpace']));
00550 }
00551 }
00552 }
00553 }
00554 }
00555 if (is_array($conf['splitRendering.'])) {
00556 foreach($conf['splitRendering.'] as $key => $value) {
00557 if (is_array($conf['splitRendering.'][$key])) {
00558 if (isset($conf['splitRendering.'][$key]['fontFile'])) {
00559 $conf['splitRendering.'][$key]['fontFile'] = $this->checkFile($conf['splitRendering.'][$key]['fontFile']);
00560 }
00561 }
00562 }
00563 }
00564
00565 return $conf;
00566 }
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580 function calcOffset($string) {
00581 $value = array();
00582 $numbers = t3lib_div::trimExplode(',', $this->calculateFunctions($string));
00583
00584 foreach ($numbers as $key => $val) {
00585 if ((string)$val == (string)intval($val)) {
00586 $value[$key] = intval($val);
00587 } else {
00588 $value[$key] = $this->calculateValue($val);
00589 }
00590 }
00591
00592 $string = implode(',', $value);
00593 return $string;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 function getResource($file,$fileArray) {
00608 if (!t3lib_div::inList($this->imageFileExt, $fileArray['ext'])) {
00609 $fileArray['ext'] = $this->gifExtension;
00610 }
00611 $cObj =t3lib_div::makeInstance('tslib_cObj');
00612 $cObj->start($this->data);
00613 return $cObj->getImgResource($file,$fileArray);
00614 }
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 function checkFile($file) {
00625 return $GLOBALS['TSFE']->tmpl->getFileName($file);
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635 function fileName($pre) {
00636
00637 $meaningfulPrefix = '';
00638
00639 if ($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix']) {
00640 $meaningfulPrefix = implode('_', array_merge($this->combinedTextStrings, $this->combinedFileNames));
00641
00642 $meaningfulPrefix = preg_replace('/[^A-Za-z0-9_-]/', '', trim($meaningfulPrefix));
00643 $meaningfulPrefix = substr($meaningfulPrefix, 0, intval($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix'])) . '_';
00644 }
00645
00646
00647 return $this->tempPath.
00648 $pre.
00649 $meaningfulPrefix .
00650 t3lib_div::shortMD5(serialize($this->setup)).
00651 '.'.$this->extension();
00652 }
00653
00654
00655
00656
00657
00658
00659
00660 function extension() {
00661 switch(strtolower($this->setup['format'])) {
00662 case 'jpg':
00663 case 'jpeg':
00664 return 'jpg';
00665 break;
00666 case 'png':
00667 return 'png';
00668 break;
00669 case 'gif':
00670 return 'gif';
00671 break;
00672 default:
00673 return $this->gifExtension;
00674 break;
00675 }
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685 protected function calculateValue($string) {
00686 $calculatedValue = 0;
00687 $parts = t3lib_div::splitCalc($string, '+-*/%');
00688
00689 foreach ($parts as $part) {
00690 $theVal = $part[1];
00691 $sign = $part[0];
00692
00693 if ((string)intval($theVal) == (string)$theVal) {
00694 $theVal = intval($theVal);
00695 } elseif ('[' . substr($theVal, 1, -1) . ']' == $theVal) {
00696 $objParts = explode('.', substr($theVal, 1, -1));
00697 $theVal = 0;
00698 if (isset($this->objBB[$objParts[0]])) {
00699 if ($objParts[1] == 'w') {
00700 $theVal = $this->objBB[$objParts[0]][0];
00701 } elseif ($objParts[1] == 'h') {
00702 $theVal = $this->objBB[$objParts[0]][1];
00703 } elseif ($objParts[1] == 'lineHeight') {
00704 $theVal = $this->objBB[$objParts[0]][2]['lineHeight'];
00705 }
00706 $theVal = intval($theVal);
00707 }
00708 } elseif (floatval($theVal)) {
00709 $theVal = floatval($theVal);
00710 } else {
00711 $theVal = 0;
00712 }
00713
00714 if ($sign == '-') {
00715 $calculatedValue-= $theVal;
00716 } elseif ($sign == '+') {
00717 $calculatedValue+= $theVal;
00718 } elseif ($sign == '/' && $theVal) {
00719 $calculatedValue = $calculatedValue / $theVal;
00720 } elseif ($sign == '*') {
00721 $calculatedValue = $calculatedValue * $theVal;
00722 } elseif ($sign == '%' && $theVal) {
00723 $calculatedValue%= $theVal;
00724 }
00725 }
00726
00727 return round($calculatedValue);
00728 }
00729
00730
00731
00732
00733
00734
00735
00736
00737 protected function calculateFunctions($string) {
00738 if (preg_match_all('#max\(([^)]+)\)#', $string, $matches)) {
00739 foreach ($matches[1] as $index => $maxExpression) {
00740 $string = str_replace(
00741 $matches[0][$index],
00742 $this->calculateMaximum(
00743 $maxExpression
00744 ),
00745 $string
00746 );
00747 }
00748 }
00749
00750 return $string;
00751 }
00752
00753
00754
00755
00756
00757
00758
00759 protected function calculateMaximum($string) {
00760 $parts = t3lib_div::trimExplode(',', $this->calcOffset($string), true);
00761 $maximum = (count($parts) ? max($parts) : 0);
00762 return $maximum;
00763 }
00764 }
00765
00766
00767 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php']) {
00768 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php']);
00769 }
00770
00771 ?>