View source
<?php
define('PREG_CLASS_PUNCTUATION', '\\x{21}-\\x{23}\\x{25}-\\x{2a}\\x{2c}-\\x{2f}\\x{3a}\\x{3b}\\x{3f}\\x{40}\\x{5b}-\\x{5d}' . '\\x{5f}\\x{7b}\\x{7d}\\x{a1}\\x{ab}\\x{b7}\\x{bb}\\x{bf}\\x{37e}\\x{387}\\x{55a}-\\x{55f}' . '\\x{589}\\x{58a}\\x{5be}\\x{5c0}\\x{5c3}\\x{5f3}\\x{5f4}\\x{60c}\\x{60d}\\x{61b}\\x{61f}' . '\\x{66a}-\\x{66d}\\x{6d4}\\x{700}-\\x{70d}\\x{964}\\x{965}\\x{970}\\x{df4}\\x{e4f}' . '\\x{e5a}\\x{e5b}\\x{f04}-\\x{f12}\\x{f3a}-\\x{f3d}\\x{f85}\\x{104a}-\\x{104f}\\x{10fb}' . '\\x{1361}-\\x{1368}\\x{166d}\\x{166e}\\x{169b}\\x{169c}\\x{16eb}-\\x{16ed}\\x{1735}' . '\\x{1736}\\x{17d4}-\\x{17d6}\\x{17d8}-\\x{17da}\\x{1800}-\\x{180a}\\x{1944}\\x{1945}' . '\\x{2010}-\\x{2027}\\x{2030}-\\x{2043}\\x{2045}-\\x{2051}\\x{2053}\\x{2054}\\x{2057}' . '\\x{207d}\\x{207e}\\x{208d}\\x{208e}\\x{2329}\\x{232a}\\x{23b4}-\\x{23b6}\\x{2768}-' . '\\x{2775}\\x{27e6}-\\x{27eb}\\x{2983}-\\x{2998}\\x{29d8}-\\x{29db}\\x{29fc}\\x{29fd}' . '\\x{3001}-\\x{3003}\\x{3008}-\\x{3011}\\x{3014}-\\x{301f}\\x{3030}\\x{303d}\\x{30a0}' . '\\x{30fb}\\x{fd3e}\\x{fd3f}\\x{fe30}-\\x{fe52}\\x{fe54}-\\x{fe61}\\x{fe63}\\x{fe68}' . '\\x{fe6a}\\x{fe6b}\\x{ff01}-\\x{ff03}\\x{ff05}-\\x{ff0a}\\x{ff0c}-\\x{ff0f}\\x{ff1a}' . '\\x{ff1b}\\x{ff1f}\\x{ff20}\\x{ff3b}-\\x{ff3d}\\x{ff3f}\\x{ff5b}\\x{ff5d}\\x{ff5f}-' . '\\x{ff65}');
define('PREG_CLASS_SEPARATOR', '\\x{20}\\x{a0}\\x{1680}\\x{180e}\\x{2000}-\\x{200a}\\x{2028}\\x{2029}\\x{202f}\\x{205f}\\x{3000}');
define('ALIGN_LEFT', 1);
define('ALIGN_CENTER', 2);
define('ALIGN_RIGHT', 3);
function textimage_help($section) {
$output = "";
switch ($section) {
case 'admin/help#textimage':
$output = '
<h2 id="textimage-introduction">The dynamic text to image generator!</h2>
<p>
Textimage adds text to image functionality using GD2 and Freetype, enabling users to create crisp images on the fly for use as theme objects, headings or limitless other possibilities.
</p>
<h2>Installing Fonts</h2>
<p>
Before you can begin using Textimage you must upload at least one TrueType or OpenType font to the server and tell Textimage where you uploaded it. Fonts must have a .tff or .otf extension to be seen by Textimage. If you do not have any TrueType or OpenType fonts, you can download some free GNU fonts from the <a href="http://savannah.nongnu.org/projects/freefont/">Free UCS Outline Fonts Project</a>. Once the fonts are uploaded, enter the UNIX-style path the fonts on the <a href="!config">configuration page</a>.
</p>
<h2 id="textimage-configuration">Configuration</h2>
<p>
The basis of Textimage is made of configuration options called <em>presets</em>. A preset defines what font, size, color, etc. should be used in the generated image. You can create new presets on the <a href="!presets">presets page</a>. Most options are pretty self explanatory, but background images can get pretty complicated if you begin to use other presets as backgrounds. Let\'s run through an example.
</p>
<p>
If you specified a backgrounds directory on the main configuration page, a list of backgrounds is automatically popuplated into the Background Image select list. Let\'s say there\'s a image called "header.png" in the image list that looks like this:
</p>
<p>
<img src="!example1" alt="example1" />
</p>
<p>
Now we\'ll create a preset called "preset1". In this preset, set the font to Braggadocio (not included), 54px, #FFFFFF (white) color. Select header.png from the background list and position the text at x-offset 14 and y-offset 22 (in pixels). After the preset is saved, Textimage is now ready to automatically generate images based on text strings. You could directly visit the image at {files}/textimage/preset1/Hello.png (where {files} is your sites file directory) and get the following result:
</p>
<p>
<img src="!example2" alt="example2" />
</p>
<p>
To get crazy now, create a new preset called "preset2". In this preset, set the font to Century (also not included), 20px, #000000 (black) color. Select <em>preset1</em> from the background list and position the text at x-offset 14 and y-offset 94. Save the preset then visit {files}/textimage/preset2/Hello/world!.png and get the following result:
</p>
<p>
<img src="!example3" alt="example3" />
</p>
<p>
The entire preset1 is generated using the first argument <em>Hello</em>. Then preset2 is generated using the name of the file <em>world!.png</em>. You could continue chaining presets together over and over again.
</p>
<h2>File Names and Storage</h2>
<p>
Textimage supports .png, .gif, and .jpg input and output files. You can change the output format of the image simply by changing the extension of the last file. In the above example we made PNG images. If we had appended it with .jpg, a JPG image would have been created. Only PNG and GIF files support transparent backgrounds.
</p>
';
$output = t($output, array(
'!config' => url('admin/build/textimage/settings'),
'!presets' => url('admin/build/textimage/presets'),
'!example1' => base_path() . drupal_get_path('module', 'textimage') . '/misc/example1.png',
'!example2' => base_path() . drupal_get_path('module', 'textimage') . '/misc/example2.png',
'!example3' => base_path() . drupal_get_path('module', 'textimage') . '/misc/example3.png',
));
break;
case 'admin/build/modules#description':
case 'admin/build/modules/textimage':
case 'admin/build/textimage':
$output = t('Provides text to image manipulations.');
break;
}
return $output;
}
function textimage_menu($may_cache) {
$items = array();
if (!$may_cache) {
if (arg(0) == 'admin') {
include_once './' . drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
}
}
if ($may_cache) {
$items[] = array(
'path' => file_directory_path() . '/textimage',
'callback' => 'textimage_image',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/textimage',
'title' => t('Textimage'),
'description' => t('Configure text to image preset functions.'),
'callback' => 'textimage_preset_list',
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'admin/build/textimage/preset',
'title' => t('Presets'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/textimage/preset/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/textimage/preset/new',
'title' => t('New'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'textimage_preset_edit',
'new',
),
'access' => user_access('administer site configuration'),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/textimage/settings',
'title' => t('Settings'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'textimage_settings_form',
),
'access' => user_access('administer site configuration'),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'js/textimage',
'callback' => 'textimage_js',
'access' => user_access('access content'),
'type' => MENU_CALLBACK,
);
}
else {
$items[] = array(
'path' => 'admin/build/textimage/preset/' . arg(4) . '/edit',
'title' => t('Edit Preset'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'textimage_preset_edit',
'edit',
arg(4),
),
'access' => user_access('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/textimage/preset/' . arg(4) . '/delete',
'title' => t('Edit Preset'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'textimage_preset_delete_confirm',
arg(4),
),
'access' => user_access('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/textimage/preset/' . arg(4) . '/flush',
'title' => t('Flush Preset Cache'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'textimage_preset_flush_confirm',
arg(4),
),
'access' => user_access('administer site configuration'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
function textimage_perm() {
return array(
'create textimages',
);
}
function textimage_image() {
$pattern = '/' . str_replace('/', '\\/', base_path() . '(\\?q=)?' . file_directory_path() . '/textimage/') . '/';
$args = explode('/', preg_replace($pattern, '', request_uri()));
$preset = array_shift($args);
if (is_numeric($preset)) {
drupal_not_found();
exit;
}
$filename = urldecode(array_pop($args));
$additional_text = $args;
preg_match('/\\.([a-z]+)$/i', $filename, $matches);
$format = $matches[1];
if ($format == 'jpg') {
$format = 'jpeg';
}
$text = preg_replace('/\\.([a-z]+)$/i', '', $filename);
if (!($img = textimage_build_image('url', $preset, $text, $additional_text, $format))) {
return FALSE;
}
drupal_goto($img);
}
function textimage_build_image($method, $preset, $text, $additional_text = array(), $format = 'png') {
$output_function = 'image' . $format;
if (!function_exists($output_function)) {
$message = t('Unable to generate Textimage as the file extension is unsupported on this system. Files must have a .png, .jpg, or .gif extension.');
watchdog('textimage', $message, WATCHDOG_ERROR);
drupal_set_message($message, 'error');
return FALSE;
}
if (!is_array($preset) && !($preset = _textimage_preset_load($preset))) {
$message = t('Unable to generate Textimage as the preset %preset is not defined.', array(
'%preset' => $preset,
));
watchdog('textimage', $message, WATCHDOG_ERROR);
drupal_set_message($message, 'error');
return FALSE;
}
$result = db_query("SELECT file FROM {textimage_image} WHERE pid = %d AND data = '%s'", $preset['pid'], serialize(array(
'format' => $format,
'text' => $text,
'additional_text' => $additional_text,
)));
$result = db_fetch_object($result);
if (!$result && (user_access('create textimages') || $method == 'theme') || $preset['pid'] == 0) {
$img = textimage_image_from_preset($preset, $text, $additional_text);
$filename = time() . rand(1000, 9999) . '.' . $format;
$directory = file_directory_path() . '/textimage/' . $preset['pid'];
textimage_directory_check($directory);
$output_function($img, $directory . '/' . $filename);
imagedestroy($img);
if ($preset['pid'] !== 0) {
db_query("INSERT INTO {textimage_image} (pid, file, data) VALUES ('%s', '%s', '%s')", $preset['pid'], $directory . '/' . $filename, serialize(array(
'format' => $format,
'text' => $text,
'additional_text' => $additional_text,
)));
}
return $directory . '/' . $filename;
}
elseif (!$result) {
drupal_access_denied();
return FALSE;
}
else {
return $result->file;
}
}
function textimage_image_from_preset($preset, $text, $additional_text = array()) {
foreach ($preset['settings'] as $key => $settings) {
if (is_array($settings)) {
foreach ($settings as $name => $value) {
${$key . '_' . $name} = $value;
}
}
}
$text_fixed_width = isset($text_fixed_width) ? $text_fixed_width : 0;
$font_file = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts') . '/' . $font_file;
switch ($text_case) {
case 'upper':
$text = drupal_strtoupper($text);
break;
case 'lower':
$text = drupal_strtolower($text);
break;
case 'ucfirst':
$text = drupal_ucfirst($text);
break;
case 'ucwords':
$text = preg_replace('/\\s(\\w+)\\b/e', "drupal_ucfirst('\$1')", $text);
break;
}
$img = textimage_text_to_image($text, $font_size, $font_file, $font_color, $text_angle, $text_maximum_width, $text_fixed_width, $text_align);
if ($text_stroke['width'] && $text_stroke['color']) {
$img = textimage_image_add_stroke($img, $text_stroke['width'], $text_stroke['color'], $font_color['opacity']);
}
if ($text_margin['top'] || $text_margin['right'] || $text_margin['bottom'] || $text_margin['left']) {
$img = textimage_image_add_margin($img, $text_margin);
}
if (is_numeric($background_image) && ($background_image = _textimage_preset_load($background_image))) {
$next_preset = $background_image;
$next_text = array_pop($additional_text);
if (empty($next_text)) {
$next_text = $text;
}
$background_resource = textimage_image_from_preset($next_preset, $next_text, $additional_text);
$text_width = imagesx($img);
$text_height = imagesy($img);
imagealphablending($background_resource, TRUE);
imagecopy($background_resource, $img, $background_xoffset, $background_yoffset, 0, 0, $text_width, $text_height);
imagesavealpha($background_resource, TRUE);
$img = $background_resource;
}
elseif (is_file($background_image)) {
$info = image_get_info($background_image);
$background_resource = image_toolkit_invoke('open', array(
$background_image,
$info['extension'],
));
$text_width = imagesx($img);
$text_height = imagesy($img);
imagealphablending($background_resource, TRUE);
imagecopy($background_resource, $img, $background_xoffset, $background_yoffset, 0, 0, $text_width, $text_height);
imagesavealpha($background_resource, TRUE);
$img = $background_resource;
}
else {
$background_resource = imagecreatetruecolor(imagesx($img), imagesy($img));
$alpha = 0;
if (!$background_color) {
$alpha = 127;
$background_color = '#FFFFFF';
}
list($r, $g, $b) = _textimage_hex2rgb($background_color);
$back = imagecolorallocatealpha($background_resource, $r, $g, $b, $alpha);
imagefill($background_resource, 0, 0, $back);
imagealphablending($background_resource, TRUE);
imagecopy($background_resource, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
imagesavealpha($background_resource, TRUE);
$img = $background_resource;
}
return $img;
}
function textimage_image_add_margin($img, $margin) {
$width = imagesx($img);
$height = imagesy($img);
$back_img = imagecreatetruecolor($width + $margin['right'] + $margin['left'], $height + $margin['top'] + $margin['bottom']);
$back = imagecolorallocatealpha($back_img, 0, 0, 0, 127);
imagefill($back_img, 0, 0, $back);
imagecopy($back_img, $img, $margin['left'], $margin['top'], 0, 0, $width, $height);
imagealphablending($back_img, TRUE);
imagesavealpha($back_img, TRUE);
return $back_img;
}
function textimage_image_add_stroke($img, $thickness, $color, $text_opacity) {
if ($thickness > 0) {
$width = imagesx($img);
$height = imagesy($img);
$border_img = imagecreatetruecolor($width, $height);
$back = imagecolorallocatealpha($border_img, 0, 0, 0, 127);
imagefill($border_img, 0, 0, $back);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$c = imagecolorsforindex($img, imagecolorat($img, $x, $y));
if ($c['alpha'] > -($text_opacity - 100) / 100 * 127) {
textimage_image_stroke_change_pixels($img, $border_img, $thickness, $color, $x, $y, $width, $height);
}
}
}
imagealphablending($img, TRUE);
imagecopy($img, $border_img, 0, 0, 0, 0, $width, $height);
}
return $img;
}
function textimage_image_stroke_change_pixels(&$img, &$border_img, $thickness, $color, $x, $y, $width, $height) {
list($r, $g, $b) = _textimage_hex2rgb($color);
$pixel = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$degree_increment = 90 / $thickness;
$radial_coords = array();
for ($degrees = 0; $degrees <= 90; $degrees += $degree_increment) {
$x_offset = round(cos($degrees) * $thickness);
$y_offset = round(sin($degrees) * $thickness);
$radial_coords[] = array(
'x' => $x + $x_offset,
'y' => $y + $y_offset,
);
$radial_coords[] = array(
'x' => $x - $x_offset,
'y' => $y + $y_offset,
);
$radial_coords[] = array(
'x' => $x + $x_offset,
'y' => $y - $y_offset,
);
$radial_coords[] = array(
'x' => $x - $x_offset,
'y' => $y - $y_offset,
);
}
$total_alpha = 0;
$total_colors = 0;
foreach ($radial_coords as $coords) {
if ($coords['x'] >= 0 && $coords['y'] >= 0 && $coords['x'] < $width && $coords['y'] < $height) {
$xy_color = imagecolorsforindex($img, imagecolorat($img, $coords['x'], $coords['y']));
}
else {
$xy_color = array(
'alpha' => '127',
);
}
$total_alpha += $xy_color['alpha'];
$total_colors++;
}
if ($total_alpha < 127 * $total_colors && $total_alpha > 0) {
if ($pixel['alpha'] < 127) {
$alpha = 127 - $pixel['alpha'];
}
else {
$alpha = 127 - (127 * $total_colors - $total_alpha);
}
$alpha = $alpha < 0 ? 0 : $alpha;
$alpha = $alpha > 127 ? 127 : $alpha;
$color = imagecolorallocatealpha($border_img, $r, $g, $b, $alpha);
imagesetpixel($border_img, $x, $y, $color);
}
}
function textimage_directory_check($directory_path) {
foreach (explode('/', $directory_path) as $dir) {
$dirs[] = $dir;
if (!file_check_directory(implode($dirs, '/'), FILE_CREATE_DIRECTORY)) {
return FALSE;
}
}
return TRUE;
}
function textimage_measure_text_width($text, $fontsize, $font) {
$box = imagettfbbox($fontsize, 0, $font, $text);
return abs($box[4] - $box[0]) + 4;
}
function textimage_wrap_text($text, $fontsize, $font, $maximum_width) {
$end = 0;
$begin = 0;
$fit = $begin;
while (true) {
if (drupal_preg_match('/[' . PREG_CLASS_PUNCTUATION . '][' . PREG_CLASS_SEPARATOR . ']*|[' . PREG_CLASS_SEPARATOR . ']+/u', $text, $match, PREG_OFFSET_CAPTURE, $end)) {
$end = $match[0][1] + drupal_strlen($match[0][0]);
}
else {
$end = drupal_strlen($text);
}
$line = preg_replace('/[' . PREG_CLASS_SEPARATOR . ']+$/u', '', drupal_substr($text, $begin, $end - $begin));
$width = textimage_measure_text_width($line, $fontsize, $font);
if ($width > $maximum_width) {
if ($fit == $begin) {
while (drupal_strlen($line) > 0 && $width > $maximum_width) {
$line = drupal_substr($line, 0, -1);
$width = textimage_measure_text_width($line, $fontsize, $font);
}
$fit = drupal_strlen($line) ? $begin + drupal_strlen($line) : $end;
}
$text = drupal_substr($text, 0, $fit) . "\n" . drupal_substr($text, $fit);
$end = $begin = ++$fit;
}
else {
$fit = $end;
}
if ($end == drupal_strlen($text)) {
break;
}
}
return $text;
}
if (!function_exists('drupal_preg_match')) {
function drupal_preg_match($pattern, $subject, &$matches, $flags = NULL, $offset = 0) {
$offset = strlen(drupal_substr($subject, 0, $offset, $encoding));
$return_value = preg_match($pattern, $subject, $matches, $flags, $offset);
if ($return_value && $flags & PREG_OFFSET_CAPTURE) {
foreach ($matches as &$match) {
$match[1] = drupal_strlen(substr($subject, 0, $match[1]));
}
}
return $return_value;
}
}
function textimage_text_to_image($text, $fontsize, $font, $color = array(
'hex' => '#000000',
'opacity' => '100',
), $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT) {
$q_angle = -$angle;
while ($q_angle > 0 || $q_angle <= -90) {
$q_angle -= $q_angle > 0 ? 90 : -90;
}
while ($angle < 0 || $angle >= 360) {
$angle += $angle < 0 ? 360 : -360;
}
$rotation = -(floor($angle / 90) * 90);
$rad = deg2rad($q_angle);
$sin = -sin($rad);
$cos = cos($rad);
if ($maximum_width - 1 > 0) {
$text = textimage_wrap_text($text, $fontsize, $font, $maximum_width - 1);
}
$coords = imagettfbbox($fontsize, 0, $font, $text);
if ($coords[3] - $coords[5] < $fontsize * 2) {
$characters = drupal_map_assoc(range(33, 122), 'chr');
$characters = join($characters);
$testcoords = imagettfbbox($fontsize, 0, $font, $characters);
if ($coords[3] - $coords[5] < $testcoords[3] - $testcoords[5]) {
$coords[1] = $testcoords[1];
$coords[3] = $testcoords[3];
$coords[5] = $testcoords[5];
$coords[7] = $testcoords[7];
}
}
$bbox = array();
for ($i = 0; $i < 7; $i += 2) {
$bbox[$i] = round($coords[$i] * $cos + $coords[$i + 1] * -$sin);
$bbox[$i + 1] = round($coords[$i + 1] * $cos - $coords[$i] * -$sin);
}
$text_width = sqrt(pow(abs($bbox[0] - $bbox[2]), 2) + pow(abs($bbox[1] - $bbox[3]), 2));
$text_height = sqrt(pow(abs($bbox[0] - $bbox[6]), 2) + pow(abs($bbox[1] - $bbox[7]), 2));
$box_width = max($bbox[0], $bbox[2], $bbox[4], $bbox[6]) - min($bbox[0], $bbox[2], $bbox[4], $bbox[6]);
$box_height = max($bbox[1], $bbox[3], $bbox[5], $bbox[7]) - min($bbox[1], $bbox[3], $bbox[5], $bbox[7]);
$image_width = $fixed_width && $maximum_width > 0 ? ($maximum_width - 1) * $cos + $text_height * $sin : $box_width;
$image_height = $fixed_width && $maximum_width > 0 ? ($maximum_width - 1) * $sin + $text_height * $cos : $box_height;
$image = imagecreatetruecolor($image_width + 1, $image_height + 1);
$back = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $back);
$x = -$bbox[0];
$y = $box_height - $bbox[3];
if ($fixed_width && $maximum_width > 0 && $align !== ALIGN_LEFT) {
switch ($align) {
case ALIGN_CENTER:
$x += ($image_width - $box_width) / 2;
$y += ($image_height - $box_height) / 2;
break;
case ALIGN_RIGHT:
$x += $image_width - $box_width;
$y += $image_height - $box_height;
break;
}
}
list($r, $g, $b) = _textimage_hex2rgb($color['hex']);
$alpha = -($color['opacity'] - 100) / 100 * 127;
$fore = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
imagettftext($image, $fontsize, $q_angle, $x, $y, $fore, $font, $text);
if ($rotation != 0) {
$image = imagerotate($image, $rotation, 0);
}
imagealphablending($image, TRUE);
imagesavealpha($image, TRUE);
return $image;
}
if (!function_exists('imagerotate')) {
function imagerotate($im, $angle, $bgcolor) {
if ($angle === 0) {
return $im;
}
$angle = 360 + $angle;
$width = imagesx($im);
$height = imagesy($im);
list($r, $g, $b, $a) = _textimage_hex2rgb($bgcolor);
switch ($angle) {
case 270:
case 90:
$rot_width = $height;
$rot_height = $width;
break;
case 180:
$rot_width = $width;
$rot_height = $height;
break;
}
$rotate = imagecreatetruecolor($rot_width, $rot_height);
$bg = imagecolorallocatealpha($rotate, $r, $g, $b, $a);
imagefilledrectangle($rotate, 0, 0, $rot_width, $rot_height, $bg);
imagealphablending($rotate, FALSE);
imagesavealpha($rotate, TRUE);
switch ($angle) {
case 270:
$rot_width--;
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
imagesetpixel($rotate, $rot_width - $y, $x, imagecolorat($im, $x, $y));
}
}
break;
case 90:
$rot_height--;
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
imagesetpixel($rotate, $y, $rot_height - $x, imagecolorat($im, $x, $y));
}
}
break;
case 180:
$rot_width--;
$rot_height--;
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
imagesetpixel($rotate, $rot_width - $x, $rot_height - $y, imagecolorat($im, $x, $y));
}
}
break;
}
return $rotate;
}
}
function _textimage_preset_load($preset) {
if (is_numeric($preset)) {
$preset = db_fetch_array(db_query('SELECT * FROM {textimage_preset} WHERE pid = %d', $preset));
}
else {
$preset = db_fetch_array(db_query("SELECT * FROM {textimage_preset} WHERE name = '%s'", $preset));
}
if (empty($preset)) {
return FALSE;
}
else {
$preset['settings'] = unserialize($preset['settings']);
return $preset;
}
}
function _textimage_hex2rgb($hex) {
$r = $g = $b = '';
$hex = ltrim($hex, '#');
if (preg_match('/^[0-9a-f]{3}$/i', $hex)) {
$r = str_repeat($hex[0], 2);
$g = str_repeat($hex[1], 2);
$b = str_repeat($hex[2], 2);
}
elseif (preg_match('/^[0-9a-f]{6}$/i', $hex)) {
$r = drupal_substr($hex, 0, 2);
$g = drupal_substr($hex, 2, 2);
$b = drupal_substr($hex, 4, 2);
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array(
$r,
$g,
$b,
);
}
function textimage_field_formatter_info() {
$formatters = array();
include_once drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
foreach (textimage_get_presets() as $preset) {
$formatters['textimage_' . $preset->name] = array(
'label' => t('Textimage:') . ' ' . $preset->name,
'field types' => array(
'text',
'email',
),
);
}
return $formatters;
}
function textimage_field_formatter($field, $item, $formatter, $node) {
$alt = $title = $item['value'];
if ($field['type'] == 'email') {
$item['value'] = $item['email'];
$alt = $title = '';
}
return theme('textimage_image', drupal_substr($formatter, 10), $item['value'], array(), 'png', $alt, $title);
}
function theme_textimage_image($preset, $text, $additional_text = array(), $format = 'png', $alt = '', $title = '', $attributes = array(), $getsize = TRUE, $image = TRUE) {
if (!($path = textimage_build_image('theme', $preset, $text, $additional_text, $format))) {
return FALSE;
}
if ($image) {
return theme('image', $path, $alt, $title, $attributes, $getsize);
}
return $path;
}
function textimage_js() {
$output = '';
switch (arg(2)) {
case 'preview':
textimage_js_preview($output);
break;
case 'background':
textimage_js_background($output);
break;
}
print $output;
exit;
}
function textimage_js_preview(&$output) {
$text = $_POST['settings']['preview']['text']['default'];
$additional_text = array();
if (isset($_POST['settings']['preview']['text']['additional'])) {
$additional_text = $_POST['settings']['preview']['text']['additional'];
rsort($additional_text);
}
$preset = array(
'pid' => 0,
'name' => $_POST['name'],
'settings' => $_POST['settings'],
);
$output = theme_textimage_image($preset, $text, $additional_text, 'png', $text, $text);
}
function textimage_js_background(&$output) {
include_once './' . drupal_get_path('module', 'textimage') . '/textimage_admin.inc';
$form = call_user_func_array($_POST['form_id'], 'new');
foreach (element_children($form['settings']['preview']['text']) as $key) {
if ($key !== 'default') {
unset($form['settings']['preview']['text'][$key]);
}
}
if (is_numeric($_POST['settings']['background']['image']) && ($preset = _textimage_preset_load($_POST['settings']['background']['image']))) {
if (!isset($form['settings']['preview']['text']['additional'])) {
$form['settings']['preview']['text']['additional'] = array();
}
_textimage_js_background($form);
while (is_numeric($preset['settings']['background']['image']) && ($preset = _textimage_preset_load($preset['settings']['background']['image']))) {
_textimage_js_background($form);
}
}
drupal_prepare_form($_POST['form_id'], $form);
$form = form_builder($_POST['form_id'], $form);
$output = drupal_render($form['settings']['preview']['text']);
}
function _textimage_js_background(&$form) {
$id = count($form['settings']['preview']['text']['additional']);
$form['settings']['preview']['text']['additional'][$id] = $form['settings']['preview']['text']['default'];
$form['settings']['preview']['text']['additional'][$id]['#title'] = 'Additional text';
}