View source
<?php
function textactions_text2canvas_form($action) {
$defaults = array(
'size' => 12,
'angle' => 0,
'xpos' => 'left-10',
'ypos' => 'bottom-10',
'RGB' => array(
'red' => 51,
'green' => 51,
'blue' => 51,
'HEX' => '#333333',
),
'fontfile' => 'MgOpenModernaBold.ttf',
'text' => 'Hello World!',
);
$action = array_merge($defaults, (array) $action);
$form = array(
'size' => array(
'#type' => 'textfield',
'#title' => t('Size'),
'#default_value' => $action['size'],
'#description' => t('Size: The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).'),
'#size' => 3,
),
'angle' => array(
'#type' => 'textfield',
'#title' => t('Angle'),
'#default_value' => $action['angle'],
'#description' => t('Angle: The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.'),
'#size' => 3,
),
'alpha' => array(
'#type' => 'textfield',
'#title' => t('Opacity'),
'#default_value' => $action['alpha'] ? $action['alpha'] : 100,
'#size' => 3,
'#description' => t('Opacity: 1-100.'),
),
'xpos' => array(
'#type' => 'textfield',
'#title' => t('X offset'),
'#default_value' => $action['xpos'],
'#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>. Syntax like <em>right-20</em> is also valid.'),
'#size' => 10,
),
'ypos' => array(
'#type' => 'textfield',
'#title' => t('Y offset'),
'#default_value' => $action['ypos'],
'#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>. Syntax like <em>bottom-20</em> is also valid.'),
'#size' => 10,
),
'RGB' => imagecache_rgb_form($action['RGB']),
'fontfile' => array(
'#type' => 'textfield',
'#title' => t('Font file name'),
'#default_value' => $action['fontfile'],
'#description' => t('Font file is either the full system path (eg <code>/usr/share/fonts/truetype/ttf-bitstream-vera/VeraMono.ttf</code>), a font file inside the in the module dir "%moduledir" or the files "%filedir" folder). Example: "arial.ttf". You <em>might</em> find a list of fonts available at !helplink if your system supports it.', array(
'%moduledir' => drupal_get_path('module', 'imagecache_textactions'),
'%filedir' => file_directory_path(),
'!helplink' => l('fonts help', 'admin/help/imagecache_textactions'),
)),
),
'text' => array(
'#type' => 'textarea',
'#rows' => 7,
'#title' => t('Text'),
'#default_value' => $action['text'],
'#description' => t('The text string.'),
),
'evaluate_text' => array(
'#type' => 'checkbox',
'#title' => t('Evaluate text as PHP code'),
'#default_value' => $action['evaluate_text'],
'#description' => t('If selected, the text will be treated as PHP code.
<p>Enter PHP code that will <b>return</b> your dynamic text. Do not use %php tags.
<br />EG <code>return format_date(time());</code>
</p><p>Note that executing incorrect PHP-code can break your Drupal site.
</p><p>You can access the $caption array which contains:<br />
<b>$caption[\'path\']</b> Name of file, e.g. image.jpg<br />
<!--
If the image is a cck imagefield, you will also have access to:<br />
<b>$caption[\'title\']</b> optional imagefield \'title\' text<br />
<b>$caption[\'alt\']</b> optional imagefield \'alt\' text<br />
<b>$caption[\'node\']</b> the complete node variable that the image is attached to.
-->
</p>', array(
'%php' => '<?php ?>',
)),
),
);
return $form;
}
function textactions_text2canvas_validate($form) {
if (!($fontfile = textactions_find_font($form['data']['fontfile']['#value'], TRUE))) {
drupal_set_message(t("Unable to confirm that the font %fontfile is available on your system. This may fail, or your system may provide an appropriate fallback, depending on your toolkit behaviour.", array(
'%fontfile' => $form['data']['fontfile']['#value'],
)), 'warning');
}
else {
drupal_set_message(t("Font was found at %fontfile", array(
'%fontfile' => $fontfile,
)));
}
if (!is_numeric($form['data']['alpha']['#value']) || $form['data']['alpha']['#value'] < 1 || $form['data']['alpha']['#value'] > 100) {
form_set_error('alpha', t('Opacity must be a number between 1 and 100.'));
}
}
function theme_textactions_text2canvas($element) {
$data = $element['#value'];
return "<em><strong>" . $data['text'] . "</strong></em>";
}
function textactions_text2canvas_image(&$image, $action = array()) {
$fontpath = textactions_find_font($action['fontfile']);
if (!$fontpath) {
drupal_set_message(t("Failed to locate the requested font %fontfile. Cannot overlay text onto image.", array(
'%fontfile' => $action['fontfile'],
)));
return FALSE;
}
if ($action['evaluate_text']) {
$text = textactions_evaluate_text($image, $action);
}
else {
$text = $action['text'];
}
$temp = textactions_create_font_image($action['size'], $action['angle'], $fontpath, $text);
if (!$temp) {
drupal_set_message(t('Failed to generate text image. Cannot calculate text dimensions. Not overlaying text.'), 'error');
return;
}
$x_ins = textactions_keyword_filter($action['xpos'], $image->info['width'], $temp['width'], $temp['bx'], 'x');
$y_ins = textactions_keyword_filter($action['ypos'], $image->info['height'], $temp['height'], $temp['by'], 'y');
if ($action['RGB']['HEX'] && ($deduced = hex_to_rgb($action['RGB']['HEX']))) {
$action['RGB'] = array_merge($action['RGB'], $deduced);
}
$action['alpha'] = $action['alpha'] / 100;
$action['RGB']['alpha'] = (1 - $action['alpha']) * 127;
return imageapi_image_overlaytext_alpha($image, $text, $action['size'], $x_ins, $y_ins, $action['RGB'], $fontpath, $action['angle']);
}
function textactions_evaluate_text($image, $action) {
$caption = array();
$caption['path'] = substr($image->source, strrpos($image->source, '/') + 1);
$GLOBALS['caption'] = $caption;
$text = drupal_eval('<' . '?php global $caption; ' . $action['text'] . ' ?' . '>');
$text = eval($action['text']);
$text = check_plain($text);
return $text;
}
function textactions_create_font_image($size, $angle, $font, $char) {
$rect = imagettfbbox($size, 0, $font, $char);
if (!$rect) {
return NULL;
}
if (0 == $angle) {
$imh = $rect[1] - $rect[7];
$imw = $rect[2] - $rect[0];
$bx = -1 - $rect[0];
$by = -1 - $rect[7];
}
else {
$rad = deg2rad($angle);
$sin = sin($rad);
$cos = cos($rad);
if ($angle > 0) {
$tmp = $rect[6] * $cos + $rect[7] * $sin;
$bx = -1 - round($tmp);
$imw = round($rect[2] * $cos + $rect[3] * $sin - $tmp);
$tmp = $rect[5] * $cos - $rect[4] * $sin;
$by = -1 - round($tmp);
$imh = round($rect[1] * $cos - $rect[0] * $sin - $tmp);
}
else {
$tmp = $rect[0] * $cos + $rect[1] * $sin;
$bx = -1 - round($tmp);
$imw = round($rect[4] * $cos + $rect[5] * $sin - $tmp);
$tmp = $rect[7] * $cos - $rect[6] * $sin;
$by = -1 - round($tmp);
$imh = round($rect[3] * $cos - $rect[2] * $sin - $tmp);
}
}
return array(
'width' => $imw,
'height' => $imh,
'bx' => $bx,
'by' => $by,
);
}
function textactions_keyword_filter($value, $current_pixels, $new_pixels, $adj, $xy) {
$v = explode('+', $value);
$v2 = explode('-', $value);
if ($v2[1]) {
$v[1] = -intval($v2[1]);
$v[0] = $v2[0];
}
switch ($v[0]) {
case 'top':
case 'left':
$value = 0;
break;
case 'bottom':
case 'right':
$value = $current_pixels - $new_pixels;
break;
case 'center':
$value = $current_pixels / 2 - $new_pixels / 2;
break;
}
$value = $value + $adj;
if ($v[1]) {
$value = $value + $v[1];
}
return $value;
}
function imageapi_image_overlaytext_alpha(&$image, $text, $size = 12, $x = 0, $y = 0, $RGB = 0, $fontfile = 'MgOpenModernaBold', $angle = 0) {
return call_user_func($image->toolkit . '_image_overlaytext_alpha', $image, $text, $size, $x, $y, $RGB, $fontfile, $angle);
}
function imageapi_gd_image_overlaytext_alpha(&$image, $text, $size = 12, $x = 0, $y = 0, $RGB, $fontfile = 'MgOpenModernaBold', $angle = 0) {
$color = imagecolorallocatealpha($image->resource, $RGB['red'], $RGB['green'], $RGB['blue'], $RGB['alpha']);
imagesavealpha($image->resource, TRUE);
$bounds = imagettftext($image->resource, $size, $angle, $x, $y, $color, $fontfile, $text);
if (empty($bounds)) {
return FALSE;
}
return TRUE;
}
function textactions_find_font($fontpath, $strict = FALSE) {
if (is_file($fontpath)) {
return realpath($fontpath);
}
$fontpath = ltrim($fontpath, '/');
$tryfontpath = drupal_get_path('module', 'imagecache_canvasactions') . '/' . $fontpath;
if (is_file($tryfontpath)) {
return realpath($tryfontpath);
}
$tryfontpath = file_create_path($fontpath);
if (is_file($tryfontpath)) {
return realpath($tryfontpath);
}
if ($strict) {
return FALSE;
}
return $fontpath;
}