/[drupal]/contributions/modules/flir/flir.module
ViewVC logotype

Contents of /contributions/modules/flir/flir.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Feb 19 20:40:40 2009 UTC (9 months, 1 week ago) by btopro
Branch: MAIN
CVS Tags: DRUPAL-6--1-3-BETA1, HEAD
Changes since 1.2: +333 -159 lines
File MIME type: text/x-php
FLIR updates
1 <?php
2 // $Id: flir.module,v 1.2.2.2 2008/10/13 20:10:11 btopro Exp $
3 //Flir - Convert your Drupal text to generated font-based images using the FLIR project
4 //Copyright (C) 2008 The Pennsylvania State University
5 //
6 //Bryan Ollendyke
7 //bto108@psu.edu
8 //
9 //Keith D. Bailey
10 //kdb163@psu.edu
11 //
12 //12 Borland
13 //University Park, PA 16802
14 //
15 //This program is free software; you can redistribute it and/or modify
16 //it under the terms of the GNU General Public License as published by
17 //the Free Software Foundation; either version 2 of the License, or
18 //(at your option) any later version.
19 //
20 //This program 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 //You should have received a copy of the GNU General Public License along
26 //with this program; if not, write to the Free Software Foundation, Inc.,
27 //51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
29 /**
30 * @file
31 * This Drupal module helps patch together the flir project with Drupal
32 *
33 */
34
35 define('FLIR_CACHE_DIR', file_directory_path() . '/flir/cache');
36 define('FLIR_FONTS_DIR', file_directory_path() . '/flir/fonts');
37 define('FLIR_PLUGIN_DIR', drupal_get_path('module', 'flir') .'/flir/plugins');
38 $FLIR = array();
39 $REAL_HEIGHT_BOUNDS = '';
40 $image = '';
41 $FStyle = array();
42 $ERROR_MSGS = '';
43
44 /*
45 * Implementation of hook_init
46 */
47 function flir_init(){
48 if (user_access('view flir images')) {
49 $js = '
50 $(document).ready(function() {
51 FLIR.init({path:"'. base_path() .'"});
52 ';
53 for($i=1; $i<11; $i++) {
54 if (variable_get('flir_selector_'. $i, '') != '' ) {
55 $js.= ' $("'. variable_get('flir_selector_'. $i, '') .'").each( function() { FLIR.replace(this, new FLIRStyle({cFont:"'. variable_get('flir_font_'. $i, '') .'",mode:"progressive" } )); } );
56 ';
57 }
58 }
59 drupal_add_js($js .'});', 'inline');
60 drupal_add_js(drupal_get_path('module', 'flir') .'/flir/flir.js', 'footer');
61 }
62 }
63
64 /**
65 * Implementation of hook_perm
66 */
67 function flir_perm() {
68 return array('view flir images');
69 }
70 /**
71 * Implementation of hook_menu().
72 */
73 function flir_menu() {
74 $items = array();
75 $items['admin/settings/flir'] = array(
76 'title' => 'Flir',
77 'description' => 'Configure settings for Flir integration module.',
78 'page callback' => 'drupal_get_form',
79 'page arguments' => array('flir_admin_settings'),
80 'access arguments' => array('administer site configuration'),
81 );
82 $items['flir/generate'] = array(
83 'page callback' => '_flir_generate',
84 'title' => 'Ajax Page',
85 'type' => MENU_CALLBACK,
86 'access arguments' => array('view flir images'),
87 );
88 return $items;
89 }
90 /**
91 * Implementation of hook_adm
92 */
93
94 /**
95 * Admin settings form.
96 */
97 function flir_admin_settings() {
98 //create all the paths in the files folder if they don't exist yet
99 $dir = file_create_path(file_directory_path() . '/flir');
100 file_check_directory($dir, 1);
101 $dir = file_create_path(file_directory_path() . '/flir/cache');
102 file_check_directory($dir, 1);
103 $dir = file_create_path(file_directory_path() . '/flir/fonts');
104 file_check_directory($dir, 1);
105
106 //place the default font in the files folder
107 $source = drupal_get_path('module', 'flir') .'/flir/fonts/CHOPS.TTF';
108 file_copy($source,file_directory_path() . '/flir/fonts',FILE_EXISTS_REPLACE);
109
110 require('flir/config-flir.php');
111
112 $form['replacement'] = array(
113 '#type' => 'fieldset',
114 '#title' => t('Flir Replacements'),
115 '#collapsible' => true,
116 '#collapsed' => true,
117 );
118
119 for($i=1; $i<11; $i++) {
120 $collapse = true;
121 if(variable_get('flir_selector_'. $i, '') != ''){
122 $collapse = false;
123 }
124 $form['replacement']['flir_'. $i] = array(
125 '#type' => 'fieldset',
126 '#title' => t('Flir Replacement '. $i),
127 '#collapsible' => true,
128 '#collapsed' => $collapse,
129 );
130 $form['replacement']['flir_'. $i]['flir_selector_'. $i] = array(
131 '#type' => 'textfield',
132 '#title' => t('Items to replace with Flir generated text'),
133 '#default_value' => variable_get('flir_selector_'. $i, ''),
134 '#description' => t("The jQuery list of items to change on the page to a FLIR image."),
135 );
136 $form['replacement']['flir_'. $i]['flir_font_'. $i] = array(
137 '#type' => 'select',
138 '#title' => t('Font to use'),
139 '#options' => $fonts,
140 '#default_value' => variable_get('flir_font_'. $i, $fonts['default']),
141 '#description' => t("The font to use for FLIR replacements"),
142 );
143 }
144 $js = '
145 $(document).ready(function() {
146 FLIR.init({path:"'. base_path() .'"});
147 ';
148 //this will be used to output a sample of each font
149 foreach ($fonts as $key => $font ) {
150 $output.='<fieldset class="collapsible">
151 <legend>'. $font .'</legend><div style="font-size:36px !important;" class="'. str_replace('.','',$font) .'">'. $font .'</div></fieldset>';
152 $js.= ' $(".'. str_replace('.','',$font) .'").each( function() { FLIR.replace(this, new FLIRStyle({cFont:"'. $font .'" } )); } );
153 ';
154 }
155 drupal_add_js($js .'});', 'inline');
156 $form['sample'] = array(
157 '#type' => 'fieldset',
158 '#title' => t('Font Samples'),
159 '#collapsible' => true,
160 '#collapse' => false,
161 );
162 $form['sample']['hidden'] = array(
163 '#type' => 'hidden',
164 '#prefix' => $output,
165 );
166
167 return system_settings_form($form);
168 }
169
170 function _flir_generate(){
171 global $FLIR;
172 global $REAL_HEIGHT_BOUNDS;
173 global $image;
174 global $FStyle;
175 global $ERROR_MSGS;
176 /*
177 Facelift Image Replacement v1.2
178 Facelift was written and is maintained by Cory Mawhorter.
179 It is available from http://facelift.mawhorter.net/
180
181 ===
182
183 This file is part of Facelife Image Replacement ("FLIR").
184
185 FLIR is free software: you can redistribute it and/or modify
186 it under the terms of the GNU General Public License as published by
187 the Free Software Foundation, either version 3 of the License, or
188 (at your option) any later version.
189
190 FLIR is distributed in the hope that it will be useful,
191 but WITHOUT ANY WARRANTY; without even the implied warranty of
192 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193 GNU General Public License for more details.
194
195 You should have received a copy of the GNU General Public License
196 along with Facelift Image Replacement. If not, see <http://www.gnu.org/licenses/>.
197 */
198
199 define('DEBUG', false);
200 define('ENABLE_FONTSIZE_BUG', false);
201
202 define('FLIR_VERSION', '1.2');
203 define('IS_WINDOWS', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
204
205 $argument['text'] = arg(2);
206 $argument['text'] = str_replace("@2@F@",'/',$argument['text']);
207 $argument['h'] = arg(3);
208 $argument['w'] = arg(4);
209 $argument['fstyle'] = arg(5);
210
211 require('flir/config-flir.php');
212 require('flir/inc-flir.php');
213
214 if(version_compare(PHP_VERSION, '4.3.0', '<'))
215 err('PHP_TOO_OLD');
216 if(version_compare(PHP_VERSION, '6.0.0', '>='))
217 err('PHP_UNSUPPORTED');
218
219 if(false !== ALLOWED_DOMAIN && $_SERVER['HTTP_REFERER'] != '') {
220 $refhost = get_hostname($_SERVER['HTTP_REFERER']);
221 if(substr(ALLOWED_DOMAIN, 0, 1) == '.') {
222 if(false === strpos($refhost, substr(ALLOWED_DOMAIN, 1)))
223 err('DISALLOWED_DOMAIN');
224 }else {
225 if($refhost != ALLOWED_DOMAIN)
226 err('DISALLOWED_DOMAIN');
227 }
228 }
229
230 $fonts_dir = str_replace('\\', '/', realpath(FLIR_FONTS_DIR .'/'));
231 if(substr($fonts_dir, -1) != '/')
232 $fonts_dir .= '/';
233
234 $FStyle = preg_match('#^\{("[\w]+":"[^"]*",?)*\}$#i', $argument['fstyle'])?json_decode($argument['fstyle'], true):array();
235
236 $FLIR['mode'] = isset($FStyle['mode']) ? $FStyle['mode'] : '';
237 $FLIR['output'] = isset($FStyle['output']) ? ($FStyle['output']=='jpeg'?'jpg':$FStyle['output']) : 'auto';
238
239 $FLIR['bkg_transparent'] = is_transparent($FStyle['cBackground']);
240
241 if($FLIR['output'] == 'auto')
242 $FLIR['output'] = $FLIR['bkg_transparent'] ? 'png' : 'gif';
243
244 // format not supported, fall back to png
245 if(($FLIR['output'] == 'gif' && !function_exists('imagegif')) || ($FLIR['output'] == 'jpg' && !function_exists('imagejpeg')))
246 $FLIR['output'] = 'png';
247
248 $FLIR['dpi'] = preg_match('#^[0-9]+$#', $FStyle['dpi']) ? $FStyle['dpi'] : 96;
249 $FLIR['size'] = is_number($FStyle['cSize'], true) ? $FStyle['cSize'] : UNKNOWN_FONT_SIZE; // pixels
250 $FLIR['size_pts'] = ENABLE_FONTSIZE_BUG ? $FLIR['size'] : get_points($FLIR['dpi'], $FLIR['size']);
251 $FLIR['maxheight']= is_number($argument['h']) ? $argument['h'] : UNKNOWN_FONT_SIZE; // pixels
252 $FLIR['maxwidth']= is_number($argument['w']) ? $argument['w'] : 800; // pixels
253
254 $font_file = '';
255 $FStyle['cFont'] = strtolower($FStyle['cFont']);
256
257 $FONT_PARENT = false;
258 if(isset($fonts[$FStyle['cFont']])) {
259 $font_file = $fonts[$FStyle['cFont']];
260 if(is_array($font_file)) {
261 $FONT_PARENT = reset($font_file);
262 $font_file = match_font_style($font_file);
263 $FONT_PARENT = $fonts_dir.(isset($FONT_PARENT['file']) ? $FONT_PARENT['file'] : $font_file);
264 }
265 }elseif(FONT_DISCOVERY) {
266 $font_file = discover_font($fonts['default'], $FStyle['cFont']);
267 }else {
268 $font_file = $fonts['default'];
269 }
270
271 $FLIR['font'] = $fonts_dir.$font_file;
272
273 //die($FStyle['cFont']);
274
275 if(!is_file($FLIR['font']))
276 err('FONT_DOESNT_EXIST');
277
278 if(in_array(strtolower(pathinfo($FLIR['font'], PATHINFO_EXTENSION)), array('pfb','pfm'))) { // pfm doesn't work
279 // You can try uncommenting this line to see what kind of mileage you get.
280 err('FONT_PS_UNSUPPORTED'); // PostScript will work as long as you don't set any kind of spacing... unless you are using Windows (PHP bug?).
281
282 $FLIR['postscript'] = true;
283 $FLIR['ps'] = array('kerning' => 0, 'space' => 0);
284 if(false === (@$FLIR['ps']['font'] = imagepsloadfont($FLIR['font'])))
285 err('FONT_PS_COULDNT_LOAD');
286 }
287
288 $FLIR['color'] = convert_color($FStyle['cColor']);
289
290 if($FLIR['bkg_transparent']) {
291 $FLIR['bkgcolor'] = array('red' => abs($FLIR['color']['red']-100)
292 , 'green' => abs($FLIR['color']['green']-100)
293 , 'blue' => abs($FLIR['color']['blue']-100));
294 }else {
295 $FLIR['bkgcolor'] = convert_color($FStyle['cBackground'], false, 'FFFFFF');
296 }
297
298 $FLIR['opacity'] = is_number($FStyle['cOpacity'], true) ? $FStyle['cOpacity']*100 : 100;
299 if($FLIR['opacity'] > 100 || $FLIR['opacity'] < 0)
300 $FLIR['opacity'] = 100;
301
302 $FLIR['text'] = $argument['text']!=''?str_replace(array('{amp}nbsp;', '{amp}', '{plus}'), array(' ','&','+'), trim($argument['text'], "\t\n\r")):'null';
303
304 $FLIR['cache'] = get_cache_fn(md5(($FLIR['mode']=='wrap'?$FLIR['maxwidth']:'').$FLIR['font'].(print_r($FStyle,true).$FLIR['text'])), $FLIR['output']);
305
306 $FLIR['text_encoded'] = $FLIR['text'];
307 $FLIR['text'] = $FLIR['original_text'] = strip_tags(html_entity_decode_utf8($FLIR['text']));
308
309 $SPACE_BOUNDS = false;
310 if(is_number($FStyle['cSpacing'], true, false, true)) {
311 $SPACE_BOUNDS = bounding_box(' ');
312 $spaces = ceil(($FStyle['cSpacing']/$SPACE_BOUNDS['width']));
313 if($spaces>0) {
314 $FLIR['text'] = space_out($FLIR['text'], $spaces);
315 define('SPACING_GAP', $spaces);
316 }
317
318 if($FLIR['postscript']) {
319 $FLIR['ps']['kerning'] = ($FStyle['cSpacing']/$FLIR['size'])*1000;
320 }
321 }
322
323 if($FLIR['postscript'] && isset($FStyle['space_width'])) {
324 $FLIR['ps']['space'] = ($FStyle['space_width']/$FLIR['size'])*1000;
325 }
326
327 if(($SPACES_COUNT = substr_count($FLIR['text'], ' ')) == strlen($FLIR['text'])) {
328 if(false === $SPACE_BOUNDS)
329 $SPACE_BOUNDS = bounding_box(' ');
330
331 $FLIR['cache'] = get_cache_fn(md5($FLIR['font'].$FLIR['size'].$SPACES_COUNT));
332 $FLIR['mode'] = 'spacer';
333 }
334
335 if(file_exists($FLIR['cache']) && !DEBUG) {
336 output_file($FLIR['cache']);
337 }else {
338 verify_gd();
339
340 $REAL_HEIGHT_BOUNDS = $FStyle['realFontHeight']=='true' ? bounding_box(HBOUNDS_TEXT, (false !== $FONT_PARENT ? $FONT_PARENT : $FLIR['font'])): false;
341
342 switch($FLIR['mode']) {
343 default:
344 $dir = dir(FLIR_PLUGIN_DIR);
345 $php_mode = strtolower($FLIR['mode'].'.php');
346 while(false !== ($entry = $dir->read())) {
347 $p = FLIR_PLUGIN_DIR.'/'.$entry;
348 if(is_dir($p) || $entry == '.' || $entry == '..') continue;
349
350 if($php_mode == strtolower($entry)) {
351 $dir->close();
352 $PLUGIN_ERROR = false;
353
354 include($p);
355
356 if(false !== $PLUGIN_ERROR)
357 break;
358 else
359 break(2);
360 }
361 }
362 $dir->close();
363
364 $bounds = bounding_box($FLIR['text']);
365 if($FStyle['realFontHeight']!='true')
366 $REAL_HEIGHT_BOUNDS = $bounds;
367 if(false === (@$image = imagecreatetruecolor($bounds['width'], $REAL_HEIGHT_BOUNDS['height'])))
368 err('COULD_NOT_CREATE');
369
370 gd_alpha();
371 imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), gd_bkg());
372 render_text($bounds);
373 break;
374 case 'wrap':
375 if(!is_number($FStyle['cLine'], true))
376 $FStyle['cLine'] = 1.0;
377
378 $bounds = bounding_box($FLIR['text']);
379 if($FStyle['realFontHeight']!='true')
380 $REAL_HEIGHT_BOUNDS = $bounds;
381
382 // if mode is wrap, check to see if text needs to be wrapped, otherwise let continue to progressive
383 if($bounds['width'] > $FLIR['maxwidth']) {
384 $image = imagettftextbox($FLIR['size_pts'], 0, 0, 0, $FLIR['color'], $FLIR['font'], $FLIR['text'], $FLIR['maxwidth'], strtolower($FStyle['cAlign']), $FStyle['cLine']);
385 break;
386 }else {
387 if(false === (@$image = imagecreatetruecolor($bounds['width'], $REAL_HEIGHT_BOUNDS['height'])))
388 err('COULD_NOT_CREATE');
389
390 gd_alpha();
391 imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), gd_bkg());
392 render_text($bounds);
393 }
394 break;
395 case 'progressive':
396 $bounds = bounding_box($FLIR['text']);
397 if($FStyle['realFontHeight']!='true')
398 $REAL_HEIGHT_BOUNDS = $bounds;
399
400 $offset_left = 0;
401
402 $nsize=$FLIR['size_pts'];
403 while(($REAL_HEIGHT_BOUNDS['height'] > $FLIR['maxheight'] || $bounds['width'] > $FLIR['maxwidth']) && $nsize > 2) {
404 $nsize-=0.5;
405 $bounds = bounding_box($FLIR['text'], NULL, $nsize);
406 $REAL_HEIGHT_BOUNDS = $FStyle['realFontHeight']=='true' ? bounding_box(HBOUNDS_TEXT, NULL, $nsize) : $bounds;
407 }
408 $FLIR['size_pts'] = $nsize;
409
410 if(false === (@$image = imagecreatetruecolor($bounds['width'], $REAL_HEIGHT_BOUNDS['height'])))
411 err('COULD_NOT_CREATE');
412
413 gd_alpha();
414 imagefilledrectangle($image, $offset_left, 0, imagesx($image), imagesy($image), gd_bkg());
415
416 imagettftext($image, $FLIR['size_pts'], 0, $bounds['xOffset'], $REAL_HEIGHT_BOUNDS['yOffset'], gd_color(), $FLIR['font'], $FLIR['text']);
417 render_text($bounds);
418 break;
419
420 case 'spacer':
421 if(false === (@$image = imagecreatetruecolor(($SPACE_BOUNDS['width']*$SPACES_COUNT), 1)))
422 err('COULD_NOT_CREATE');
423
424 imagesavealpha($image, true);
425 imagealphablending($image, false);
426
427 imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), gd_bkg());
428 break;
429 }
430
431 if($FLIR['postscript'])
432 imagepsfreefont($FLIR['ps']['font']);
433
434 if(false !== $image) {
435 switch($FLIR['output']) {
436 default:
437 case 'png':
438 imagepng($image, $FLIR['cache']);
439 break;
440 case 'gif':
441 imagegif($image, $FLIR['cache']);
442 break;
443 case 'jpg':
444 $qual = is_number($FStyle['quality']) ? $FStyle['quality'] : 90;
445 imagejpeg($image, $FLIR['cache'], $qual);
446 break;
447 }
448 imagedestroy($image);
449 }
450
451 output_file($FLIR['cache']);
452 } // if(file_exists($FLIR['cache'])) {
453
454 flush();
455
456 if(CACHE_CLEANUP_FREQ != -1 && rand(1, CACHE_CLEANUP_FREQ) == 1)
457 @cleanup_cache();
458 }

  ViewVC Help
Powered by ViewVC 1.1.2