*** empty log message ***
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_iconworks.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skaarhoj (kasper@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains class for icon generation in the backend
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 * XHTML compliant
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 82: class t3lib_iconWorks
42 * 96: function getIconImage($table,$row=array(),$backPath,$params='',$shaded=0)
43 * 113: function getIcon($table,$row=array(),$shaded=0)
44 * 195: function makeIcon($iconfile,$mode, $user, $protectSection=0,$absFile='')
45 * 298: function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)
46 * 331: function imagecreatefrom($file)
47 * 348: function imagemake($im, $path)
48 *
49 * TOTAL FUNCTIONS: 6
50 * (This index is automatically created/updated by the extension "extdeveval")
51 *
52 */
53
54
55
56
57
58
59
60
61
62
63 /**
64 * Icon generation, backend
65 * This library has functions that returns - and if necessary creates - the icon for an element in TYPO3
66 *
67 * Expects global vars:
68 * - $BACK_PATH
69 * - PATH_typo3
70 * - $TCA, $PAGES_TYPES
71 *
72 *
73 * Notes:
74 * These functions are strongly related to the interface of TYPO3.
75 * The class is included in eg. init.php
76 * ALL functions called without making a class instance, eg. "t3lib_iconWorks::getIconImage()"
77 *
78 * @author Kasper Skaarhoj <kasper@typo3.com>
79 * @package TYPO3
80 * @subpackage t3lib
81 */
82 class t3lib_iconWorks {
83
84 /**
85 * Returns an icon image tag, 18x16 pixels, based on input information.
86 * This function is recommended to use in your backend modules.
87 *
88 * @param string The table name
89 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
90 * @param string The backpath to the main TYPO3 directory (relative path back to PATH_typo3)
91 * @param string Additional attributes for the image tag
92 * @param boolean If set, the icon will be grayed/shaded.
93 * @return string <img>-tag
94 * @see getIcon()
95 */
96 function getIconImage($table,$row=array(),$backPath,$params='',$shaded=0) {
97 $str='<img src="'.$backPath.t3lib_iconWorks::getIcon($table,$row,$shaded).'" width="18" height="16" border="0"'.(trim($params)?' '.trim($params):'');
98 if (!stristr($str,'alt="')) $str.=' alt=""';
99 $str.=' />';
100 return $str;
101 }
102
103 /**
104 * Creates the icon for input table/row
105 * Returns filename for the image icon, relative to PATH_typo3
106 *
107 * @param string The table name
108 * @param array The table row ("enablefields" are at least needed for correct icon display and for pages records some more fields in addition!)
109 * @param boolean If set, the icon will be grayed/shaded.
110 * @return string Icon filename
111 * @see getIconImage()
112 */
113 function getIcon($table,$row=array(),$shaded=0) {
114 global $TCA, $PAGES_TYPES, $ICON_TYPES;
115 $user=false;
116 if ($table=='pages') {
117 if (!$iconfile = $PAGES_TYPES[$row['doktype']]['icon']) {
118 $iconfile = $PAGES_TYPES['default']['icon'];
119 }
120 if ($row['module'] && $ICON_TYPES[$row['module']]['icon']) {
121 $iconfile = $ICON_TYPES[$row['module']]['icon'];
122 }
123 } else {
124 if (!$iconfile = $TCA[$table]['ctrl']['typeicons'][$row[$TCA[$table]['ctrl']['typeicon_column']]]) {
125 $iconfile = (($TCA[$table]['ctrl']['iconfile']) ? $TCA[$table]['ctrl']['iconfile'] : $table.'.gif');
126 }
127 }
128 // Setting path if not already set:
129 if (!strstr($iconfile,'/')) {
130 $iconfile = 'gfx/i/'.$iconfile;
131 }
132 if (substr($iconfile,0,3)=='../') {
133 $absfile=PATH_site.substr($iconfile,3);
134 } else {
135 $absfile=PATH_typo3.$iconfile;
136 }
137
138
139 $hidden = false;
140 $timing = false;
141 $futuretiming = false;
142 $deleted = false;
143 $protectSection=0;
144 if ($enCols=$TCA[$table]['ctrl']['enablecolumns']) {
145 if ($enCol=$row[$enCols['disabled']]) {if ($enCol){$hidden=true;}}
146 if ($enCol=$row[$enCols['starttime']]) {if (time() < $enCol){$timing=true;}}
147 if ($enCol=$row[$enCols['endtime']]) {
148 if ($enCol!=0){
149 if ($enCol < time()) {
150 $timing=true;
151 } else {
152 $futuretiming=true;
153 }
154 }
155 }
156 if ($enCol=$row[$enCols['fe_group']]) {
157 $user=$enCol;
158 }
159 }
160 if ($col=$row[$TCA[$table]['ctrl']['delete']]) {
161 $deleted = true;
162 }
163 if ($table=='pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
164 $protectSection=1;
165 }
166 if ($hidden || $timing || $futuretiming || $deleted || $user || $shaded) {
167 $string='';
168 if ($deleted) {
169 $string='deleted';
170 } else {
171 if ($hidden) $string.='hidden';
172 if ($timing) $string.='timing';
173 if (!$string && $futuretiming) {
174 $string='futuretiming';
175 }
176 }
177 $theRes= t3lib_iconWorks::makeIcon($GLOBALS['BACK_PATH'].$iconfile, $string, $user, $protectSection, $absfile);
178 return $theRes;
179 } else {
180 return $iconfile;
181 }
182 }
183
184 /**
185 * Creates the icon file for the function getIcon()
186 *
187 * @param string Original unprocessed Icon file, relative path to PATH_typo3
188 * @param string Mode string, eg. "deleted" or "futuretiming" determining how the icon will look
189 * @param integer The number of the fe_group record uid if applicable
190 * @param boolean Flag determines if the protected-section icon should be applied.
191 * @param string Absolute path to file from which to create the icon.
192 * @return string Filename relative to PATH_typo3
193 * @access private
194 */
195 function makeIcon($iconfile,$mode, $user, $protectSection=0,$absFile='') {
196 $iconFileName = 'icon_'.t3lib_div::shortMD5($iconfile.'|'.$mode.'|-'.$user.'|'.$protectSection).'.'.($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']?'png':'gif');
197 $mainpath = '../typo3temp/'.$iconFileName;
198 $path = PATH_site.'typo3temp/'.$iconFileName;
199
200
201 if (@file_exists(PATH_typo3.'icons/'.$iconFileName)) { // Returns if found in typo3/icons/
202 return 'icons/'.$iconFileName;
203 } elseif (@file_exists($path)) { // Returns if found in ../typo3temp/icons/
204 return $mainpath;
205 } else { // Makes icon:
206 if (@file_exists($absFile)) {
207 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
208 $im = t3lib_iconworks::imagecreatefrom($absFile);
209 if ($im<0) return $iconfile;
210
211 if ($mode!='futuretiming' && !(!$mode && $user)) {
212 for ($c=0; $c<ImageColorsTotal($im); $c++) {
213 $cols = ImageColorsForIndex($im,$c);
214 $newcol = round(($cols['red']+$cols['green']+$cols['blue'])/3);
215 $lighten = 2;
216 $newcol = round (255-((255-$newcol)/$lighten));
217 ImageColorSet($im,$c,$newcol,$newcol,$newcol);
218 }
219 }
220 if ($user) {
221 $black = ImageColorAllocate($im, 0,0,0);
222 imagefilledrectangle($im, 0,0,(($user>10)?9:5),8,$black);
223
224 $white = ImageColorAllocate($im, 255,255,255);
225 imagestring($im, 1, 1, 1, $user, $white);
226
227 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_group.gif');
228 if ($ol_im<0) return $iconfile;
229
230 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
231 }
232 if ($mode) {
233 unset($ol_im);
234 switch($mode) {
235 case 'deleted':
236 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_deleted.gif');
237 break;
238 case 'futuretiming':
239 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
240 break;
241 case 'timing':
242 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
243 break;
244 case 'hiddentiming':
245 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden_timing.gif');
246 break;
247 case 'hidden':
248 default:
249 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden.gif');
250 break;
251 }
252 if ($ol_im<0) return $iconfile;
253 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
254 }
255 if ($protectSection) {
256 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_sub5.gif');
257 if ($ol_im<0) return $iconfile;
258 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
259 }
260 @t3lib_iconWorks::imagemake($im, $path);
261 t3lib_div::gif_compress($path, 'IM');
262 ImageDestroy($im);
263 return $mainpath;
264 } else {
265 return $iconfile;
266 }
267 } else {
268 return $GLOBALS['BACK_PATH'].'gfx/fileicons/default.gif';
269 }
270 }
271 }
272
273 /**
274 * The necessity of using this function for combining two images if GD is version 2 is that
275 * GD2 cannot manage to combine two indexed-color images without totally spoiling everything.
276 * In class.t3lib_stdgraphic this was solved by combining the images onto a first created true color image
277 * However it has turned out that this method will not work if the indexed png-files contains transparency.
278 * So I had to turn my attention to ImageMagick - my 'enemy of death'.
279 * And so it happend - ImageMagick is now used to combine my two indexed-color images with transparency. And that works.
280 * Of course it works only if ImageMagick is able to create valid png-images - which you cannot be sure of with older versions (still 5+)
281 * The only drawback is (apparently) that IM creates true-color png's. The transparency of these will not be shown by MSIE on windows at this time (although it's straight 0%/100% transparency!) and the file size may be larger.
282 *
283 * For parameters, see PHP function "imagecopyresized()"
284 *
285 * @param pointer
286 * @param pointer
287 * @param integer
288 * @param integer
289 * @param integer
290 * @param integer
291 * @param integer
292 * @param integer
293 * @param integer
294 * @param integer
295 * @return void
296 * @access private
297 */
298 function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h) {
299 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_2'] && $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) { // Maybe I'll have to change this if GD2/gif does not work either...
300 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']) {
301 $cmd=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].
302 ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']?$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']:'combine').
303 ' -compose over ';
304 $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
305
306 ImagePng($im, $tempBaseName.'_im.png');
307 ImagePng($cpImg, $tempBaseName.'_cpImg.png');
308 exec($cmd.
309 $tempBaseName.'_cpImg.png '.
310 $tempBaseName.'_im.png '.
311 $tempBaseName.'_out.png '
312 );
313 $im = imagecreatefrompng($tempBaseName.'_out.png');
314 unlink($tempBaseName.'_im.png');
315 unlink($tempBaseName.'_cpImg.png');
316 unlink($tempBaseName.'_out.png');
317 }
318 } else {
319 imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
320 }
321 }
322
323 /**
324 * Create new image pointer from input file (either gif/png, in case the wrong format it is converted by t3lib_div::read_png_gif())
325 *
326 * @param string Absolute filename of the image file from which to start the icon creation.
327 * @return mixed If success, image pointer, otherwise "-1"
328 * @access private
329 * @see t3lib_div::read_png_gif
330 */
331 function imagecreatefrom($file) {
332 $file = t3lib_div::read_png_gif($file,$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
333 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
334 return $file ? imagecreatefrompng($file) : -1;
335 } else {
336 return $file ? imagecreatefromgif($file) : -1;
337 }
338 }
339
340 /**
341 * Write the icon in $im pointer to $path
342 *
343 * @param pointer Pointer to GDlib image resource
344 * @param string Absolute path to the filename in which to write the icon.
345 * @return void
346 * @access private
347 */
348 function imagemake($im, $path) {
349 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
350 @ImagePng($im, $path);
351 } else {
352 @ImageGif($im, $path);
353 }
354 }
355 }
356 ?>