Jump to:
| Project: | Textimage |
| Version: | 6.x-2.x-dev |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | minor |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
Firstly, great work on the module. This is very well crafted and works like a charm!
I'm developing a carbon trading system and it is generating one off certificates for purchases, and I thought that I would share the code as a jump start for those developers who find reading code difficult. This would go in the developers / advanced sections of any doco.
Creating non-file based streaming images.
This is a starter to those developers that need textimage for one off or privately streamed images.
Step 1: Create your textimage preset
Step 2: Define your menu callback
<?php
/**
* Implementation of hook_menu().
*/
function example_menu() {
$items = array();
$items['example/%'] = array(
'title' => t('Text image raw feed example'),
'type' => MENU_CALLBACK,
'page callback' => 'example_textimage_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
?>Step 3: Create the image feed
<?php
function example_textimage_callback($preset_name) {
// Integrety check
$format = 'jpeg';
$text = 'The last preset text in the chain';
$additional_text = array('More text for nested presets');
$output_function = 'image'. $format;
// Load preset
if (!$preset = _textimage_preset_load($preset_name)) {
$message = t('Unable to generate Textimage as the preset %preset is not defined.', array('%preset' => $preset_name));
watchdog('textimage', $message, WATCHDOG_ERROR);
drupal_set_message($message, 'error');
drupal_goto('');
}
// Generate the image resource
$img = textimage_image_from_preset($preset['pid'], $text, $additional_text);
ob_start();
$output_function($img);
$image_data = ob_get_contents();
$image_length = ob_get_length();
ob_end_clean();
imagedestroy($img);
$headers = array(
'Content-type: image/jpeg',
'Content-Length: '. $image_length,
'Cache-Control: max-age=0, no-cache',
'Expires: '. gmdate('D, d M Y H:i:s', time()) .' GMT',
'Last-Modified: '. time(),
);
foreach ($headers as $header) {
$header = preg_replace('/\r?\n(?!\t| )/', '', $header);
drupal_set_header($header);
}
print $image_data;
exit();
}
?>Step 4: Flush and test
Flush the menu cache and test the callback. This example bypasses any checks on the preset usage. A more relevant example would be to pass through an ID of an object that you want to render, and to hard code the preset within the callback. You would use the results of the loaded object to set the text and additional text fields.
Happy programming
Comments
#1
Hi Alan,
Example looks great. I would be more than happy to link to a page/blog entry with this example if you have one, as I have done with the three howto's I've put together on my blog.
Cheers,
Deciphered.
#2
If things look good, you can add it to yours. You have done all the hard work and adding it besides the other examples makes sense.
Cheers
Alan