/[drupal]/contributions/modules/textimage/textimage_admin.inc
ViewVC logotype

Contents of /contributions/modules/textimage/textimage_admin.inc

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


Revision 1.16 - (show annotations) (download) (as text)
Thu May 14 12:32:20 2009 UTC (6 months, 1 week ago) by deciphered
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +7 -6 lines
File MIME type: text/x-php
Fixed minor issues.
1 <?php
2 // $Id: textimage_admin.inc,v 1.15 2009/05/14 03:53:24 deciphered Exp $
3
4 /**
5 * @file
6 */
7
8 function textimage_settings_form() {
9 $form = array();
10
11 $fonts_path = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts');
12 $form['fonts'] = array(
13 '#type' => 'fieldset',
14 '#title' => t('Fonts'),
15 '#description' => t('Number of fonts found: !fonts', array('!fonts' => count(_textimage_font_list($fonts_path)))),
16 );
17 $form['fonts']['textimage_fonts_path'] = array(
18 '#type' => 'textfield',
19 '#title' => t('Path'),
20 '#default_value' => $fonts_path,
21 '#maxlength' => 255,
22 '#description' =>
23 t('Location of the directory where the TrueType (.ttf) or OpenType (.otf) fonts are stored.') .'<br />'.
24 t('Relative paths will be resolved relative to the Drupal installation directory.')
25 ,
26 );
27
28 $images_path = variable_get('textimage_images_path', drupal_get_path('module', 'textimage') . '/backgrounds');
29 $form['images'] = array(
30 '#type' => 'fieldset',
31 '#title' => t('Background images'),
32 '#description' => t('Number of background images found: !images', array('!images' => count(_textimage_image_list($images_path)))),
33 );
34 $form['images']['textimage_images_path'] = array(
35 '#type' => 'textfield',
36 '#title' => t('Path'),
37 '#default_value' => $images_path,
38 '#maxlength' => 255,
39 '#description' =>
40 t('Location of the directory where the background images are stored. Images in this directory can be overlaid with dynamic text in a preset.') .'<br />'.
41 t('Relative paths will be resolved relative to the Drupal installation directory.')
42 ,
43 );
44
45 return system_settings_form($form);
46 }
47
48 function textimage_settings_form_validate($form, &$form_state) {
49 // Check for valid fonts path.
50 if ($form_state['values']['textimage_fonts_path'] != "" && !is_dir(rtrim($form_state['values']['textimage_fonts_path'], '\\/'))) {
51 form_set_error('textimage_fonts_path', t('The fonts path "%path" is invalid.', array('%path' => $form_state['values']['textimage_fonts_path'])));
52 }
53
54 // Check for valid backgorund images path.
55 if ($form_state['values']['textimage_images_path'] != "" && !is_dir(rtrim($form_state['values']['textimage_images_path'], '\\/'))) {
56 form_set_error('textimage_images_path', t('The background images path "%path" is invalid.', array('%path' => $form_state['values']['textimage_fonts_path'])));
57 }
58 }
59
60 function textimage_preset_edit(&$form_state, $op, $pid = NULL) {
61 $preset = array();
62 $image_presets = array();
63
64 if ($op == 'edit' && is_numeric($pid)) {
65 $preset = _textimage_preset_load($pid);
66 }
67
68 $fonts_path = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts');
69 $font_options = _textimage_font_list($fonts_path);
70
71 $images_path = variable_get('textimage_images_path', drupal_get_path('module', 'textimage') . '/backgrounds');
72 $image_files = drupal_map_assoc(_textimage_image_list($images_path));
73
74 $presets = textimage_get_presets();
75 $presets_list = array();
76 foreach ($presets as $p) {
77 $presets_list[$p->pid] = $p;
78 }
79 foreach ($presets as $p) {
80 if ($p->pid != $pid) {
81 $preset_test = $p;
82 $preset_err = FALSE;
83 while (is_numeric($preset_test->settings['background']['image'])) {
84 if ($preset_test->settings['background']['image'] == $pid) {
85 $preset_err = TRUE;
86 break;
87 }
88
89 $preset_test = $presets_list[$preset_test->settings['background']['image']];
90 }
91
92 if (!$preset_err) {
93 $image_presets[$p->pid] = $p->name;
94 }
95 }
96 }
97
98 $image_options = array(
99 t('Default:') => array('' => t('Default (use background color)')),
100 t('Use the result of a Textimage Preset:') => $image_presets,
101 t('Use a Background Image:') => $image_files,
102 );
103
104 $form = array();
105 $form['#tree'] = TRUE;
106
107 if ($op == 'edit') {
108 $form['pid'] = array(
109 '#type' => 'hidden',
110 '#value' => $pid,
111 );
112 }
113
114 $form['name'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Preset Name'),
117 '#description' => t('Preset names should be short and only use alphanumeric characters.'),
118 '#default_value' => isset($preset['name'])
119 ? $preset['name']
120 : '',
121 '#required' => TRUE,
122 );
123
124 $form['description'] = array(
125 '#type' => 'textarea',
126 '#title' => t('Description'),
127 '#description' => t('A short description displayed in the list of presets.'),
128 '#default_value' => isset($preset['description'])
129 ? $preset['description']
130 : '',
131 '#rows' => 1,
132 );
133
134 // Preview textimage.
135 $preview = '';
136 if (count($preset) > 0) {
137 $preview = $preset;
138 $preview['pid'] = 0;
139 $preview_text = isset($preview['settings']['preview']['text']['default'])
140 ? $preview['settings']['preview']['text']['default']
141 : t('Preview');
142
143 $additional_text = array();
144 if (isset($preset['settings']['preview']['text']['additional'])) {
145 $additional_text = $preset['settings']['preview']['text']['additional'];
146 rsort($additional_text);
147 }
148
149 $preview = theme_textimage_image($preview, $preview_text, $additional_text, 'png', $preview_text, $preview_text);
150 }
151 $form['preview'] = array(
152 '#markup' =>
153 '<strong>' . t('Preview') . ":</strong>\n".
154 '<div id="textimage-preview">' . $preview . '</div>'
155 ,
156 );
157
158 $form['settings'] = array(
159 '#type' => 'vertical_tabs',
160 );
161
162 // Text settings
163 $form['settings']['font'] = array(
164 '#type' => 'fieldset',
165 '#title' => t('Font settings'),
166 '#collapsible' => TRUE,
167 '#collapsed' => FALSE,
168 );
169 $form['settings']['font']['file'] = array(
170 '#type' => 'select',
171 '#title' => t('Font'),
172 '#options' => $font_options,
173 '#default_value' => isset($preset['settings']['font']['file'])
174 ? $preset['settings']['font']['file']
175 : '',
176 '#description' => t('Select the font to be used in this image. If no fonts are listed, check the <a href="!path">settings for Fonts Path</a>.', array('!path' => url('admin/build/textimage/settings'))),
177 '#required' => TRUE,
178 );
179 $form['settings']['font']['size'] = array(
180 '#type' => 'textfield',
181 '#title' => t('Size'),
182 '#field_suffix' => t('px'),
183 '#description' => t('Enter the size in pixels of the text to be generated.'),
184 '#default_value' => isset($preset['settings']['font']['size'])
185 ? $preset['settings']['font']['size']
186 : 16,
187 '#maxlength' => 5,
188 '#size' => 3,
189 '#required' => TRUE,
190 '#validate' => array('_textimage_number_validate' => array('size')),
191 );
192 $form['settings']['font']['color']['hex'] = array(
193 '#type' => 'textfield',
194 '#title' => t('Color'),
195 '#description' => t('Enter the hex color code you wish to use for the generated text (i.e. #000000).'),
196 '#default_value' => isset($preset['settings']['font']['color']['hex'])
197 ? $preset['settings']['font']['color']['hex']
198 : '#000000',
199 '#maxlength' => 7,
200 '#size' => 8,
201 '#validate' => array('_textimage_hex_validate' => array('color')),
202 '#required' => TRUE,
203 '#attributes' => array(
204 'class' => 'color'
205 ),
206 );
207 $form['settings']['font']['color']['opacity'] = array(
208 '#type' => 'textfield',
209 '#title' => t('Opacity'),
210 '#default_value' => isset($preset['settings']['font']['color']['opacity'])
211 ? $preset['settings']['font']['color']['opacity']
212 : '100',
213 '#maxlength' => 3,
214 '#size' => 2,
215 '#field_suffix' => '%',
216 );
217
218 $form['settings']['text'] = array(
219 '#type' => 'fieldset',
220 '#title' => t('Text settings'),
221 '#collapsible' => TRUE,
222 '#collapsed' => FALSE,
223 );
224 $form['settings']['text']['maximum_width'] = array(
225 '#type' => 'textfield',
226 '#title' => t('Maximum width'),
227 '#field_suffix' => t('px'),
228 '#description' => t('Text lines wider than this will be wrapped. Leave blank to disable wrapping.'),
229 '#default_value' => isset($preset['settings']['text']['maximum_width'])
230 ? $preset['settings']['text']['maximum_width']
231 : '',
232 '#maxlength' => 4,
233 '#size' => 4,
234 );
235 $form['settings']['text']['fixed_width'] = array(
236 '#type' => 'checkbox',
237 '#title' => t('Fixed width?'),
238 '#description' => t('If checked the size of generated image will always be equal to the max width') . '.',
239 '#default_value' => isset($preset['settings']['text']['fixed_width'])
240 ? $preset['settings']['text']['fixed_width']
241 : '',
242 );
243 $form['settings']['text']['align'] = array(
244 '#type' => 'select',
245 '#title' => t('Text Align'),
246 '#description' => t('Only works when fixed width is enabled') . '.',
247 '#options' => array(ALIGN_LEFT => t('Left'), ALIGN_CENTER => t('Center'), ALIGN_RIGHT => t('Right')),
248 '#default_value' => isset($preset['settings']['text']['align'])
249 ? $preset['settings']['text']['align']
250 : '',
251 );
252 $form['settings']['text']['angle'] = array(
253 '#type' => 'textfield',
254 '#title' => t('Text Rotation'),
255 '#field_suffix' => t('&deg;'),
256 '#description' => t('Enter the angle in degrees at which the text will be displayed. Positive numbers rotate the text clockwise, negative numbers counter-clockwise.'),
257 '#default_value' => isset($preset['settings']['text']['angle'])
258 ? $preset['settings']['text']['angle']
259 : 0,
260 '#maxlength' => 4,
261 '#size' => 4,
262 '#validate' => array('_textimage_number_validate' => array('angle')),
263 );
264 $form['settings']['text']['case'] = array(
265 '#type' => 'select',
266 '#title' => t('Convert to case'),
267 '#options' => array('' => t('Default'), 'upper' => t('UPPERCASE'), 'lower' => t('lowercase'), 'ucwords' => t('Uppercase Words'), 'ucfirst' => t('Uppercase first')),
268 '#description' => t('Covert the input text to a consistent format. The default makes no changes to input text.'),
269 '#default_value' => isset($preset['settings']['text']['case'])
270 ? $preset['settings']['text']['case']
271 : '',
272 );
273
274 // Outline
275 $form['settings']['text']['stroke'] = array(
276 '#type' => 'fieldset',
277 '#title' => t('Outline'),
278 '#collapsible' => TRUE,
279 '#collapsed' => TRUE,
280 );
281 $form['settings']['text']['stroke']['width'] = array(
282 '#type' => 'textfield',
283 '#title' => t('Width'),
284 '#description' => t('Optionally add a stroke outline around the text. Enter the stroke width in pixels.'),
285 '#default_value' => isset($preset['settings']['text']['stroke']['width'])
286 ? $preset['settings']['text']['stroke']['width']
287 : 0,
288 '#maxlength' => 2,
289 '#size' => 3,
290 '#field_suffix' => 'px',
291 '#validate' => array('_textimage_number_validate' => array('stroke_width')),
292 );
293 $form['settings']['text']['stroke']['color'] = array(
294 '#type' => 'textfield',
295 '#title' => t('Color'),
296 '#default_value' => isset($preset['settings']['text']['stroke']['color'])
297 ? $preset['settings']['text']['stroke']['color']
298 : '#000000',
299 '#maxlength' => 7,
300 '#size' => 8,
301 '#attributes' => array(
302 'class' => 'color'
303 ),
304 '#description' => t('Enter the hex color code you wish to use for the stroke outline (i.e. #000000)'),
305 '#validate' => array('_textimage_hex_validate' => array('color')),
306 );
307
308 // Margin
309 $form['settings']['text']['margin'] = array(
310 '#type' => 'fieldset',
311 '#title' => t('Margin'),
312 '#collapsible' => TRUE,
313 '#collapsed' => TRUE,
314 '#description' => t('Specify the margin in pixels to be added around the generated text.'),
315 );
316 $form['settings']['text']['margin']['top'] = array(
317 '#type' => 'textfield',
318 '#title' => t('Top'),
319 '#field_suffix' => t('px'),
320 '#default_value' => isset($preset['settings']['text']['margin']['top'])
321 ? $preset['settings']['text']['margin']['top']
322 : 0,
323 '#maxlength' => 4,
324 '#size' => 4,
325 '#validate' => array('_textimage_number_validate' => array('margin_top')),
326 );
327 $form['settings']['text']['margin']['right'] = array(
328 '#type' => 'textfield',
329 '#title' => t('Right'),
330 '#field_suffix' => t('px'),
331 '#default_value' => isset($preset['settings']['text']['margin']['right'])
332 ? $preset['settings']['text']['margin']['right']
333 : 0,
334 '#maxlength' => 4,
335 '#size' => 4,
336 '#validate' => array('_textimage_number_validate' => array('margin_right')),
337 );
338 $form['settings']['text']['margin']['bottom'] = array(
339 '#type' => 'textfield',
340 '#title' => t('Bottom'),
341 '#field_suffix' => t('px'),
342 '#default_value' => isset($preset['settings']['text']['margin']['bottom'])
343 ? $preset['settings']['text']['margin']['bottom']
344 : 0,
345 '#maxlength' => 4,
346 '#size' => 4,
347 '#validate' => array('_textimage_number_validate' => array('margin_bottom')),
348 );
349 $form['settings']['text']['margin']['left'] = array(
350 '#type' => 'textfield',
351 '#title' => t('Left'),
352 '#field_suffix' => t('px'),
353 '#default_value' => isset($preset['settings']['text']['margin']['left'])
354 ? $preset['settings']['text']['margin']['left']
355 : 0,
356 '#maxlength' => 4,
357 '#size' => 4,
358 '#validate' => array('_textimage_number_validate' => array('margin_left')),
359 );
360
361 // Background Settings
362 $form['settings']['background'] = array(
363 '#type' => 'fieldset',
364 '#title' => t('Background settings'),
365 '#collapsible' => TRUE,
366 '#collapsed' => FALSE,
367 );
368 $form['settings']['background']['color'] = array(
369 '#type' => 'textfield',
370 '#title' => t('Color'),
371 '#description' => t('Enter the hex color code you wish to use for the background of the generated image (i.e. #FFFFFF). Leave blank for transparent.'),
372 '#default_value' => isset($preset['settings']['background']['color'])
373 ? $preset['settings']['background']['color']
374 : '#FFFFFF',
375 '#maxlength' => 7,
376 '#size' => 8,
377 '#validate' => array('_textimage_hex_validate' => array('color')),
378 '#attributes' => array(
379 'class' => 'color'
380 ),
381 );
382 $form['settings']['background']['image'] = array(
383 '#type' => 'select',
384 '#title' => t('Background Type'),
385 '#options' => $image_options,
386 '#default_value' => isset($preset['settings']['background']['image'])
387 ? $preset['settings']['background']['image']
388 : ''
389 ,
390 '#ahah' => array(
391 'path' => 'js/textimage/background',
392 'wrapper' => 'textimage-preview-text'
393 ),
394 '#description' => t('Select the background type to be used in this image.'),
395 );
396 $form['settings']['background']['xoffset'] = array(
397 '#type' => 'textfield',
398 '#title' => t('Text X-Offset'),
399 '#field_suffix' => 'px',
400 '#default_value' => isset($preset['settings']['background']['xoffset'])
401 ? $preset['settings']['background']['xoffset']
402 : 0,
403 '#maxlength' => 4,
404 '#size' => 4,
405 '#validate' => array('_textimage_number_validate' => array('xoffset')),
406 );
407 $form['settings']['background']['yoffset'] = array(
408 '#type' => 'textfield',
409 '#title' => t('Text Y-Offset'),
410 '#field_suffix' => 'px',
411 '#description' => t('Specify the x and y coordinates on the image you where the top-left corner of the dynamic text should be positioned.'),
412 '#default_value' => isset($preset['settings']['background']['yoffset'])
413 ? $preset['settings']['background']['yoffset']
414 : 0,
415 '#maxlength' => 4,
416 '#size' => 4,
417 '#validate' => array('_textimage_number_validate' => array('yoffset')),
418 );
419
420 // Preview Textimage
421 $form['settings']['preview'] = array(
422 '#type' => 'fieldset',
423 '#title' => t('Preview settings'),
424 '#collapsible' => TRUE,
425 '#collapsed' => TRUE,
426 );
427 $form['settings']['preview']['text'] = array(
428 '#prefix' => '<div id="textimage-preview-text">',
429 '#suffix' => '</div>',
430 );
431 $form['settings']['preview']['text']['default'] = array(
432 '#type' => 'textarea',
433 '#title' => t('Preview text'),
434 '#default_value' => isset($preset['settings']['preview']['text']['default'])
435 ? $preset['settings']['preview']['text']['default']
436 : t('Preview')
437 ,
438 '#rows' => 2,
439 );
440
441 // Add additional text fields.
442 if (isset($preset['settings']['preview']['text']['additional'])) {
443 foreach ($preset['settings']['preview']['text']['additional'] as $id => $value) {
444 $form['settings']['preview']['text']['additional'][$id] = array(
445 '#type' => 'textarea',
446 '#title' => t('Additional text'),
447 '#default_value' => isset($preset['settings']['preview']['text']['additional'][$id])
448 ? $preset['settings']['preview']['text']['additional'][$id]
449 : t('Preview')
450 ,
451 '#rows' => 2,
452 );
453 }
454 }
455
456 $form['settings']['preview']['button'] = array(
457 '#type' => 'button',
458 '#value' => t('Preview'),
459 '#ahah' => array(
460 'path' => 'js/textimage/preview',
461 'wrapper' => 'textimage-preview'
462 ),
463 );
464
465 $form['submit'] = array(
466 '#type' => 'submit',
467 '#value' => t('Submit'),
468 );
469
470 return $form;
471 }
472
473 function theme_textimage_preset_edit(&$form) {
474 drupal_add_css(drupal_get_path('module', 'textimage') . '/misc/css/textimage_admin.css');
475
476 drupal_render_children($form);
477 }
478
479 function textimage_preset_edit_validate($form, &$form_state) {
480 // Check for illegal characters in preset names
481 if (preg_match('/[^0-9a-zA-Z_\-]/', $form_state['values']['name'])) {
482 form_set_error('name', t('Please only use alphanumic characters, underscores (_), and hyphens (-) for preset names.'));
483 }
484
485 // Check for numbers at beginning of preset names
486 if (preg_match('/^[0-9]/', $form_state['values']['name'])) {
487 form_set_error('name', t('Preset names can\'t begin with a number.'));
488 }
489
490 // Check for duplicate preset names
491 $preset = _textimage_preset_load($form_state['values']['name']);
492 if ($preset['name'] && (!isset($form_state['values']['pid']) || $form_state['values']['pid'] != $preset['pid'])) {
493 form_set_error('name', t('The name %name is already in use by another preset.', array('%name' => $form_state['values']['name'])));
494 }
495 }
496
497 function textimage_preset_edit_submit($form, &$form_state) {
498 _textimage_preset_flush(0);
499
500 if (!isset($form_state['values']['pid'])) {
501 $return = _textimage_preset_create($form_state['values']['name'], $form_state['values']['description'], $form_state['values']['settings']);
502 }
503 elseif (is_numeric($form_state['values']['pid'])) {
504 $return = _textimage_preset_update($form_state['values']['pid'], $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['settings']);
505 }
506
507 if ($return) {
508 _textimage_flush_cache();
509 drupal_set_message(t('Updated preset %name', array('%name' => $form_state['values']['name'])));
510 drupal_goto('admin/build/textimage/preset/list');
511 }
512 else {
513 drupal_set_message(t('The preset was unable to be updated.'), 'error');
514 }
515 }
516
517 function textimage_preset_list() {
518 $fonts_path = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts');
519 $presets = textimage_get_presets();
520
521 $header = array(t('Name'), t('Summary'), t('Description'), array('data' => t('Operations'), 'colspan' => '3'));
522 $rows = array();
523
524 foreach ($presets as $preset) {
525 $font = _textimage_font_name($fonts_path . '/' . $preset->settings['font']['file']);
526
527 $rows[] = array(
528 $preset->name,
529 $font['name'] . ' ' . $preset->settings['font']['size'] . t('px'),
530 $preset->description,
531 l(t('edit'), 'admin/build/textimage/preset/' . $preset->pid . '/edit'),
532 l(t('delete'), 'admin/build/textimage/preset/' . $preset->pid . '/delete'),
533 l(t('flush cache'), 'admin/build/textimage/preset/' . $preset->pid . '/flush'),
534 );
535 }
536
537 if (empty($rows)) {
538 $rows[] = array(array('data' => t('No presets defined. <a href="!url">Create a new preset</a>.', array('!url' => url('admin/build/textimage/preset/new'))), 'colspan' => '6', 'class' => 'message'));
539 }
540
541 return theme('table', $header, $rows);
542 }
543
544 function textimage_preset_delete_confirm(&$form_state, $pid) {
545 $preset = _textimage_preset_load($pid);
546
547 $form['pid'] = array('#type' => 'value', '#value' => $preset['pid']);
548 $form['name'] = array('#type' => 'value', '#value' => $preset['name']);
549
550 return confirm_form($form,
551 t('Are you sure you want to delete the preset %name?', array('%name' => $preset['name'])),
552 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/textimage/preset/list',
553 t('This action cannot be undone.'),
554 t('Delete'), t('Cancel')
555 );
556 }
557
558 function textimage_preset_delete_confirm_submit($form, &$form_state) {
559 _textimage_preset_delete($form_state['values']['pid']);
560 _textimage_flush_cache();
561 drupal_set_message(t('Deleted preset %name', array('%name' => $form_state['values']['name'])));
562 drupal_goto('admin/build/textimage/preset/list');
563 }
564
565 function textimage_preset_flush_confirm(&$form_state, $pid) {
566 $preset = _textimage_preset_load($pid);
567
568 $form['pid'] = array('#type' => 'value', '#value' => $preset['pid']);
569 $form['name'] = array('#type' => 'value', '#value' => $preset['name']);
570
571 return confirm_form($form,
572 t('Are you sure you want to flush the image cache for preset %name?', array('%name' => $preset['name'])),
573 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/textimage/preset/list',
574 t('This action cannot be undone.'),
575 t('Flush Cache'), t('Cancel')
576 );
577 }
578
579 function textimage_preset_flush_confirm_submit($form, &$form_state) {
580 _textimage_preset_flush($form_state['values']['pid']);
581 drupal_goto('admin/build/textimage/preset/list');
582 }
583
584 function textimage_get_presets() {
585 static $presets = array();
586
587 if (empty($presets)) {
588 $result = db_query("SELECT pid, name, description, settings FROM {textimage_preset} ORDER BY name");
589 $presets = array();
590 foreach ($result as $preset) {
591 $preset->settings = unserialize($preset->settings);
592 $presets[$preset->name] = $preset;
593 }
594 }
595
596 return $presets;
597 }
598
599 /**
600 * create a preset
601 * @param name
602 * name of the preset to be created.
603 */
604 function _textimage_preset_create($name, $description, $settings) {
605 return db_insert('textimage_preset')
606 ->fields(array('name' => $name, 'description' => $description, 'settings' => serialize($settings)))
607 ->execute();
608 }
609
610 /**
611 * update a preset
612 * @param id
613 * preset id
614 * @param name
615 * new name for the preset
616 */
617 function _textimage_preset_update($id, $name, $description, $settings) {
618 $name = check_plain($name);
619 _textimage_preset_flush($id);
620 return db_update('textimage_preset')
621 ->fields(array('name' => $name, 'description' => $description, 'settings' => serialize($settings)))
622 ->condition('pid', $id)
623 ->execute();
624 }
625
626 function _textimage_preset_delete($id) {
627 _textimage_preset_flush($id);
628 return db_delete('textimage_preset')
629 ->condition('pid', $id)
630 ->execute();
631 }
632
633 /**
634 * flush cached media for a preset.
635 * @param pid
636 * a preset id.
637 */
638 function _textimage_preset_flush($pid) {
639 if ($pid !== 0) {
640 drupal_set_message(t('Flushed Preset Images (ID: @pid)', array('@pid' => $pid)));
641 }
642
643 $preset = _textimage_preset_load($pid);
644 $presetdir = realpath(file_directory_path() . '/textimage/' . $pid);
645
646 if (is_dir($presetdir)) {
647 _textimage_recursive_delete($presetdir);
648 @unlink($presetdir);
649 }
650
651 db_delete('textimage_image')
652 ->condition('pid', $pid)
653 ->execute();
654 }
655
656 function _textimage_flush_cache() {
657 if (module_exists('content')) {
658 cache_clear_all('*', 'cache_content', TRUE);
659 }
660 }
661
662 /**
663 * Recursively delete all files and folders in the specified filepath, then
664 * delete the containing folder Note that this only deletes visible files with
665 * write permission
666 *
667 * @param string $path
668 * An absolute filepath (relative to the filesystem) to delete
669 */
670 function _textimage_recursive_delete($path) {
671 $listing = $path . "/*";
672
673 foreach (glob($listing) as $file) {
674 if (is_file($file) === TRUE) {
675 @unlink($file);
676 }
677 elseif (is_dir($file) === TRUE) {
678 _textimage_recursive_delete($file);
679 @rmdir($file);
680 }
681 }
682
683 @rmdir($path);
684 }
685
686 /**
687 * Returns an array of files with .otf/.ttf extensions in the specified directory.
688 *
689 * @param $fontdir
690 * Full path of the font directory.
691 * @return
692 * Array of font files.
693 */
694 function _textimage_font_list($fontdir) {
695 $filelist = array();
696 if (is_dir($fontdir) && $handle = opendir($fontdir)) {
697 while ($file = readdir($handle)) {
698 if (preg_match("/\.[ot]tf$/i", $file) == 1) {
699 $font = _textimage_font_name($fontdir . '/' . $file);
700 $filelist[$file] = $font['name'];
701 }
702 }
703 closedir($handle);
704 }
705
706 asort($filelist);
707 return $filelist;
708 }
709
710 function _textimage_font_name($filename) {
711 $fd = fopen($filename, "r");
712 $text = fread($fd, filesize($filename));
713 fclose($fd);
714
715 $number_of_tabs = _textimage_dec2hex(ord($text[4])) . _textimage_dec2hex(ord($text[5]));
716 for ($i=0; $i < hexdec($number_of_tabs); $i++) {
717 $tag = $text[12 + $i *16] . $text[12 + $i * 16 + 1] . $text[12 + $i * 16 + 2] . $text[12 + $i * 16 + 3];
718 if ($tag == "name") {
719 $offset_name_table_hex = _textimage_dec2hex(ord($text[12 + $i * 16 + 8])) . _textimage_dec2hex(ord($text[12 + $i * 16 + 8 + 1])) . _textimage_dec2hex(ord($text[12 + $i * 16 + 8 + 2])) . _textimage_dec2hex(ord($text[12 + $i * 16 + 8 + 3]));
720 $offset_name_table_dec = hexdec($offset_name_table_hex);
721 $offset_storage_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec+4])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 5]));
722 $offset_storage_dec = hexdec($offset_storage_hex);
723 $number_name_records_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec + 2])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 3]));
724 $number_name_records_dec = hexdec($number_name_records_hex);
725 break;
726 }
727 }
728
729 $storage_dec = $offset_storage_dec + $offset_name_table_dec;
730 $storage_hex = drupal_strtoupper(dechex($storage_dec));
731 $font = array('copyright' => '', 'family' => '', 'subfamily' => '', 'name' => '');
732
733 for ($j = 0; $j < $number_name_records_dec; $j++) {
734 $platform_id_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 0])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 1]));
735 $platform_id_dec = hexdec($platform_id_hex);
736 $name_id_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 6])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 7]));
737 $name_id_dec = hexdec($name_id_hex);
738 $string_length_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 8])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 9]));
739 $string_length_dec = hexdec($string_length_hex);
740 $string_offset_hex = _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 10])) . _textimage_dec2hex(ord($text[$offset_name_table_dec + 6 + $j * 12 + 11]));
741 $string_offset_dec = hexdec($string_offset_hex);
742
743 if ($name_id_dec == 0 && empty($font['copyright'])) {
744 for ($l = 0; $l < $string_length_dec; $l++) {
745 if (ord($text[$storage_dec + $string_offset_dec + $l]) >= 32) {
746 $font['copyright'] .= $text[$storage_dec + $string_offset_dec + $l];
747 }
748 }
749 }
750
751 if ($name_id_dec == 1 && empty($font['family'])) {
752 for ($l = 0; $l < $string_length_dec; $l++) {
753 if (ord($text[$storage_dec + $string_offset_dec + $l]) >= 32) {
754 $font['family'] .= $text[$storage_dec + $string_offset_dec + $l];
755 }
756 }
757 }
758
759 if ($name_id_dec == 2 && empty($font['subfamily'])) {
760 for ($l = 0; $l < $string_length_dec; $l++) {
761 if (ord($text[$storage_dec + $string_offset_dec + $l]) >= 32) {
762 $font['subfamily'] .= $text[$storage_dec + $string_offset_dec + $l];
763 }
764 }
765 }
766
767 if ($name_id_dec == 4 && empty($font['name'])) {
768 for ($l = 0; $l < $string_length_dec; $l++) {
769 if (ord($text[$storage_dec + $string_offset_dec + $l]) >= 32) {
770 $font['name'] .= $text[$storage_dec + $string_offset_dec + $l];
771 }
772 }
773 }
774
775 if ($font['copyright'] != "" && $font['family'] != "" && $font['subfamily'] != "" && $font['name'] != "") {
776 break;
777 }
778 }
779
780 return $font;
781 }
782
783 function _textimage_dec2hex($dec) {
784 $hex = dechex($dec);
785 return str_repeat("0", 2 - drupal_strlen($hex)) . drupal_strtoupper($hex);
786 }
787
788 function _textimage_number_validate($field, $field_name) {
789 if (!empty($field['#value']) && !is_numeric($field['#value'])) {
790 form_set_error($field_name, t('The value for %field must be a number.', array('%field' => $field['#title'])));
791 }
792 }
793
794 function _textimage_hex_validate($field, $field_name) {
795 if (!empty($field['#value']) && !preg_match('/^#[0123456789ABCDEF]{1,6}$/i', $field['#value'])) {
796 form_set_error($field_name, t('The value for %field must be in a hexidecimal format (i.e. #FFFFFF is white).', array('%field' => $field['#title'])));
797 }
798 }
799
800 /**
801 * Returns an array of files with jpg, png, and gif extensions in the specified directory.
802 *
803 * @param $imagesdir
804 * Full path of the images directory.
805 * @return
806 * Array of image files.
807 */
808 function _textimage_image_list($imagesdir) {
809 $filelist = array();
810 if (is_dir($imagesdir) && $handle = opendir($imagesdir)) {
811 while ($file = readdir($handle)) {
812 if (preg_match("/\.gif|\.png|\.jpg$/i", $file) == 1) {
813 $filelist[] = $imagesdir . '/' . $file;
814 }
815 }
816 closedir($handle);
817 }
818
819 return $filelist;
820 }

  ViewVC Help
Powered by ViewVC 1.1.2