How can I add text to images in Drupal 7?

I have the following modules installed:

Imagecache Actions 7.x-1.x-dev
Imagecache Autorotate 7.x-1.x-dev
Imagecache Canvas Actions 7.x-1.x-dev
Imagecache Color Actions 7.x-1.x-dev
Imagecache Custom Actions 7.x-1.x-dev
Imagecache_actions Test Suite 7.x-1.x-dev
SmartCrop

Is there something missing? There is no action to add text.

CommentFileSizeAuthor
imagecache.jpg94.47 KBCptCotton
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dman’s picture

Status: Active » Postponed

textactions has been moved out of this module from DRUPAL-6-2.x (and therefore also D7)
The successor is to be http://drupal.org/project/imageapi_text - however that has no release in D7, and is still very beta in D6.

In short, it hasn't been built yet, and won't be (by me) until next time I need this function myself. My roadmap for this one is solid, but long, and it hasn't been a priority for a while.

farald’s picture

Subscribing, in case you change your mind:)

FreeFox’s picture

I installed the module for this feature but ... it wasn't there ...
Maybe better combine all the image related modules into 1 big one.

Murz’s picture

subscribe

Axel Pressbutton’s picture

Subscribing...just in case a working solution for D7 appears at any time as it's a very handy addition.

mandrzej’s picture

subs

Lostmonkey’s picture

subscribe

Jorge Navarro’s picture

subscribe

P.Selfin’s picture

subscribe to

fietserwin’s picture

Status: Postponed » Needs review

Please see my (temporary) sandbox project Image Effects Text: http://drupal.org/sandbox/fietserwin/1435964. File any issue over there. When the project has stabilized enough it can (hopefully) be incorporated into the imagecache actions project.

Jorge Navarro’s picture

fietserwin, thanks, I'm getting this error:
Fatal error: Call to undefined function file_create_path() in /home/user/domain.com/sites/all/modules/image_effects_text/image_effects_text.inc on line 602

fietserwin’s picture

Thanks for reporting. An untested code path which had some D6 remains. Solved as per #1437124: Fatal error: Call to undefined function file_create_path() in /image_effects_text/image_effects_text.inc.

eugen80’s picture

After installing I got this:

Notice: Undefined variable: effect in image_effects_text_help() (line 46 of sites/all/modules/image_effects_text/image_effects_text.module).

Warning: Missing argument 2 for image_effects_text_help_inc(), called in sites/all/modules/image_effects_text/image_effects_text.module on line 46 and defined in image_effects_text_help_inc() (line 15 of sites/all/modules/image_effects_text/image_effects_text.inc).

eugen80’s picture

After adding Text effect with default settings, i got this:

Warning: is_readable() [function.is-readable]: Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in image_effects_text_find_font() (line 607 of sites/all/modules/image_effects_text/image_effects_text.inc).

Warning: imagettftext() [function.imagettftext]: Could not find/open font in image_gd_image_effects_text() (line 352 of sites/all/modules/image_effects_text/image_effects_text.inc).

drubb’s picture

Here's an example how this could be done using an imagecache custom action (PHP). It's an adoption of the code found at http://www.9lessons.info/2011/11/php-image-and-text-watermark.html

$fontsize = 72;
$fontfile = 'sites/default/files/myfont.ttf';
$text = 'Some text on the image';

$box = imagettfbbox($fontsize, 0, $fontfile, $text);
$textwidth = abs($box[4] - $box[0]);
$textheight = abs($box[5] - $box[1]);
$xcord = ($width/2)-($textwidth/2)-2;
$ycord = ($height/2)+($textheight/2);
imagettftext ($image->resource, $fontsize, 0, $xcord, $ycord, 0x00FFFFFF, $fontfile, $text);
return $image;

This calculates the height and width of the watermark text and centers it on the image.

The code should work, but I haven't tested it thoroughly, because I have a bug in my ICA version: using this (or any other) custom code works with drupal's sample.png, but not with other images. I'm searching for the reason, perhaps a problem with filesystem access rights.

Hope it helps!

drubb’s picture

Finally found the error: the code above doesn't work, because you have to do the image operations using a copy of the original image! So here's the complete working code:

$fontsize = 72;
$fontfile = 'sites/default/files/myfont.ttf';
$text = 'Some text on the image';

$img2 = imagecreatetruecolor($width, $height);
imagecopyresampled($img2, $image->resource, 0, 0, 0, 0, $width, $height, $width, $height);

$box = imagettfbbox($fontsize, 0, $fontfile, $text);
$textwidth = abs($box[4] - $box[0]);
$textheight = abs($box[5] - $box[1]);
$xcord = ($width/2)-($textwidth/2)-2;
$ycord = ($height/2)+($textheight/2);
imagettftext ($img2, $fontsize, 0, $xcord, $ycord, 0x00FFFFFF, $fontfile, $text);

imagecopyresampled($image->resource, $img2, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($img2);

return $image;

Crystian’s picture

Thanks! work for me!

immoreel’s picture

To get the above to work (I got PHP Fatal error: Call to undefined function entity_load_single() in /var/www/vhosts/xxxxxxxx.com/httpdocs/sites/all/modules/imagecache_actions/utility.inc on line 289) I needed the http://drupal.org/project/entity module

denjell’s picture

#16 worked for me, however i am very interested in knowing how to call $fields in the custom php

"If possible, an array of owning $fields may also be available."

but how???

dotist’s picture

thanks for the snippet, drubb #16 - works great.
i'm also trying to print some field info on the image - a timestamp, the viewers username and the url...
there's another suggested solution here, but it's unclear to me how they are printing the field elements that it seems they are grabbing in the code...
any further suggestion would be really appreciated!

shp’s picture

Subscribing for this feature.

fietserwin’s picture

Title: How can I add text to images (D7) ? » Add text effect to D7
Status: Needs review » Fixed

Fixed and committed. The sandbox project is now a submodule of imagecache_actions.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.